Skip to content

Commit e41fe7f

Browse files
chernrickjreback
authored andcommitted
15819 rolling window on empty df (#16431)
1 parent 348afeb commit e41fe7f

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

doc/source/whatsnew/v0.20.2.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Bug Fixes
3939
- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
4040
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
4141

42+
4243
Conversion
4344
^^^^^^^^^^
4445

@@ -73,7 +74,7 @@ Plotting
7374
Groupby/Resample/Rolling
7475
^^^^^^^^^^^^^^^^^^^^^^^^
7576

76-
77+
- Bug creating datetime rolling window on an empty DataFrame (:issue:`15819`)
7778

7879

7980
Sparse

pandas/core/window.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ def validate(self):
10741074
super(Rolling, self).validate()
10751075

10761076
# we allow rolling on a datetimelike index
1077-
if (self.is_datetimelike and
1077+
if ((self.obj.empty or self.is_datetimelike) and
10781078
isinstance(self.window, (compat.string_types, DateOffset,
10791079
timedelta))):
10801080

pandas/tests/test_window.py

+32
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,20 @@ def test_closed(self):
441441
with pytest.raises(ValueError):
442442
df.rolling(window=3, closed='neither')
443443

444+
@pytest.mark.parametrize('roller', ['1s', 1])
445+
def tests_empty_df_rolling(self, roller):
446+
# GH 15819 Verifies that datetime and integer rolling windows can be
447+
# applied to empty DataFrames
448+
expected = DataFrame()
449+
result = DataFrame().rolling(roller).sum()
450+
tm.assert_frame_equal(result, expected)
451+
452+
# Verifies that datetime and integer rolling windows can be applied to
453+
# empty DataFrames with datetime index
454+
expected = DataFrame(index=pd.DatetimeIndex([]))
455+
result = DataFrame(index=pd.DatetimeIndex([])).rolling(roller).sum()
456+
tm.assert_frame_equal(result, expected)
457+
444458

445459
class TestExpanding(Base):
446460

@@ -483,6 +497,24 @@ def test_numpy_compat(self):
483497
tm.assert_raises_regex(UnsupportedFunctionCall, msg,
484498
getattr(e, func), dtype=np.float64)
485499

500+
@pytest.mark.parametrize(
501+
'expander',
502+
[1, pytest.mark.xfail(
503+
reason='GH 16425 expanding with offset not supported')('1s')])
504+
def tests_empty_df_expanding(self, expander):
505+
# GH 15819 Verifies that datetime and integer expanding windows can be
506+
# applied to empty DataFrames
507+
expected = DataFrame()
508+
result = DataFrame().expanding(expander).sum()
509+
tm.assert_frame_equal(result, expected)
510+
511+
# Verifies that datetime and integer expanding windows can be applied
512+
# to empty DataFrames with datetime index
513+
expected = DataFrame(index=pd.DatetimeIndex([]))
514+
result = DataFrame(
515+
index=pd.DatetimeIndex([])).expanding(expander).sum()
516+
tm.assert_frame_equal(result, expected)
517+
486518

487519
class TestEWM(Base):
488520

0 commit comments

Comments
 (0)