Skip to content

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

Merged
merged 19 commits into from
Apr 25, 2020
Merged

ENH: Implement StringArray.min / max #33351

merged 19 commits into from
Apr 25, 2020

Conversation

dsaxton
Copy link
Member

@dsaxton dsaxton commented Apr 7, 2020

  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

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.

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a 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!

raise TypeError(f"Cannot perform reduction '{name}' with string dtype")

def min(self, axis=None, out=None, keepdims=False, skipna=True):
Copy link
Member

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)

Copy link
Member Author

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?

Copy link
Member

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)

@dsaxton
Copy link
Member Author

dsaxton commented Apr 7, 2020

@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)?

@jorisvandenbossche
Copy link
Member

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.
Do you want to open an issue for this?

@@ -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 [
Copy link
Contributor

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?

Copy link
Member

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"

Copy link
Contributor

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.

Copy link
Member

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

Copy link
Contributor

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

Copy link
Member

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)

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty good.

@jreback jreback added Enhancement ExtensionArray Extending pandas with custom dtypes or arrays. Strings String extension data type and string data labels Apr 8, 2020
@jreback jreback added this to the 1.1 milestone Apr 8, 2020
@dsaxton
Copy link
Member Author

dsaxton commented Apr 8, 2020

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.

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.)

@simonjayhawkins
Copy link
Member

i'm seeing a regression here...

>>> import numpy as np
>>> import pandas as pd
>>>
>>> pd.__version__
'1.1.0.dev0+1273.gfd6296309'
>>>
>>> arr = pd.array([np.inf, 100])
>>>
>>> np.max(arr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in amax
  File "C:\Users\simon\Anaconda3\envs\pandas-dev\lib\site-packages\numpy\core\fromnumeric.py", line 2667, in amax
    return _wrapreduction(a, np.maximum, 'max', axis, None, out,
  File "C:\Users\simon\Anaconda3\envs\pandas-dev\lib\site-packages\numpy\core\fromnumeric.py", line 88, in _wrapreduction
    return reduction(axis=axis, out=out, **passkwargs)
  File "C:\Users\simon\pandas\pandas\core\arrays\numpy_.py", line 362, in max
    nv.validate_max((), kwargs)
  File "C:\Users\simon\pandas\pandas\compat\numpy\function.py", line 70, in __call__
    validate_args_and_kwargs(
  File "C:\Users\simon\pandas\pandas\util\_validators.py", line 205, in validate_args_and_kwargs
    validate_kwargs(fname, kwargs, compat_args)
  File "C:\Users\simon\pandas\pandas\util\_validators.py", line 148, in validate_kwargs
    _check_for_invalid_keys(fname, kwargs, compat_args)
  File "C:\Users\simon\pandas\pandas\util\_validators.py", line 122, in _check_for_invalid_keys
    raise TypeError(f"{fname}() got an unexpected keyword argument '{bad_arg}'")
TypeError: max() got an unexpected keyword argument 'axis'
>>>

on master

>>> import numpy as np
>>> import pandas as pd
>>>
>>> pd.__version__
'1.1.0.dev0+1347.g428791c5e'
>>>
>>> arr = pd.array([np.inf, 100])
>>>
>>> np.max(arr)
inf
>>>

@dsaxton
Copy link
Member Author

dsaxton commented Apr 24, 2020

i'm seeing a regression here...

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

@jorisvandenbossche
Copy link
Member

Yeah, that's because the validation functions is not handling axis properly


@pytest.mark.parametrize("method", ["min", "max"])
def test_min_max_numpy(method):
arr = pd.Series(["a", "b", "c", None], dtype="string")
Copy link
Member

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

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche jorisvandenbossche merged commit f49269f into pandas-dev:master Apr 25, 2020
@jorisvandenbossche
Copy link
Member

Thanks @dsaxton !

@dsaxton dsaxton deleted the string-min-max branch April 25, 2020 13:31
rhshadrach pushed a commit to rhshadrach/pandas that referenced this pull request May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ExtensionArray Extending pandas with custom dtypes or arrays. Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants