Skip to content

15819 rolling window on empty df #16431

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 8 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Bug Fixes

- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)


Conversion
^^^^^^^^^^

Expand Down Expand Up @@ -68,7 +69,7 @@ Plotting
Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^


- Bug creating datetime rolling window on empty DataFrame (:issue:`15819`)
Copy link
Contributor

Choose a reason for hiding this comment

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

on an empty DataFrame



Sparse
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ def validate(self):
super(Rolling, self).validate()

# we allow rolling on a datetimelike index
if (self.is_datetimelike and
if ((self.obj.empty or self.is_datetimelike) and
isinstance(self.window, (compat.string_types, DateOffset,
timedelta))):

Expand Down
60 changes: 60 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,34 @@ def test_closed(self):
with pytest.raises(ValueError):
df.rolling(window=3, closed='neither')

def test_empty_df_datetime_rolling_sum(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than making all separate tests, either use parametrize, or you can consolidate these into 2 tests for rolling (and 2 for expanding)

e.g.

@pytest.mark.parametrize('roller', ['1s', 1]):
def tests_empty_df_rolling(self, roller):
    expected = DataFrame()
    result = DataFrame().rolling(roller).sum()
    tm.assert_frame_equal(result, expected)

    expected = DataFrame(index=pd.DatetimeIndex([]))
    result = DataFrame(index=pd.DatetimeIndex([])).rolling(roller).sum()
    tm.assert_frame_equal(result, expected)

# Verifies that datetime rolling window can be applied to empty DataFrame
# GH 15819
expected = DataFrame()
result = DataFrame().rolling('1s').sum()
tm.assert_frame_equal(result, expected)

def test_empty_df_integer_rolling_sum(self):
# Verifies that integer rolling window can be applied to empty DataFrame
# GH 15819
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the same tests for expanding (not that the expanding will raise for the time-aware, a separate issue, so xfail that one, put a ref to #16425 there).

Copy link
Contributor

Choose a reason for hiding this comment

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

can you add these as well (which are known working)

DataFrame(index=pd.DatetimeIndex([])).rolling(1).sum()

DataFrame(index=pd.DatetimeIndex([])).rolling('1s').sum()

expected = DataFrame()
result = DataFrame().rolling(1).sum()
tm.assert_frame_equal(result, expected)

def test_empty_df_datetime_index_datetime_rolling_sum(self):
# Verifies that integer and datetime rolling windows can be applied to empty DataFrame with datetime index
# GH 15819
expected = DataFrame(index=pd.DatetimeIndex([]))
result = DataFrame(index=pd.DatetimeIndex([])).rolling('1s').sum()
tm.assert_frame_equal(result, expected)

def test_empty_df_datetime_index_integer_rolling_sum(self):
# Verifies that integer and datetime rolling windows can be applied to empty DataFrame with datetime index
# GH 15819
expected = DataFrame(index=pd.DatetimeIndex([]))
result = DataFrame(index=pd.DatetimeIndex([])).rolling(1).sum()
tm.assert_frame_equal(result, expected)


class TestExpanding(Base):

Expand Down Expand Up @@ -483,6 +511,38 @@ def test_numpy_compat(self):
tm.assert_raises_regex(UnsupportedFunctionCall, msg,
getattr(e, func), dtype=np.float64)

@pytest.mark.xfail(reason="Open issue GH 16425")
def test_empty_df_datetime_expanding_sum(self):
# Verifies that datetime expanding window can be applied to empty DataFrame
# GH 15819
# GH 16425
expected = DataFrame()
result = DataFrame().expanding('1s').sum()
tm.assert_frame_equal(result, expected)

def test_empty_df_integer_expanding_sum(self):
# Verifies that integer expanding window can be applied to empty DataFrame
# GH 15819
expected = DataFrame()
result = DataFrame().expanding(1).sum()
tm.assert_frame_equal(result, expected)

@pytest.mark.xfail(reason="Open issue GH 16425")
Copy link
Contributor

Choose a reason for hiding this comment

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

make a referece to what this issue is fixing as well (like I showed above), this is a printed warning when running the tests e.g.

pytest ..... -r x will show them

def test_empty_df_datetime_index_datetime_expanding_sum(self):
# Verifies that datetime expanding window can be applied to empty DataFrame with datetime index
# GH 15819
# GH 16425
expected = DataFrame(index=pd.DatetimeIndex([]))
result = DataFrame(index=pd.DatetimeIndex([])).expanding('1s').sum()
tm.assert_frame_equal(result, expected)

def test_empty_df_datetime_index_integer_expanding_sum(self):
# Verifies that integer expanding window can be applied to empty DataFrame with datetime index
# GH 15819
expected = DataFrame(index=pd.DatetimeIndex([]))
result = DataFrame(index=pd.DatetimeIndex([])).expanding(1).sum()
tm.assert_frame_equal(result, expected)


class TestEWM(Base):

Expand Down