Skip to content

Resampler.__iter__() not working #22365

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 1 commit into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions doc/source/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ This is mainly syntactic sugar for the alternative and much more verbose:
Additionally this method avoids recomputing the internal grouping information
derived from the passed key.

.. _groupby.iterating:
.. _groupby.iterating-label:

Iterating through groups
------------------------
Expand All @@ -415,8 +415,7 @@ In the case of grouping by multiple keys, the group name will be a tuple:
...: print(group)
...:

It's standard Python-fu but remember you can unpack the tuple in the for loop
statement if you wish: ``for (k1, k2), group in grouped:``.
See :ref:`timeseries.iterating-label`.

Selecting a group
-----------------
Expand Down
18 changes: 18 additions & 0 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,24 @@ regularity will result in a ``DatetimeIndex``, although frequency is lost:
ts2[[0, 2, 6]].index
.. _timeseries.iterating-label:

Iterating through groups
------------------------

With the :ref:`Resampler` object in hand, iterating through the grouped data is very
natural and functions similarly to :py:func:`itertools.groupby`:

.. ipython:: python
resampled = df.resample('H')
for name, group in resampled:
print(name)
print(group)
See :ref:`groupby.iterating-label`.

.. _timeseries.components:

Time/Date Components
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ Other Enhancements
- :func:`to_timedelta` now supports iso-formated timedelta strings (:issue:`21877`)
- :class:`Series` and :class:`DataFrame` now support :class:`Iterable` in constructor (:issue:`2193`)
- :class:`DatetimeIndex` gained :attr:`DatetimeIndex.timetz` attribute. Returns local time with timezone information. (:issue:`21358`)
- :class:`Resampler` now is iterable like :class:`GroupBy` (:issue:`15314`).

.. _whatsnew_0240.api_breaking:

Expand Down
17 changes: 17 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ def __getattr__(self, attr):

return object.__getattribute__(self, attr)

def __iter__(self):
"""
Resampler iterator
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a bit more explanation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returns
-------
Generator yielding sequence of (name, subsetted object)
for each group
See Also
--------
GroupBy.__iter__
"""
self._set_binner()
return super(Resampler, self).__iter__()

@property
def obj(self):
return self.groupby.obj
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,17 @@ def test_apply_to_empty_series(self):

assert_series_equal(result, expected, check_dtype=False)

def test_resampler_is_iterable(self):
# GH 15314
series = self.create_series()
freq = 'H'
tg = TimeGrouper(freq, convention='start')
grouped = series.groupby(tg)
resampled = series.resample(freq)
for (rk, rv), (gk, gv) in zip(resampled, grouped):
assert rk == gk
assert_series_equal(rv, gv)


class TestDatetimeIndex(Base):
_index_factory = lambda x: date_range
Expand Down