Skip to content

Commit

Permalink
TST (string dtype): update tests/reductions tests (#60133)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored Oct 31, 2024
1 parent 2fdb16b commit 4651ddb
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ def test_idxminmax_object_dtype(self, using_infer_string):
with pytest.raises(TypeError, match=msg):
ser3.idxmin(skipna=False)

# TODO(infer_string) implement argmin/max for python string dtype
@pytest.mark.xfail(
using_string_dtype() and not HAS_PYARROW, reason="TODO(infer_string)"
)
Expand Down Expand Up @@ -1431,12 +1432,14 @@ def test_mode_numerical_nan(self, dropna, expected):
expected = Series(expected)
tm.assert_series_equal(result, expected)

@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
@pytest.mark.parametrize(
"dropna, expected1, expected2, expected3",
[(True, ["b"], ["bar"], ["nan"]), (False, ["b"], [np.nan], ["nan"])],
"dropna, expected1, expected2",
[
(True, ["b"], ["bar"]),
(False, ["b"], [np.nan]),
],
)
def test_mode_str_obj(self, dropna, expected1, expected2, expected3):
def test_mode_object(self, dropna, expected1, expected2):
# Test string and object types.
data = ["a"] * 2 + ["b"] * 3

Expand All @@ -1449,30 +1452,45 @@ def test_mode_str_obj(self, dropna, expected1, expected2, expected3):

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

@pytest.mark.parametrize(
"dropna, expected1, expected2",
[
(True, ["b"], ["bar"]),
(False, ["b"], [np.nan]),
],
)
def test_mode_string(self, dropna, expected1, expected2, any_string_dtype):
# Test string and object types.
data = ["a"] * 2 + ["b"] * 3

s = Series(data, dtype=any_string_dtype)
result = s.mode(dropna)
expected1 = Series(expected1, dtype=any_string_dtype)
tm.assert_series_equal(result, expected1)

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

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

@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
@pytest.mark.parametrize(
"dropna, expected1, expected2",
[(True, ["foo"], ["foo"]), (False, ["foo"], [np.nan])],
)
def test_mode_mixeddtype(self, dropna, expected1, expected2):
s = Series([1, "foo", "foo"])
result = s.mode(dropna)
expected = Series(expected1)
expected = Series(expected1, dtype=object)
tm.assert_series_equal(result, expected)

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

@pytest.mark.parametrize(
Expand Down Expand Up @@ -1597,12 +1615,11 @@ def test_mode_intoverflow(self, dropna, expected1, expected2):
expected2 = Series(expected2, dtype=np.uint64)
tm.assert_series_equal(result, expected2)

@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
def test_mode_sortwarning(self):
# Check for the warning that is raised when the mode
# results cannot be sorted

expected = Series(["foo", np.nan])
expected = Series(["foo", np.nan], dtype=object)
s = Series([1, "foo", "foo", np.nan, np.nan])

with tm.assert_produces_warning(UserWarning, match="Unable to sort modes"):
Expand Down

0 comments on commit 4651ddb

Please sign in to comment.