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
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
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 @@ -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 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:
jreback marked this conversation as resolved.
Show resolved Hide resolved
result = sum_x / <float64_t>nobs
if neg_ct == 0 and result < 0:
# all positive
Expand Down
46 changes: 46 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,3 +1357,49 @@ 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)


@pytest.mark.parametrize(
"start, exp_values",
[
(1, [0.03, 0.0155, 0.0155, 0.011, 0.01025]),
(2, [0.001, 0.001, 0.0015, 0.00366666]),
],
)
def test_rolling_mean_all_nan_window_floating_artifacts(start, exp_values):
# 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,
]
)

values = exp_values + [
0.00366666,
0.005,
0.005,
0.008,
np.NaN,
np.NaN,
0.005,
0.102500,
]
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 :)

values,
index=list(range(start, len(values) + start)),
)
result = df.iloc[start:].rolling(5, min_periods=0).mean()
tm.assert_frame_equal(result, expected)