Skip to content

Commit 472516c

Browse files
authored
Adjust reduction folder tests for string option (#56143)
1 parent bd27a3e commit 472516c

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

pandas/tests/reductions/test_reductions.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
import pandas._testing as tm
3030
from pandas.core import nanops
31+
from pandas.core.arrays.string_arrow import ArrowStringArrayNumpySemantics
3132

3233

3334
def get_objs():
@@ -57,7 +58,11 @@ class TestReductions:
5758
def test_ops(self, opname, obj):
5859
result = getattr(obj, opname)()
5960
if not isinstance(obj, PeriodIndex):
60-
expected = getattr(obj.values, opname)()
61+
if isinstance(obj.values, ArrowStringArrayNumpySemantics):
62+
# max not on the interface
63+
expected = getattr(np.array(obj.values), opname)()
64+
else:
65+
expected = getattr(obj.values, opname)()
6166
else:
6267
expected = Period(ordinal=getattr(obj.asi8, opname)(), freq=obj.freq)
6368

@@ -1165,7 +1170,7 @@ def test_assert_idxminmax_empty_raises(self, test_input, error_type):
11651170
with pytest.raises(ValueError, match=msg):
11661171
test_input.idxmax(skipna=False)
11671172

1168-
def test_idxminmax_object_dtype(self):
1173+
def test_idxminmax_object_dtype(self, using_infer_string):
11691174
# pre-2.1 object-dtype was disallowed for argmin/max
11701175
ser = Series(["foo", "bar", "baz"])
11711176
assert ser.idxmax() == 0
@@ -1179,18 +1184,19 @@ def test_idxminmax_object_dtype(self):
11791184
assert ser2.idxmin() == 0
11801185
assert ser2.idxmin(skipna=False) == 0
11811186

1182-
# attempting to compare np.nan with string raises
1183-
ser3 = Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"])
1184-
msg = "'>' not supported between instances of 'float' and 'str'"
1185-
with pytest.raises(TypeError, match=msg):
1186-
ser3.idxmax()
1187-
with pytest.raises(TypeError, match=msg):
1188-
ser3.idxmax(skipna=False)
1189-
msg = "'<' not supported between instances of 'float' and 'str'"
1190-
with pytest.raises(TypeError, match=msg):
1191-
ser3.idxmin()
1192-
with pytest.raises(TypeError, match=msg):
1193-
ser3.idxmin(skipna=False)
1187+
if not using_infer_string:
1188+
# attempting to compare np.nan with string raises
1189+
ser3 = Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"])
1190+
msg = "'>' not supported between instances of 'float' and 'str'"
1191+
with pytest.raises(TypeError, match=msg):
1192+
ser3.idxmax()
1193+
with pytest.raises(TypeError, match=msg):
1194+
ser3.idxmax(skipna=False)
1195+
msg = "'<' not supported between instances of 'float' and 'str'"
1196+
with pytest.raises(TypeError, match=msg):
1197+
ser3.idxmin()
1198+
with pytest.raises(TypeError, match=msg):
1199+
ser3.idxmin(skipna=False)
11941200

11951201
def test_idxminmax_object_frame(self):
11961202
# GH#4279
@@ -1445,14 +1451,14 @@ def test_mode_str_obj(self, dropna, expected1, expected2, expected3):
14451451

14461452
s = Series(data, dtype=object)
14471453
result = s.mode(dropna)
1448-
expected2 = Series(expected2, dtype=object)
1454+
expected2 = Series(expected2, dtype=None if expected2 == ["bar"] else object)
14491455
tm.assert_series_equal(result, expected2)
14501456

14511457
data = ["foo", "bar", "bar", np.nan, np.nan, np.nan]
14521458

14531459
s = Series(data, dtype=object).astype(str)
14541460
result = s.mode(dropna)
1455-
expected3 = Series(expected3, dtype=str)
1461+
expected3 = Series(expected3)
14561462
tm.assert_series_equal(result, expected3)
14571463

14581464
@pytest.mark.parametrize(
@@ -1467,7 +1473,7 @@ def test_mode_mixeddtype(self, dropna, expected1, expected2):
14671473

14681474
s = Series([1, "foo", "foo", np.nan, np.nan, np.nan])
14691475
result = s.mode(dropna)
1470-
expected = Series(expected2, dtype=object)
1476+
expected = Series(expected2, dtype=None if expected2 == ["foo"] else object)
14711477
tm.assert_series_equal(result, expected)
14721478

14731479
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)