Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
import pandas._testing as tm
from pandas.core import nanops
from pandas.core.arrays.string_arrow import ArrowStringArrayNumpySemantics


def get_objs():
Expand Down Expand Up @@ -57,7 +58,11 @@ class TestReductions:
def test_ops(self, opname, obj):
result = getattr(obj, opname)()
if not isinstance(obj, PeriodIndex):
expected = getattr(obj.values, opname)()
if isinstance(obj.values, ArrowStringArrayNumpySemantics):
# max not on the interface
expected = getattr(np.array(obj.values), opname)()
else:
expected = getattr(obj.values, opname)()
else:
expected = Period(ordinal=getattr(obj.asi8, opname)(), freq=obj.freq)

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

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

# attempting to compare np.nan with string raises
ser3 = Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"])
msg = "'>' not supported between instances of 'float' and 'str'"
with pytest.raises(TypeError, match=msg):
ser3.idxmax()
with pytest.raises(TypeError, match=msg):
ser3.idxmax(skipna=False)
msg = "'<' not supported between instances of 'float' and 'str'"
with pytest.raises(TypeError, match=msg):
ser3.idxmin()
with pytest.raises(TypeError, match=msg):
ser3.idxmin(skipna=False)
if not using_infer_string:
# attempting to compare np.nan with string raises
ser3 = Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"])
msg = "'>' not supported between instances of 'float' and 'str'"
with pytest.raises(TypeError, match=msg):
ser3.idxmax()
with pytest.raises(TypeError, match=msg):
ser3.idxmax(skipna=False)
msg = "'<' not supported between instances of 'float' and 'str'"
with pytest.raises(TypeError, match=msg):
ser3.idxmin()
with pytest.raises(TypeError, match=msg):
ser3.idxmin(skipna=False)

def test_idxminmax_object_frame(self):
# GH#4279
Expand Down Expand Up @@ -1445,14 +1451,14 @@ def test_mode_str_obj(self, dropna, expected1, expected2, expected3):

s = Series(data, dtype=object)
result = s.mode(dropna)
expected2 = Series(expected2, dtype=object)
expected2 = Series(expected2, dtype=None if expected2 == ["bar"] else object)
tm.assert_series_equal(result, expected2)

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

s = Series(data, dtype=object).astype(str)
result = s.mode(dropna)
expected3 = Series(expected3, dtype=str)
expected3 = Series(expected3)
tm.assert_series_equal(result, expected3)

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

s = Series([1, "foo", "foo", np.nan, np.nan, np.nan])
result = s.mode(dropna)
expected = Series(expected2, dtype=object)
expected = Series(expected2, dtype=None if expected2 == ["foo"] else object)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
Expand Down