Skip to content
18 changes: 18 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,24 @@ def string_dtype(request):
return request.param


@pytest.fixture(
params=[
"string",
pytest.param(
"arrow_string", marks=td.skip_if_no("pyarrow", min_version="1.0.0")
),
]
)
def nullable_string_dtype(request):
"""
Parametrized fixture for string dtypes.

* 'string'
* 'arrow_string'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will change in #39908, but getting the fixture in play as a pre-cursor should still be an advantage.

"""
return request.param


@pytest.fixture(params=tm.BYTES_DTYPES)
def bytes_dtype(request):
"""
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ def test_is_string_dtype():
assert com.is_string_dtype(object)
assert com.is_string_dtype(np.array(["a", "b"]))
assert com.is_string_dtype(pd.StringDtype())
assert com.is_string_dtype(pd.array(["a", "b"], dtype="string"))


def test_is_string_dtype_nullable(nullable_string_dtype):
assert com.is_string_dtype(pd.array(["a", "b"], dtype=nullable_string_dtype))


integer_dtypes: List = []
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
ExtensionDtype,
)
from pandas.api.types import is_bool_dtype
from pandas.core.arrays.string_arrow import ArrowStringDtype


class JSONDtype(ExtensionDtype):
Expand Down Expand Up @@ -194,7 +195,7 @@ def astype(self, dtype, copy=True):
if copy:
return self.copy()
return self
elif isinstance(dtype, StringDtype):
elif isinstance(dtype, (StringDtype, ArrowStringDtype)):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrowStringDtype is removed in #39908 and therefore this change will be reverted.

value = self.astype(str) # numpy doesn'y like nested dicts
return dtype.construct_array_type()._from_sequence(value, copy=False)

Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/frame/methods/test_combine_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,17 @@ def test_combine_first_with_asymmetric_other(self, val):

tm.assert_frame_equal(res, exp)

def test_combine_first_string_dtype_only_na(self):
def test_combine_first_string_dtype_only_na(self, nullable_string_dtype):
# GH: 37519
df = DataFrame({"a": ["962", "85"], "b": [pd.NA] * 2}, dtype="string")
df2 = DataFrame({"a": ["85"], "b": [pd.NA]}, dtype="string")
df = DataFrame(
{"a": ["962", "85"], "b": [pd.NA] * 2}, dtype=nullable_string_dtype
)
df2 = DataFrame({"a": ["85"], "b": [pd.NA]}, dtype=nullable_string_dtype)
df.set_index(["a", "b"], inplace=True)
df2.set_index(["a", "b"], inplace=True)
result = df.combine_first(df2)
expected = DataFrame(
{"a": ["962", "85"], "b": [pd.NA] * 2}, dtype="string"
{"a": ["962", "85"], "b": [pd.NA] * 2}, dtype=nullable_string_dtype
).set_index(["a", "b"])
tm.assert_frame_equal(result, expected)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,10 +1654,10 @@ def test_constructor_empty_with_string_dtype(self):
df = DataFrame(index=[0, 1], columns=[0, 1], dtype="U5")
tm.assert_frame_equal(df, expected)

def test_constructor_empty_with_string_extension(self):
def test_constructor_empty_with_string_extension(self, nullable_string_dtype):
# GH 34915
expected = DataFrame(index=[], columns=["c1"], dtype="string")
df = DataFrame(columns=["c1"], dtype="string")
expected = DataFrame(index=[], columns=["c1"], dtype=nullable_string_dtype)
df = DataFrame(columns=["c1"], dtype=nullable_string_dtype)
tm.assert_frame_equal(df, expected)

def test_constructor_single_value(self):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tools/test_to_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,9 @@ def test_precision_float_conversion(strrep):
(["1", "2", "3.5"], Series([1, 2, 3.5])),
],
)
def test_to_numeric_from_nullable_string(values, expected):
def test_to_numeric_from_nullable_string(values, nullable_string_dtype, expected):
# https://github.com/pandas-dev/pandas/issues/37262
s = Series(values, dtype="string")
s = Series(values, dtype=nullable_string_dtype)
result = to_numeric(s)
tm.assert_series_equal(result, expected)

Expand Down