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

BUG: rolling mean and sum not numerical stable for all nan window #41106

Merged
merged 7 commits into from
Apr 26, 2021

Conversation

phofl
Copy link
Member

@phofl phofl commented Apr 22, 2021

The problem is not numerical stable, so we are dividing -3e-18 / 0, which returns -inf running into line 177 setting to 0. Checking for nobs > 0 would solve these cases

cc @mroeschke

@phofl phofl added the Window rolling, ewma, expanding label Apr 22, 2021
]
)

expected = DataFrame(
Copy link
Member

Choose a reason for hiding this comment

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

Mind parameterizing over expected and the slice?

Copy link
Member Author

Choose a reason for hiding this comment

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

Could yes, but you make a pretty long parametrization, because most of exepected differs too

Copy link
Member Author

Choose a reason for hiding this comment

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

Parametrized, got a bit better :)

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM the CI failure look unrelated.

Can open up another issue if you have time to investigate if we need the nobs > 0 check elsewhere.

@mroeschke mroeschke added this to the 1.3 milestone Apr 24, 2021
@phofl
Copy link
Member Author

phofl commented Apr 24, 2021

cc @mroeschke
I've added the sum fix mentioned in the issue, the test returns

              0
0  2.000000e-03
1  1.000000e-02
2  1.500000e-02
3  1.300000e-02
4  5.000000e-03
5 -8.673617e-19

on master. Sum should be the only other case we have the problem. As long as all nan should return 0 we can do it this way

@phofl phofl changed the title BUG: rolling returning mean 0 for all nan window BUG: rolling mean and sum not numerical stable for all nan window Apr 24, 2021
Copy link
Member

@mroeschke mroeschke 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 checking. Just one comment about the whatsnew otherwise LGTM

@@ -846,6 +846,7 @@ Groupby/resample/rolling
- Bug in :meth:`GroupBy.cummin` and :meth:`GroupBy.cummax` incorrectly rounding integer values near the ``int64`` implementations bounds (:issue:`40767`)
- Bug in :meth:`.GroupBy.rank` with nullable dtypes incorrectly raising ``TypeError`` (:issue:`41010`)
- Bug in :meth:`.GroupBy.cummin` and :meth:`.GroupBy.cummax` computing wrong result with nullable data types too large to roundtrip when casting to float (:issue:`37493`)
- Bug in :meth:`DataFrame.rolling` returning mean zero, respectively sum not zero for all ``NaN`` window with ``min_periods=0`` if calculation is not numerical stable (:issue:`41053`)
Copy link
Member

Choose a reason for hiding this comment

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

Could you add 2 separate lines for the mean bug and sum bug for clarity?

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

@jreback jreback merged commit c3451ac into pandas-dev:master Apr 26, 2021
@jreback
Copy link
Contributor

jreback commented Apr 26, 2021

thanks @phofl

@phofl phofl deleted the 41053 branch April 26, 2021 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Inconsistent handling of empty windows for rolling mean with min_periods=0
3 participants