Skip to content

ENH: Add cumulative methods to ea #48111

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 129 commits into from
Dec 13, 2022

Conversation

phofl
Copy link
Member

@phofl phofl commented Aug 16, 2022

This tries to finish #28509. I've reduced the scope to masked arrays and would prefer to do datetimelike as a follow up, if we want to change the behavior there. This is currently a big buggy as mentioned in #28509 (comment)
Currently, we are dispatching back to the current implementation.

I was wondering about the exact interface. Currently, you'd have to call _accumulate with the name of the function, since cumsum etc. are not really added to the interface, meaning

arr = pd.array([1, 2, pd.NA], dtype="Int64")
arr.cumsum()

raises, because the method is not registered. I this intended?

datajanko and others added 30 commits September 13, 2019 20:19
data = self._data.copy()

if name in {"cummin", "cummax"}:
func = np.minimum.accumulate if name == "cummin" else np.maximum.accumulate
Copy link
Member

Choose a reason for hiding this comment

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

do the numpy functions not work directly?

Copy link
Member Author

Choose a reason for hiding this comment

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

This has a different behavior than going through nanops. The initial pr included this but I ripped it out to keep it a bit more focused. Plan to tackle this afterwards, but we have to decide what we actually want here first. Hence only doing masked ops here

Copy link
Member

Choose a reason for hiding this comment

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

This has a different behavior than going through nanops

can you expand on this?

Copy link
Member Author

Choose a reason for hiding this comment

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

skipna is the problem, difference in behavior with regards to floats, remembered this incorrect.

See #28509 (comment)

Wanted to investigate this as a follow up when we can check this more isolated

@phofl
Copy link
Member Author

phofl commented Nov 29, 2022

Should have addressed everything


if name in {"cummin", "cummax"}:
func = np.minimum.accumulate if name == "cummin" else np.maximum.accumulate
result = cast(np.ndarray, nanops.na_accum_func(data, func, skipna=skipna))
Copy link
Member

Choose a reason for hiding this comment

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

this looks like it might choke on PeriodDtype?

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems to work, could you elaborate what you suspect?

ser = Series([pd.Period('2012-1-1', freq='D'), pd.Period('2013-1-1', freq='D')])
ser.cummin()

Copy link
Member

Choose a reason for hiding this comment

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

i think this goes wrong when there's a NaT present

Copy link
Member Author

Choose a reason for hiding this comment

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

This would restore the previous behavior (but the previous behavior was wrong as well...). Are you ok with addressing this in a follow up?

Copy link
Member

Choose a reason for hiding this comment

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

This would restore the previous behavior

previous as of when? IIRC this was last changed multiple years ago.

but the previous behavior was wrong as well

so does this PR get the behavior right for dt64/td64? If so, the solution for PeriodDtype is similar to median/min/max to do a view to dt64, do the op, then view back.

Copy link
Member Author

@phofl phofl Dec 7, 2022

Choose a reason for hiding this comment

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

Sorry, my explanation was a bit confusing.

The initial pr which this work is based on tried the following:

  • add accumulate to the ea interface
  • implement masked-based accumulators
  • implement new accumulation logic for date time and related arrays

This got confusing, since it was pretty big and the new date time logic changed the behavior. So I made the decision to reduce scope and only tackle the first 2 points and defer the third to a follow up.

To achieve this, I just send the date time logic through nanops again (this is what I meant with previous behavior, previous as in before _accumulate was added to the ea interface). This avoid any change in behavior for the date time was and should keep review more focused.

Copy link
Member

Choose a reason for hiding this comment

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

Let's try this one more time, thanks for your patience in explaining this to me.

IIUC this PR should not change any behavior for dt64/dt64tz/td64 dtypes, correct?

Copy link
Member Author

@phofl phofl Dec 12, 2022

Choose a reason for hiding this comment

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

Yes, correct. Want to do those changes as follow ups

Copy link
Member

Choose a reason for hiding this comment

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

Cool, ill trust you to handle these.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx, yep want to get this into 2.0 as well

self.assert_series_equal(result, expected, check_dtype=False)


class BaseNoAccumulateTests(BaseAccumulateTests):
Copy link
Member

Choose a reason for hiding this comment

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

i find this pattern really weird (xref #44742), is there a way to do this with just one class?

Copy link
Member Author

@phofl phofl Dec 12, 2022

Choose a reason for hiding this comment

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

Combined the classes. Have to overwrite the tests that should not get executed now.

Copy link
Member

Choose a reason for hiding this comment

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

thanks

Copy link
Member

@jbrockmendel jbrockmendel left a comment

Choose a reason for hiding this comment

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

LGTM pending green

@mroeschke mroeschke merged commit b5953aa into pandas-dev:main Dec 13, 2022
@mroeschke
Copy link
Member

Thanks @phofl

@phofl phofl deleted the 28385-add-cumulative-methods-to-EA branch December 13, 2022 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NA - MaskedArrays Related to pd.NA and nullable extension arrays Reduction Operations sum, mean, min, max, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nullable Int64 column changes type after some (cumsum) operations
4 participants