Skip to content

ERR: better error message on invalid window when using .rolling #12669

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
anntzer opened this issue Mar 19, 2016 · 9 comments
Closed

ERR: better error message on invalid window when using .rolling #12669

anntzer opened this issue Mar 19, 2016 · 9 comments
Labels
Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@anntzer
Copy link
Contributor

anntzer commented Mar 19, 2016

xref #12765. Let's pick up this as a test case and give a more meaningful error message. e.g. for Window the win_type is missing, so this should be the error.

In [15]: pd.DataFrame(np.arange(10)).rolling(2., center=True).median()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-73c4e557093d> in <module>()
----> 1 pd.DataFrame(np.arange(10)).rolling(2., center=True).median()

/usr/lib/python3.5/site-packages/pandas/core/window.py in median(self, **kwargs)
    811     @Appender(_shared_docs['median'])
    812     def median(self, **kwargs):
--> 813         return super(Rolling, self).median(**kwargs)
    814 
    815     @Substitution(name='rolling')

/usr/lib/python3.5/site-packages/pandas/core/window.py in median(self, how, **kwargs)
    592         if self.freq is not None and how is None:
    593             how = 'median'
--> 594         return self._apply('roll_median_c', how=how, **kwargs)
    595 
    596     _shared_docs['std'] = dedent("""

/usr/lib/python3.5/site-packages/pandas/core/window.py in _apply(self, func, window, center, check_minp, how, **kwargs)
    473             # calculation function
    474             if center:
--> 475                 offset = _offset(window, center)
    476                 additional_nans = np.array([np.NaN] * offset)
    477 

/usr/lib/python3.5/site-packages/pandas/core/window.py in _offset(window, center)
   1356 def _offset(window, center):
   1357     if not com.is_integer(window):
-> 1358         window = len(window)
   1359     offset = (window - 1) / 2. if center else 0
   1360     try:

TypeError: object of type 'float' has no len()

Note that pd.DataFrame(np.arange(10)).rolling(2.).median() works fine.

pandas 0.18.0.

@jreback
Copy link
Contributor

jreback commented Mar 19, 2016

yeah looks like the validation is only in place for a Window and not the Rolling and sub-classes. It should be an int, or raise a nicer message.

pull-requests welcome.

@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels Mar 19, 2016
@jreback jreback added this to the 0.18.1 milestone Mar 19, 2016
@jreback jreback changed the title Confusing error when passing a float size to rolling(center=True) ERR: better error message on invalid window when using .rolling Mar 19, 2016
@shoyer
Copy link
Member

shoyer commented Mar 25, 2016

I would prefer requiring an int raising with an informative TypeError in this case.

@anntzer
Copy link
Contributor Author

anntzer commented Mar 25, 2016

I'm also in favor of requiring ints too.

@pt247
Copy link

pt247 commented Mar 29, 2016

Dose this look all right? I don't think we need ' whatsnew entry'. Do we?

@jreback
Copy link
Contributor

jreback commented Mar 29, 2016

@pt247 pls comment on PRs directly - always need a whatsnew entry

@gliptak
Copy link
Contributor

gliptak commented Apr 1, 2016

The input validation is inconsistent in window.py. I was proposing to reuse ``_prep_window` to unify behaviour. Could somebody familiar with the code offer guidance (instead of spot fixes)? Thanks

@jreback
Copy link
Contributor

jreback commented Apr 1, 2016

@gliptak these DO have different validations, as Window can accept an integer or an integer array, while Rolling can only accept an integer. This does not require a big change.

@gliptak
Copy link
Contributor

gliptak commented Apr 10, 2016

I closed #12714, #12718 is closer to the approach described.

@jreback
Copy link
Contributor

jreback commented Apr 10, 2016

ok, that's fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
5 participants