-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
ENH: Implement StringArray.min / max #33351
Conversation
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.
Thanks for looking at this!
pandas/core/arrays/string_.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to move all of axis, out and keepdims into **kwargs
? (will need to test with numpy)
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 comment
The 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 comment
The 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)
@jorisvandenbossche What do you think of implementing these reductions at the PandasArray or BaseMaskedArray level (I figure they should probably be used at least for IntegerArray anyways, but if so then might as well make them inheritable by other masked arrays, e.g., BooleanArray)? |
Yeah, we should indeed decide on this more generally (for all our internal EAs, so string/boolean/int, I think the older ones already have those methods). And also more broadly for all/most reductions (so also mean, sum, etc). But that can be discussed / done separately from this PR though. Eg one question is if we actually add this to the EA interface (so base class EA), or only to our own EAs. |
@@ -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 comment
The 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 comment
The 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 comment
The 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 comment
The 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 extension/test_strings.py
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.
ok on the test names? @jorisvandenbossche
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.
@dsaxton can you move this special case for string to test_strings.py
? (and there you an override this test method, to do the correct thing for string dtype)
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.
looks pretty good.
After annotating the StringArray methods mypy complains about a mismatch between the signatures with PandasArray. I think we could just replace the parent class implementations in this PR and that should still work / be a bit cleaner? (I'll make that update for now and can revert if there are drawbacks.) |
i'm seeing a regression here...
on master
|
I think this is caused by validating all the kwargs, any idea what the best approach is here @jorisvandenbossche ? Looks like before only out and keepdims were validated |
Yeah, that's because the validation functions is not handling |
|
||
@pytest.mark.parametrize("method", ["min", "max"]) | ||
def test_min_max_numpy(method): | ||
arr = pd.Series(["a", "b", "c", None], dtype="string") |
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.
so we should also test here for the actual array (and not put in a Series), maybe parametrize over pd.array and pd.Series as constructor
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.
lgtm. @jorisvandenbossche
Thanks @dsaxton ! |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
Using the new masked reductions from @jorisvandenbossche to implement these for StringArray. Part of #31746 but doesn't close because we're not adding sum here.