Skip to content

Initial implementation of rolling iterators #27399

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

Closed

Conversation

ThomasKluiters
Copy link
Contributor

@ThomasKluiters ThomasKluiters commented Jul 15, 2019

Hi, I have yet to implements tests, use flake8 and create a whatsnew entry as I am uncertain whether this implementation would be correct.

I would really appreciate it if someone could give me some feedback on the code.

This PR will implement iterators for window functions.

Some questions I have:

  • Does DataFrame.rolling(x) yield DataFrames?
  • Does Series.rolling(x) yield Series?

@pep8speaks
Copy link

pep8speaks commented Jul 15, 2019

Hello @ThomasKluiters! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-07-21 11:48:05 UTC

@WillAyd
Copy link
Member

WillAyd commented Jul 15, 2019

Can you add tests for this?

@WillAyd WillAyd added the Window rolling, ewma, expanding label Jul 15, 2019
@WillAyd
Copy link
Member

WillAyd commented Jul 15, 2019

Also I think yes to both of your questions

@ThomasKluiters
Copy link
Contributor Author

Can you add tests for this?

I wouldn't mind adding tests! I had to know what the expected output would be, but now that I know I'll be writing them! Thanks :)

@ThomasKluiters
Copy link
Contributor Author

@mroeschke I have changed the files according to your feedback, could you have a look?

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Looking good. Could you also add tests where min_periods is utilized?

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

Can you add expanding to the tests here as well?

@ThomasKluiters
Copy link
Contributor Author

Can you add expanding to the tests here as well?

They're already there, view the changes under test_expanding :)

@WillAyd
Copy link
Member

WillAyd commented Jul 17, 2019

@ThomasKluiters thanks - sorry misread that on my end

@ThomasKluiters
Copy link
Contributor Author

Looking good. Could you also add tests where min_periods is utilized?

@mroeschke
Expanding kind of already does this, but I have added three new unit tests that use window and min_periods, please check!

@mroeschke
Copy link
Member

For the min_periods tests, could you also test where there's nans in the Series/DataFrames?

@ThomasKluiters
Copy link
Contributor Author

For the min_periods tests, could you also test where there's nans in the Series/DataFrames?

Well, it turns out that this required a bit of work - I've added some tests for it and had to change some code in window.py

if arr.ndim == 1:
arr = np.expand_dims(arr, axis=1)

counts = libwindow.roll_sum(
Copy link
Member

Choose a reason for hiding this comment

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

Instead of this, could you just generate the slice and yield if the number non-null elements meets _minp?

result = values.iloc[slice(s, e)]
if result.count() > _minp:
    yield result

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep! Done!

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Looks pretty good! Over to @WillAyd, @jreback for a second look.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

can you add a small section in computation.rst which shows off the iterator, probably in the rolling parts would work well.

I had a thought to co-locate all of these tests in pandas/tests/window/test_iterator.py (rather than splitting them off to the sub-files). I am not convinced our current organziation of tests here is that great.

otherwise lgtm.

@@ -1206,6 +1206,7 @@ Groupby/resample/rolling
- Improved :class:`pandas.core.window.Rolling`, :class:`pandas.core.window.Window` and :class:`pandas.core.window.EWM` functions to exclude nuisance columns from results instead of raising errors and raise a ``DataError`` only if all columns are nuisance (:issue:`12537`)
- Bug in :meth:`pandas.core.window.Rolling.max` and :meth:`pandas.core.window.Rolling.min` where incorrect results are returned with an empty variable window (:issue:`26005`)
- Raise a helpful exception when an unsupported weighted window function is used as an argument of :meth:`pandas.core.window.Window.aggregate` (:issue:`26597`)
- Improved :class:`pandas.core.window.Rolling` and :class:`pandas.core.window.Window` it is now possible to iterate over a window object (:issue:`12537`)
Copy link
Contributor

Choose a reason for hiding this comment

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

move to 1.0 whatsnew

Copy link
Contributor

Choose a reason for hiding this comment

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

also provide a reference to the new doc section.

],
)
def test_iterator_dataframe(self, dataframe, expected, window):
expected = [DataFrame(values, index=index) for (values, index) in expected]
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 issue number as a comment here

],
)
def test_iterator_series(self, series, expected, window):
expected = [Series(values, index=index) for (values, index) in expected]
Copy link
Contributor

Choose a reason for hiding this comment

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

same

@jreback jreback added this to the 1.0 milestone Jul 20, 2019
@ThomasKluiters
Copy link
Contributor Author

@jreback

I have moved the tests to test_iterator and added the reference to the issue at the top.
I have moved the whatsnew to 1.0.0.
I have added the documentation to computation.rst

Though, I can't seem to figure out how to reference computation.rst in the Whatsnew, could you help me with this please :)? Thanks!

@jreback
Copy link
Contributor

jreback commented Jul 25, 2019

can you merge master and see if can get passing

@jreback
Copy link
Contributor

jreback commented Sep 8, 2019

IIRC this looked reasonable, can you merge master and update to any comments

@jreback
Copy link
Contributor

jreback commented Oct 3, 2019

closing this as stale, though its a nice change, needs a rebase and comments addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: implement __iter__ for window objects
5 participants