Skip to content

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

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 7 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BUG: rolling returning mean 0 for all nan window
  • Loading branch information
phofl committed Apr 22, 2021
commit 712876977b7ac2b625880caddeec0689b87f7bac
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ Groupby/resample/rolling
- Bug in :class:`core.window.RollingGroupby` where ``as_index=False`` argument in ``groupby`` was ignored (:issue:`39433`)
- Bug in :meth:`.GroupBy.any` and :meth:`.GroupBy.all` raising ``ValueError`` when using with nullable type columns holding ``NA`` even with ``skipna=True`` (:issue:`40585`)
- Bug in :meth:`GroupBy.cummin` and :meth:`GroupBy.cummax` incorrectly rounding integer values near the ``int64`` implementations bounds (:issue:`40767`)
- Bug in :meth:`DataFrame.rolling` returning mean zero for all ``NaN`` window with ``min_periods=0`` if calculation is not numerical stable (:issue:`41053`)

Reshaping
^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/window/aggregations.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ cdef inline float64_t calc_mean(int64_t minp, Py_ssize_t nobs,
cdef:
float64_t result

if nobs >= minp:
if nobs >= minp and nobs > 0:
result = sum_x / <float64_t>nobs
if neg_ct == 0 and result < 0:
# all positive
Expand Down
64 changes: 64 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,3 +1357,67 @@ def test_rolling_std_small_values():
result = s.rolling(2).std()
expected = Series([np.nan, 7.071068e-9, 7.071068e-9])
tm.assert_series_equal(result, expected, atol=1.0e-15, rtol=1.0e-15)


def test_rolling_mean_all_nan_window_floating_artifacts():
# GH#41053
df = DataFrame(
[
0.03,
0.03,
0.001,
np.NaN,
0.002,
0.008,
np.NaN,
np.NaN,
np.NaN,
np.NaN,
np.NaN,
np.NaN,
0.005,
0.2,
]
)

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

[
0.03,
0.0155,
0.0155,
0.011,
0.01025,
0.00366666,
0.005,
0.005,
0.008,
np.NaN,
np.NaN,
0.005,
0.102500,
],
index=list(range(1, 14)),
)
result = (
df.iloc[1:].rolling(5, min_periods=0).mean()
) # Returns 0 at indexes 10 and 11
tm.assert_frame_equal(result, expected)
expected = DataFrame(
[
0.001,
0.001,
0.0015,
0.00366666,
0.00366666,
0.005,
0.005,
0.008,
np.NaN,
np.NaN,
0.005,
0.102500,
],
index=list(range(2, 14)),
)
result = df.iloc[2:].rolling(5, min_periods=0).mean()
tm.assert_frame_equal(result, expected)