Skip to content

Commit 0bde7ce

Browse files
MarcoGorellimroeschke
authored andcommitted
BUG: Make sure correct values are passed to Rolling._on when axis=1 (pandas-dev#28267)
* Make sure correct values are passed to Rolling._on when axis=1 * Update rolling.py * Capitalise 'd' as in documentation * Parametrize over tz_naive_fixture * autoformat
1 parent 4252ab7 commit 0bde7ce

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Groupby/resample/rolling
177177
^^^^^^^^^^^^^^^^^^^^^^^^
178178

179179
-
180-
-
180+
- Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`)
181181
- Bug in :meth:`DataFrame.groupby` not offering selection by column name when ``axis=1`` (:issue:`27614`)
182182
- Bug in :meth:`DataFrameGroupby.agg` not able to use lambda function with named aggregation (:issue:`27519`)
183183

pandas/core/window/rolling.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,10 @@ def is_datetimelike(self):
16531653
def _on(self):
16541654

16551655
if self.on is None:
1656-
return self.obj.index
1656+
if self.axis == 0:
1657+
return self.obj.index
1658+
elif self.axis == 1:
1659+
return self.obj.columns
16571660
elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns:
16581661
return Index(self.obj[self.on])
16591662
else:

pandas/tests/window/test_rolling.py

+27
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,30 @@ def test_readonly_array(self):
334334
result = pd.Series(arr).rolling(2).mean()
335335
expected = pd.Series([np.nan, 2, np.nan, np.nan, 4])
336336
tm.assert_series_equal(result, expected)
337+
338+
def test_rolling_datetime(self, axis_frame, tz_naive_fixture):
339+
# GH-28192
340+
tz = tz_naive_fixture
341+
df = pd.DataFrame(
342+
{
343+
i: [1] * 2
344+
for i in pd.date_range("2019-8-01", "2019-08-03", freq="D", tz=tz)
345+
}
346+
)
347+
if axis_frame in [0, "index"]:
348+
result = df.T.rolling("2D", axis=axis_frame).sum().T
349+
else:
350+
result = df.rolling("2D", axis=axis_frame).sum()
351+
expected = pd.DataFrame(
352+
{
353+
**{
354+
i: [1.0] * 2
355+
for i in pd.date_range("2019-8-01", periods=1, freq="D", tz=tz)
356+
},
357+
**{
358+
i: [2.0] * 2
359+
for i in pd.date_range("2019-8-02", "2019-8-03", freq="D", tz=tz)
360+
},
361+
}
362+
)
363+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)