Skip to content

Commit 45ef2bc

Browse files
seliktp
authored and
tp
committed
ENH: Raise useful error when iterating a Window (pandas-dev#20996)
* ENH: Raise useful error when iterating a Window Until Issue pandas-dev#11704 is completed, raise a NotImplementedError to provide a more clear error message when attempting to iterate over a Rolling or Expanding window.
1 parent 5e362d7 commit 45ef2bc

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ Other API Changes
967967
- Constructing a Series from a list of length 1 no longer broadcasts this list when a longer index is specified (:issue:`19714`, :issue:`20391`).
968968
- :func:`DataFrame.to_dict` with ``orient='index'`` no longer casts int columns to float for a DataFrame with only int and float columns (:issue:`18580`)
969969
- A user-defined-function that is passed to :func:`Series.rolling().aggregate() <pandas.core.window.Rolling.aggregate>`, :func:`DataFrame.rolling().aggregate() <pandas.core.window.Rolling.aggregate>`, or its expanding cousins, will now *always* be passed a ``Series``, rather than a ``np.array``; ``.apply()`` only has the ``raw`` keyword, see :ref:`here <whatsnew_0230.enhancements.window_raw>`. This is consistent with the signatures of ``.aggregate()`` across pandas (:issue:`20584`)
970+
- Rolling and Expanding types raise ``NotImplementedError`` upon iteration (:issue:`11704`).
970971

971972
.. _whatsnew_0230.deprecations:
972973

pandas/core/window.py

+4
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ def __unicode__(self):
181181
return "{klass} [{attrs}]".format(klass=self._window_type,
182182
attrs=','.join(attrs))
183183

184+
def __iter__(self):
185+
url = 'https://github.com/pandas-dev/pandas/issues/11704'
186+
raise NotImplementedError('See issue #11704 {url}'.format(url=url))
187+
184188
def _get_index(self, index=None):
185189
"""
186190
Return index as ndarrays

pandas/tests/test_window.py

+16
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,14 @@ def test_multi_index_names(self):
512512
tm.assert_index_equal(result.columns, df.columns)
513513
assert result.index.names == [None, '1', '2']
514514

515+
@pytest.mark.parametrize('klass', [pd.Series, pd.DataFrame])
516+
def test_iter_raises(self, klass):
517+
# https://github.com/pandas-dev/pandas/issues/11704
518+
# Iteration over a Window
519+
obj = klass([1, 2, 3, 4])
520+
with pytest.raises(NotImplementedError):
521+
iter(obj.rolling(2))
522+
515523

516524
class TestExpanding(Base):
517525

@@ -590,6 +598,14 @@ def test_missing_minp_zero(self):
590598
expected = pd.Series([np.nan])
591599
tm.assert_series_equal(result, expected)
592600

601+
@pytest.mark.parametrize('klass', [pd.Series, pd.DataFrame])
602+
def test_iter_raises(self, klass):
603+
# https://github.com/pandas-dev/pandas/issues/11704
604+
# Iteration over a Window
605+
obj = klass([1, 2, 3, 4])
606+
with pytest.raises(NotImplementedError):
607+
iter(obj.expanding(2))
608+
593609

594610
class TestEWM(Base):
595611

0 commit comments

Comments
 (0)