Skip to content

ENH: Raise useful error when iterating a Window #20996

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 7 commits into from
May 12, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ Other API Changes
- Constructing a Series from a list of length 1 no longer broadcasts this list when a longer index is specified (:issue:`19714`, :issue:`20391`).
- :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`)
- 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`)
- Window types, such as Rolling and Expanding, raise `NotImplementedError` upon iteration. This will ideally be replaced by mimicking the (key, group) iteration of GroupBy (:issue:`11704`).
Copy link
Member

Choose a reason for hiding this comment

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

Use double backticks around literals like NotImplementedError. Could also simply by just saying "Rolling and Expanding types will now raise a ``NotImplementedError`` upon iteration (:issue:`11704`)"

You may be right on how that eventually gets implemented but I wouldn't mention it here because who knows what change it could be subject to


.. _whatsnew_0230.deprecations:

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def __unicode__(self):
return "{klass} [{attrs}]".format(klass=self._window_type,
attrs=','.join(attrs))

def __iter__(self):
url = 'https://github.com/pandas-dev/pandas/issues/11704'
raise NotImplementedError('See issue #11704 {url}'.format(url=url))

def _get_index(self, index=None):
"""
Return index as ndarrays
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ def win_types_special(request):
return request.param


# Issue 11704: Iteration over a Window

@pytest.fixture
Copy link
Member

Choose a reason for hiding this comment

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

The fixtures here are a little overkill. Just parametrize a "klass" of pd.Series and pd.DataFrame (both can be constructed from the values provided).

def series():
return pd.Series([1, 2, 3, 4])

@pytest.fixture
def frame():
return pd.DataFrame({'a': [1, 2, 3, 4], 'b': [10, 20, 30, 40]})

@pytest.mark.parametrize('which', [series(), frame()])
def test_rolling_iterator(which):
Copy link
Member

Choose a reason for hiding this comment

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

Move this to TestRolling and call it test_iterator_raises

with pytest.raises(NotImplementedError):
iter(which.rolling(2))

@pytest.mark.parametrize('which', [series(), frame()])
def test_expanding_iterator(which):
Copy link
Member

Choose a reason for hiding this comment

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

Move this to TestExpanding and call it test_iterator_raises

with pytest.raises(NotImplementedError):
iter(which.expanding())


class Base(object):

_nan_locs = np.arange(20, 40)
Expand Down