Skip to content

Commit 0871326

Browse files
committed
Update now that .sum() is supported
1 parent f3c44cb commit 0871326

File tree

7 files changed

+12
-35
lines changed

7 files changed

+12
-35
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,6 @@ def _groupby_op(
23042304
):
23052305
if isinstance(self.dtype, StringDtype):
23062306
if how in [
2307-
"sum",
23082307
"prod",
23092308
"mean",
23102309
"median",

pandas/core/arrays/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2609,7 +2609,6 @@ def _groupby_op(
26092609
if isinstance(self.dtype, StringDtype):
26102610
# StringArray
26112611
if op.how in [
2612-
"sum",
26132612
"prod",
26142613
"mean",
26152614
"median",

pandas/tests/extension/base/groupby.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
is_bool_dtype,
77
is_numeric_dtype,
88
is_object_dtype,
9+
is_string_dtype,
910
)
1011

1112
import pandas as pd
@@ -150,6 +151,7 @@ def test_in_numeric_groupby(self, data_for_grouping):
150151
is_numeric_dtype(dtype)
151152
or is_bool_dtype(dtype)
152153
or dtype.name == "decimal"
154+
or is_string_dtype(dtype)
153155
or is_object_dtype(dtype)
154156
or dtype.kind == "m" # in particular duration[*][pyarrow]
155157
):

pandas/tests/groupby/aggregate/test_cython.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ def test_cython_agg_return_dict():
148148

149149
def test_cython_fail_agg():
150150
dr = bdate_range("1/1/2000", periods=50)
151-
ts = Series(["A", "B", "C", "D", "E"] * 10, index=dr)
151+
ts = Series(["A", "B", "C", "D", "E"] * 10, dtype=object, index=dr)
152152

153153
grouped = ts.groupby(lambda x: x.month)
154154
summed = grouped.sum()
155-
expected = grouped.agg(np.sum)
155+
expected = grouped.agg(np.sum).astype(object)
156156
tm.assert_series_equal(summed, expected)
157157

158158

pandas/tests/groupby/test_groupby.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,6 @@ def test_omit_nuisance_agg(df, agg_function, numeric_only, using_infer_string):
709709
grouped = df.groupby("A")
710710

711711
no_drop_nuisance = ("var", "std", "sem", "mean", "prod", "median")
712-
if using_infer_string:
713-
no_drop_nuisance += ("sum",)
714712
if agg_function in no_drop_nuisance and not numeric_only:
715713
# Added numeric_only as part of GH#46560; these do not drop nuisance
716714
# columns when numeric_only is False
@@ -1814,7 +1812,7 @@ def get_categorical_invalid_expected():
18141812

18151813
if op in ["prod", "sum", "skew"]:
18161814
# ops that require more than just ordered-ness
1817-
if is_dt64 or is_cat or is_per or is_str:
1815+
if is_dt64 or is_cat or is_per or (is_str and op != "sum"):
18181816
# GH#41291
18191817
# datetime64 -> prod and sum are invalid
18201818
if is_dt64:

pandas/tests/groupby/test_raises.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ def test_groupby_raises_string(
182182

183183
if using_infer_string:
184184
if groupby_func in [
185-
"sum",
186185
"prod",
187186
"mean",
188187
"median",
@@ -213,13 +212,7 @@ def test_groupby_raises_string(
213212
elif groupby_func in ["cummin", "cummax"]:
214213
msg = msg.replace("object", "str")
215214
elif groupby_func == "corrwith":
216-
if df["d"].dtype.storage == "pyarrow":
217-
msg = (
218-
"ArrowStringArrayNumpySemantics' with dtype str does not "
219-
"support operation 'mean'"
220-
)
221-
else:
222-
msg = "Cannot perform reduction 'mean' with string dtype"
215+
msg = "Cannot perform reduction 'mean' with string dtype"
223216

224217
if groupby_func == "fillna":
225218
kind = "Series" if groupby_series else "DataFrame"
@@ -273,17 +266,12 @@ def test_groupby_raises_string_np(
273266
}[groupby_func_np]
274267

275268
if using_infer_string:
276-
klass = TypeError
277-
if df["d"].dtype.storage == "python":
278-
msg = (
279-
f"Cannot perform reduction '{groupby_func_np.__name__}' "
280-
"with string dtype"
281-
)
282-
else:
283-
msg = (
284-
"'ArrowStringArrayNumpySemantics' with dtype str does not "
285-
f"support operation '{groupby_func_np.__name__}'"
286-
)
269+
if groupby_func_np is np.mean:
270+
klass = TypeError
271+
msg = (
272+
f"Cannot perform reduction '{groupby_func_np.__name__}' "
273+
"with string dtype"
274+
)
287275

288276
_call_and_check(klass, msg, how, gb, groupby_func_np, ())
289277

pandas/tests/resample/test_resample_api.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -912,11 +912,6 @@ def test_frame_downsample_method(
912912
msg = expected_data
913913
with pytest.raises(klass, match=msg):
914914
_ = func(**kwargs)
915-
elif method == "sum" and using_infer_string and numeric_only is not True:
916-
klass = TypeError
917-
msg = f"dtype 'str' does not support operation '{method}'"
918-
with pytest.raises(klass, match=msg):
919-
_ = func(**kwargs)
920915
else:
921916
result = func(**kwargs)
922917
expected = DataFrame(expected_data, index=expected_index)
@@ -968,10 +963,6 @@ def test_series_downsample_method(
968963
msg = "dtype 'str' does not support operation 'prod'"
969964
with pytest.raises(TypeError, match=msg):
970965
func(**kwargs)
971-
elif method == "sum" and using_infer_string and numeric_only is not True:
972-
msg = "dtype 'str' does not support operation 'sum'"
973-
with pytest.raises(TypeError, match=msg):
974-
func(**kwargs)
975966

976967
else:
977968
result = func(**kwargs)

0 commit comments

Comments
 (0)