Skip to content

Commit 15349da

Browse files
committed
update tests
1 parent a1cb2b1 commit 15349da

File tree

8 files changed

+63
-19
lines changed

8 files changed

+63
-19
lines changed

pandas/tests/frame/test_stack_unstack.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ def test_unstack_period_frame(self):
21132113
@pytest.mark.filterwarnings(
21142114
"ignore:The previous implementation of stack is deprecated"
21152115
)
2116-
def test_stack_multiple_bug(self, future_stack):
2116+
def test_stack_multiple_bug(self, future_stack, using_infer_string):
21172117
# bug when some uniques are not present in the data GH#3170
21182118
id_col = ([1] * 3) + ([2] * 3)
21192119
name = (["a"] * 3) + (["b"] * 3)
@@ -2125,6 +2125,8 @@ def test_stack_multiple_bug(self, future_stack):
21252125
multi.columns.name = "Params"
21262126
unst = multi.unstack("ID")
21272127
msg = re.escape("agg function failed [how->mean,dtype->")
2128+
if using_infer_string:
2129+
msg = "str dtype does not support mean operations"
21282130
with pytest.raises(TypeError, match=msg):
21292131
unst.resample("W-THU").mean()
21302132
down = unst.resample("W-THU").mean(numeric_only=True)

pandas/tests/groupby/test_groupby.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def test_frame_multi_key_function_list():
428428
tm.assert_frame_equal(agged, expected)
429429

430430

431-
def test_frame_multi_key_function_list_partial_failure():
431+
def test_frame_multi_key_function_list_partial_failure(using_infer_string):
432432
data = DataFrame(
433433
{
434434
"A": [
@@ -479,6 +479,8 @@ def test_frame_multi_key_function_list_partial_failure():
479479
grouped = data.groupby(["A", "B"])
480480
funcs = ["mean", "std"]
481481
msg = re.escape("agg function failed [how->mean,dtype->")
482+
if using_infer_string:
483+
msg = "str dtype does not support mean operations"
482484
with pytest.raises(TypeError, match=msg):
483485
grouped.agg(funcs)
484486

@@ -665,9 +667,11 @@ def test_groupby_multi_corner(df):
665667
tm.assert_frame_equal(agged, expected)
666668

667669

668-
def test_raises_on_nuisance(df):
670+
def test_raises_on_nuisance(df, using_infer_string):
669671
grouped = df.groupby("A")
670672
msg = re.escape("agg function failed [how->mean,dtype->")
673+
if using_infer_string:
674+
msg = "str dtype does not support mean operations"
671675
with pytest.raises(TypeError, match=msg):
672676
grouped.agg("mean")
673677
with pytest.raises(TypeError, match=msg):
@@ -743,9 +747,11 @@ def test_raise_on_nuisance_python_single(df):
743747
grouped.skew()
744748

745749

746-
def test_raise_on_nuisance_python_multiple(three_group):
750+
def test_raise_on_nuisance_python_multiple(three_group, using_infer_string):
747751
grouped = three_group.groupby(["A", "B"])
748752
msg = re.escape("agg function failed [how->mean,dtype->")
753+
if using_infer_string:
754+
msg = "str dtype does not support mean operations"
749755
with pytest.raises(TypeError, match=msg):
750756
grouped.agg("mean")
751757
with pytest.raises(TypeError, match=msg):
@@ -783,12 +789,16 @@ def test_nonsense_func():
783789
df.groupby(lambda x: x + "foo")
784790

785791

786-
def test_wrap_aggregated_output_multindex(multiindex_dataframe_random_data):
792+
def test_wrap_aggregated_output_multindex(
793+
multiindex_dataframe_random_data, using_infer_string
794+
):
787795
df = multiindex_dataframe_random_data.T
788796
df["baz", "two"] = "peekaboo"
789797

790798
keys = [np.array([0, 0, 1]), np.array([0, 0, 1])]
791799
msg = re.escape("agg function failed [how->mean,dtype->")
800+
if using_infer_string:
801+
msg = "str dtype does not support mean operations"
792802
with pytest.raises(TypeError, match=msg):
793803
df.groupby(keys).agg("mean")
794804
agged = df.drop(columns=("baz", "two")).groupby(keys).agg("mean")

pandas/tests/groupby/test_groupby_subclass.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_groupby_resample_preserves_subclass(obj):
109109

110110
df = obj(
111111
{
112-
"Buyer": "Carl Carl Carl Carl Joe Carl".split(),
112+
"Buyer": Series("Carl Carl Carl Carl Joe Carl".split(), dtype=object),
113113
"Quantity": [18, 3, 5, 1, 9, 3],
114114
"Date": [
115115
datetime(2013, 9, 1, 13, 0),

pandas/tests/groupby/test_numeric_only.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def _check(self, df, method, expected_columns, expected_columns_numeric):
180180
"category type does not support sum operations",
181181
re.escape(f"agg function failed [how->{method},dtype->object]"),
182182
re.escape(f"agg function failed [how->{method},dtype->string]"),
183-
re.escape(f"agg function failed [how->{method},dtype->str]"),
183+
f"str dtype does not support {method} operations",
184184
]
185185
)
186186
with pytest.raises(exception, match=msg):
@@ -198,7 +198,7 @@ def _check(self, df, method, expected_columns, expected_columns_numeric):
198198
f"Cannot perform {method} with non-ordered Categorical",
199199
re.escape(f"agg function failed [how->{method},dtype->object]"),
200200
re.escape(f"agg function failed [how->{method},dtype->string]"),
201-
re.escape(f"agg function failed [how->{method},dtype->str]"),
201+
f"str dtype does not support {method} operations",
202202
]
203203
)
204204
with pytest.raises(exception, match=msg):

pandas/tests/groupby/test_raises.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,13 @@ def test_groupby_raises_string_np(
274274

275275
if using_infer_string:
276276
klass = TypeError
277-
msg = (
278-
"'.*StringArrayNumpySemantics' with dtype str does not "
279-
f"support operation '{groupby_func_np.__name__}'"
280-
)
277+
if df["d"].dtype.storage == "python":
278+
msg = "Cannot perform reduction 'mean' with string dtype"
279+
else:
280+
msg = (
281+
"'ArrowStringArrayNumpySemantics' with dtype str does not "
282+
f"support operation '{groupby_func_np.__name__}'"
283+
)
281284

282285
_call_and_check(klass, msg, how, gb, groupby_func_np, ())
283286

pandas/tests/resample/test_resample_api.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_api_compat_before_use(attr):
187187
getattr(rs, attr)
188188

189189

190-
def tests_raises_on_nuisance(test_frame):
190+
def tests_raises_on_nuisance(test_frame, using_infer_string):
191191
df = test_frame
192192
df["D"] = "foo"
193193
r = df.resample("h")
@@ -197,6 +197,8 @@ def tests_raises_on_nuisance(test_frame):
197197

198198
expected = r[["A", "B", "C"]].mean()
199199
msg = re.escape("agg function failed [how->mean,dtype->")
200+
if using_infer_string:
201+
msg = "str dtype does not support mean operations"
200202
with pytest.raises(TypeError, match=msg):
201203
r.mean()
202204
result = r.mean(numeric_only=True)
@@ -881,7 +883,9 @@ def test_end_and_end_day_origin(
881883
("sem", lib.no_default, "could not convert string to float"),
882884
],
883885
)
884-
def test_frame_downsample_method(method, numeric_only, expected_data):
886+
def test_frame_downsample_method(
887+
method, numeric_only, expected_data, using_infer_string
888+
):
885889
# GH#46442 test if `numeric_only` behave as expected for DataFrameGroupBy
886890

887891
index = date_range("2018-01-01", periods=2, freq="D")
@@ -898,11 +902,21 @@ def test_frame_downsample_method(method, numeric_only, expected_data):
898902
if method in ("var", "mean", "median", "prod"):
899903
klass = TypeError
900904
msg = re.escape(f"agg function failed [how->{method},dtype->")
905+
if using_infer_string:
906+
msg = f"str dtype does not support {method} operations"
907+
elif method in ["sum", "std", "sem"] and using_infer_string:
908+
klass = TypeError
909+
msg = f"str dtype does not support {method} operations"
901910
else:
902911
klass = ValueError
903912
msg = expected_data
904913
with pytest.raises(klass, match=msg):
905914
_ = func(**kwargs)
915+
elif method == "sum" and using_infer_string and numeric_only is not True:
916+
klass = TypeError
917+
msg = "str dtype does not support sum operations"
918+
with pytest.raises(klass, match=msg):
919+
_ = func(**kwargs)
906920
else:
907921
result = func(**kwargs)
908922
expected = DataFrame(expected_data, index=expected_index)
@@ -932,7 +946,9 @@ def test_frame_downsample_method(method, numeric_only, expected_data):
932946
("last", lib.no_default, ["cat_2"]),
933947
],
934948
)
935-
def test_series_downsample_method(method, numeric_only, expected_data):
949+
def test_series_downsample_method(
950+
method, numeric_only, expected_data, using_infer_string
951+
):
936952
# GH#46442 test if `numeric_only` behave as expected for SeriesGroupBy
937953

938954
index = date_range("2018-01-01", periods=2, freq="D")
@@ -948,8 +964,15 @@ def test_series_downsample_method(method, numeric_only, expected_data):
948964
func(**kwargs)
949965
elif method == "prod":
950966
msg = re.escape("agg function failed [how->prod,dtype->")
967+
if using_infer_string:
968+
msg = "str dtype does not support prod operations"
969+
with pytest.raises(TypeError, match=msg):
970+
func(**kwargs)
971+
elif method == "sum" and using_infer_string and numeric_only is not True:
972+
msg = "str dtype does not support sum operations"
951973
with pytest.raises(TypeError, match=msg):
952974
func(**kwargs)
975+
953976
else:
954977
result = func(**kwargs)
955978
expected = Series(expected_data, index=expected_index)

pandas/tests/reshape/merge/test_join.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ def test_join_non_unique_period_index(self):
620620
)
621621
tm.assert_frame_equal(result, expected)
622622

623-
def test_mixed_type_join_with_suffix(self):
623+
def test_mixed_type_join_with_suffix(self, using_infer_string):
624624
# GH #916
625625
df = DataFrame(
626626
np.random.default_rng(2).standard_normal((20, 6)),
@@ -631,6 +631,8 @@ def test_mixed_type_join_with_suffix(self):
631631

632632
grouped = df.groupby("id")
633633
msg = re.escape("agg function failed [how->mean,dtype->")
634+
if using_infer_string:
635+
msg = "str dtype does not support mean operations"
634636
with pytest.raises(TypeError, match=msg):
635637
grouped.mean()
636638
mn = grouped.mean(numeric_only=True)

pandas/tests/reshape/test_pivot.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_pivot_table_categorical_observed_equal(self, observed):
135135

136136
tm.assert_frame_equal(result, expected)
137137

138-
def test_pivot_table_nocols(self):
138+
def test_pivot_table_nocols(self, using_infer_string):
139139
df = DataFrame(
140140
{"rows": ["a", "b", "c"], "cols": ["x", "y", "z"], "values": [1, 2, 3]}
141141
)
@@ -935,12 +935,14 @@ def test_margins(self, data):
935935
for value_col in table.columns.levels[0]:
936936
self._check_output(table[value_col], value_col, data)
937937

938-
def test_no_col(self, data):
938+
def test_no_col(self, data, using_infer_string):
939939
# no col
940940

941941
# to help with a buglet
942942
data.columns = [k * 2 for k in data.columns]
943943
msg = re.escape("agg function failed [how->mean,dtype->")
944+
if using_infer_string:
945+
msg = "str dtype does not support mean operations"
944946
with pytest.raises(TypeError, match=msg):
945947
data.pivot_table(index=["AA", "BB"], margins=True, aggfunc="mean")
946948
table = data.drop(columns="CC").pivot_table(
@@ -990,7 +992,7 @@ def test_no_col(self, data):
990992
],
991993
)
992994
def test_margin_with_only_columns_defined(
993-
self, columns, aggfunc, values, expected_columns
995+
self, columns, aggfunc, values, expected_columns, using_infer_string
994996
):
995997
# GH 31016
996998
df = DataFrame(
@@ -1014,6 +1016,8 @@ def test_margin_with_only_columns_defined(
10141016
)
10151017
if aggfunc != "sum":
10161018
msg = re.escape("agg function failed [how->mean,dtype->")
1019+
if using_infer_string:
1020+
msg = "str dtype does not support mean operations"
10171021
with pytest.raises(TypeError, match=msg):
10181022
df.pivot_table(columns=columns, margins=True, aggfunc=aggfunc)
10191023
if "B" not in columns:

0 commit comments

Comments
 (0)