Skip to content

BUG: Make sure correct values are passed to Rolling._on when axis=1 #28267

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 6 commits into from
Sep 4, 2019
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^

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

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,10 @@ def is_datetimelike(self):
def _on(self):

if self.on is None:
return self.obj.index
if self.axis == 0:
return self.obj.index
elif self.axis == 1:
return self.obj.transpose().index
Copy link
Member

Choose a reason for hiding this comment

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

I think we can just call self.obj.columns instead of having to transpose.

elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns:
return Index(self.obj[self.on])
else:
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,26 @@ def test_readonly_array(self):
result = pd.Series(arr).rolling(2).mean()
expected = pd.Series([np.nan, 2, np.nan, np.nan, 4])
tm.assert_series_equal(result, expected)

def test_rolling_datetime(self, axis_frame):
Copy link
Member

Choose a reason for hiding this comment

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

One last thing: Could you parameterize over the tz_naive_fixture?

# GH-28192
df = pd.DataFrame(
{i: [1] * 2 for i in pd.date_range("2019-8-01", "2019-08-03", freq="D")}
)
if axis_frame in [0, "index"]:
result = df.T.rolling("2d", axis=axis_frame).sum().T
else:
result = df.rolling("2d", axis=axis_frame).sum()
expected = pd.DataFrame(
{
**{
i: [1.0] * 2
for i in pd.date_range("2019-8-01", periods=1, freq="D")
},
**{
i: [2.0] * 2
for i in pd.date_range("2019-8-02", "2019-8-03", freq="D")
},
}
)
tm.assert_frame_equal(result, expected)