Skip to content
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: Support mask in groupby cumprod #48138

Merged
merged 13 commits into from
Sep 19, 2022

Conversation

phofl
Copy link
Member

@phofl phofl commented Aug 18, 2022

This is a general issue here. If we overflow int64 we get garbage. Previously we were working with float64, which gave us back numbers, but they were incorrect. But we keep precision as long as our numbers fit into int64, which was not the case previously, since we were casting to float64 beforehand, imo this is more important.

cc @jorisvandenbossche

@phofl phofl added Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays labels Aug 18, 2022
@jorisvandenbossche
Copy link
Member

This is a general issue here. If we overflow int64 we get garbage. Previously we were working with float64, which gave us back numbers, but they were incorrect. But we keep precision as long as our numbers fit into int64, which was not the case previously, since we were casting to float64 beforehand, imo this is more important.

Comparing to the plain (non-grouped) sum/prod, those currently also overflow:

In [35]: pd.Series([int(1e16)]*100).sum()
Out[35]: 1000000000000000000

In [36]: pd.Series([int(1e16)]*1000).sum()
Out[36]: -8446744073709551616

In [40]: pd.Series([2]*62).prod()
Out[40]: 4611686018427387904

In [41]: pd.Series([2]*63).prod()
Out[41]: -9223372036854775808

So it seems sensible that the groupby variants follow this as well. In general, we should maybe better document those constraints and expectations around overflow (not sure if this is now documented somewhere?)

@@ -100,6 +100,7 @@ Deprecations

Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Performance improvement in :meth:`.GroupBy.cumprod` for extension array dtypes (:issue:`37493`)
Copy link
Member

Choose a reason for hiding this comment

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

This also now uses int64 instead of float64 for the numpy dtypes? So that also changes behaviour in those cases regarding overflow?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, should we mention this in the whatsnew?

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 so, yes. Maybe as notable bug fix, as it has some behaviour change?

Copy link
Member Author

Choose a reason for hiding this comment

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

image

@@ -641,10 +641,10 @@ def test_groupby_cumprod():
tm.assert_series_equal(actual, expected)

df = DataFrame({"key": ["b"] * 100, "value": 2})
df["value"] = df["value"].astype(float)
Copy link
Member

Choose a reason for hiding this comment

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

We can maybe keep this with as int (or test both in addition), so we have a test for the silent overflow behaviour?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a new test explicitly testing that overflow is consistent with numpy

@@ -641,10 +641,10 @@ def test_groupby_cumprod():
tm.assert_series_equal(actual, expected)

df = DataFrame({"key": ["b"] * 100, "value": 2})
df["value"] = df["value"].astype(float)
actual = df.groupby("key")["value"].cumprod()
# if overflows, groupby product casts to float
# while numpy passes back invalid values
Copy link
Member

Choose a reason for hiding this comment

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

This comment can probably be updated

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@phofl
Copy link
Member Author

phofl commented Sep 12, 2022

So this is the last one of the groupby algos. We can start refactoring the groupby ops code paths after this is through


In previous versions we cast to float when applying ``cumsum`` and ``cumprod`` which
lead to incorrect results even if the result could be hold by ``int64`` dtype.
Additionally, the aggregation overflows consistent with numpy when the limit of
Copy link
Member

Choose a reason for hiding this comment

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

I would maybe mention that it is making it consistent with the DataFrame method as well? (without groupby)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a reference to the methods

@jorisvandenbossche
Copy link
Member

I am still a bit uneasy about this change, since it is silently changing actual results that you get (a previously somewhat correct results (an inexact float) could silently become completely incorrect (overflowed int)).
So it would be good to get some input from others.

To what extent would it be possible to split the overflow behaviour change from the mask introduction, so we could for example leave that behaviour change for 2.0? (not sure myself whether this is worth it, just wondering)

@phofl
Copy link
Member Author

phofl commented Sep 12, 2022

It is a bit unfortunate, this is true. But we can preserve precision now if possible, this was buggy before and since the behaviour is aligned with numpy and the regular DataFrame behaviour this should be ok imo. In the end it probably does not matter how far off your values are if they are off.

We could cast to float before calling the algos, this would keep the current behaviour but would lose performance gains and the precision fixes (would also hit cumsum that is already merged).

Since we intend to do 2.0 as the next release anyways, would it be ok to merge this and revert to casting to float before passing the array to the cython algos, If we do an unexpected 1.6 next?

@jorisvandenbossche
Copy link
Member

Since we intend to do 2.0 as the next release anyways, would it be ok to merge this and revert to casting to float before passing the array to the cython algos, If we do an unexpected 1.6 next?

Sounds good!

@phofl
Copy link
Member Author

phofl commented Sep 14, 2022

Great! Thanks.

@mroeschke Would you mind having a look before merging?

pandas/_libs/groupby.pyx Outdated Show resolved Hide resolved
@mroeschke mroeschke added this to the 1.6 milestone Sep 19, 2022
@mroeschke mroeschke merged commit f19aeaf into pandas-dev:main Sep 19, 2022
@mroeschke
Copy link
Member

Thanks @phofl

@phofl phofl deleted the groupby_cumprod_mask branch September 20, 2022 08:22
@mroeschke mroeschke modified the milestones: 1.6, 2.0 Oct 13, 2022
noatamir pushed a commit to noatamir/pandas that referenced this pull request Nov 9, 2022
* ENH: Support mask in groupby cumprod

* Add whatsnew

* Move whatsnew

* Adress review

* Fix example

* Clarify

* Change dtype access
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: support masked arrays in groupby cython algos
3 participants