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 fix where df.rolling doesn't work with certain datetime64 index t… #55325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ Performance improvements
Bug fixes
~~~~~~~~~
- Bug in :class:`AbstractHolidayCalendar` where timezone data was not propagated when computing holiday observances (:issue:`54580`)
- Bug in :class:`pandas.core.window.Rolling` where df.rolling does not work with 'datetime64[us]', 'datetime64[ms]', and 'datetime64[s]' index types (:issue:`55299`)
- Bug in :class:`pandas.core.window.Rolling` where duplicate datetimelike indexes are treated as consecutive rather than equal with ``closed='left'`` and ``closed='neither'`` (:issue:`20712`)
- Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55009`)
- Bug in :meth:`pandas.read_excel` with a ODS file without cached formatted cell for float values (:issue:`55219`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ def _index_array(self):
# TODO: why do we get here with e.g. MultiIndex?
if needs_i8_conversion(self._on.dtype):
idx = cast("PeriodIndex | DatetimeIndex | TimedeltaIndex", self._on)
if (type(idx) == DatetimeIndex) and (idx.T.unit != "ns"):
idx = idx.astype("datetime64[ns]")
return idx.asi8
return None

Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1950,3 +1950,30 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype):
op2 = getattr(rolling2, kernel)
expected = op2(*arg2, numeric_only=numeric_only)
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
"data_type",
[
"datetime64[us]",
"datetime64[ms]",
"datetime64[s]",
],
)
def test_rolling_sum_on_dtypes(data_type):
# GH 55299
index = [
"2019-01-03 05:11",
"2019-01-03 05:23",
"2019-01-03 05:28",
"2019-01-03 05:32",
"2019-01-03 05:36",
]
arr = [True, False, False, True, True]

df_exp = DataFrame({"arr": arr}, index=to_datetime(index).astype("datetime64[ns]"))
df_test = DataFrame({"arr": arr}, index=to_datetime(index).astype(data_type))
sum_df_exp = df_exp.rolling("5min").sum()
sum_df_test = df_test.rolling("5min").sum()

assert list(sum_df_test.values) == list(sum_df_exp.values)