-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Implement StringArray.min / max #33351
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 2 commits
a470f02
a50a2c4
4e09c50
6e8f0c5
34f8d5f
08ce4d5
fa13cec
64e6d19
d804e4b
5cc91e0
0c98b08
df6ba29
3613f65
fd62963
9083273
208835c
77144d0
9197125
3fcd200
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
from pandas import compat | ||
from pandas.core import ops | ||
from pandas.core.array_algos import masked_reductions | ||
from pandas.core.arrays import IntegerArray, PandasArray | ||
from pandas.core.arrays.integer import _IntegerDtype | ||
from pandas.core.construction import extract_array | ||
|
@@ -282,8 +283,23 @@ def astype(self, dtype, copy=True): | |
return super().astype(dtype, copy) | ||
|
||
def _reduce(self, name, skipna=True, **kwargs): | ||
if name in ["min", "max"]: | ||
return getattr(self, name)(skipna=skipna, **kwargs) | ||
|
||
raise TypeError(f"Cannot perform reduction '{name}' with string dtype") | ||
|
||
def min(self, axis=None, out=None, keepdims=False, skipna=True): | ||
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. Is it possible to move all of axis, out and keepdims into This should probably also validate those keywords, if we want to accept them (and we should also test this if we are adding them) 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. Updated, assuming by test with numpy you mean the validation functions in /compat/numpy/function? 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, indeed, those validation functions can be used to check additional keywords. In addition, we should also test this in the tests (so test that np.min(a) works, because that is the only reason we would add those keywords) |
||
result = masked_reductions.min( | ||
values=self.to_numpy(na_value=np.nan), mask=self.isna(), skipna=skipna | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
return result | ||
|
||
def max(self, axis=None, out=None, keepdims=False, skipna=True): | ||
result = masked_reductions.max( | ||
values=self.to_numpy(na_value=np.nan), mask=self.isna(), skipna=skipna | ||
) | ||
return result | ||
|
||
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 [ | ||
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, @TomAugspurger, @jbrockmendel is this the pattern we are using here for skips like this? 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. in indexes tests when we have a "this is tested in this other place" comment we usually return/pass instead of pytest.skip. It's not a perfect system, but I think of it as a way of distinguishing between "this test is skipped but would be nice to enable" vs "nothing to see here" 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. Returning None rather than skipping also keeps the test output cleaner, since we print skipped tests. 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. We typically refine this for a specific type by overriding this test to have custom behaviour 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. ok on the test names? @jorisvandenbossche 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. @dsaxton can you move this special case for string to |
||
"min", | ||
"max", | ||
]: | ||
# Tested in pandas/tests/arrays/string_/test_string.py | ||
pytest.skip("These reductions are implemented") | ||
|
||
op_name = all_numeric_reductions | ||
s = pd.Series(data) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.