-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Implement some reductions for string Series #31757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
c87ce47
5365fa1
460030f
6497e16
c052feb
4312b4f
b67cc56
214995f
95c83fb
4082ae3
12a2323
4a61b81
c6465e5
5c1a5fb
ca99650
18800e1
3eddb73
0c1a2a3
5714c06
5e01c3f
0e4a4e2
ed01138
1128950
7c0f05d
d84f5bd
7db2052
e19bbe1
df99b4f
ba20705
d69b587
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,7 +274,16 @@ def astype(self, dtype, copy=True): | |
return super().astype(dtype, copy) | ||
|
||
def _reduce(self, name, skipna=True, **kwargs): | ||
raise TypeError(f"Cannot perform reduction '{name}' with string dtype") | ||
if name in ["min", "max", "sum"]: | ||
na_mask = isna(self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the masking should be done inside the methods themselves, _reduce just dispatches There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we implement these methods for StringArray in that case? The NA handling for PandasArray seems to be broken for string inputs, so it might have to get handled within each method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I would say don't care about PandasArray too much (since PandasArray is not using pd.NA), and just implement the methods here on StringArray. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the reason why the NA-handling wasn't working was due to an apparently long-standing bug in nanops.nanminmax which I think we can fix here: #18588. Basically we are filling NA with infinite values when taking the min or max, but this doesn't make sense for object dtypes and an error gets raised even if skipna is True. If we fix that by explicitly masking the missing values instead, I believe we can just use this function directly in StringArray methods. |
||
if not na_mask.any(): | ||
return getattr(self, name)(skipna=False, **kwargs) | ||
elif skipna: | ||
return getattr(self[~na_mask], name)(skipna=False, **kwargs) | ||
else: | ||
return libmissing.NA | ||
else: | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError(f"Cannot perform reduction '{name}' with string dtype") | ||
|
||
def value_counts(self, dropna=False): | ||
from pandas import value_counts | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,13 @@ class BaseNoReduceTests(BaseReduceTests): | |
|
||
@pytest.mark.parametrize("skipna", [True, False]) | ||
def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna): | ||
if isinstance(data, pd.arrays.StringArray) and all_numeric_reductions in [ | ||
"min", | ||
"max", | ||
"sum", | ||
]: | ||
pytest.skip("These reductions are implemented") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you see if you can rather update this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By updating in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, actually looking at the base reduction tests now: they are not really written in a way that they will pass for strings. But so you can copy this test to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, so we can remove the special cases for |
||
|
||
op_name = all_numeric_reductions | ||
s = pd.Series(data) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is likely too invasive for 1.02, move to 1.1