Skip to content

Series.rolling/DataFrame.rolling don't fully check arguments, have odd behavior when used with invalid inputs #13383

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
mikegraham opened this issue Jun 7, 2016 · 3 comments
Labels
Bug Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@mikegraham
Copy link
Contributor

In [171]: s = pd.Series(range(3))

In [172]: s.rolling(-1) # doesn't raise
Out[172]: Rolling [window=-1,center=False,axis=0]

In [173]: s.rolling(-1).mean() # Odd, indirect error
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-173-cda5b8dd0812> in <module>()
----> 1 s.rolling(-1).mean()

/home/mike/modernpandas/local/lib/python2.7/site-packages/pandas/core/window.pyc in mean(self, **kwargs)
    885     @Appender(_shared_docs['mean'])
    886     def mean(self, **kwargs):
--> 887         return super(Rolling, self).mean(**kwargs)
    888 
    889     @Substitution(name='rolling')

/home/mike/modernpandas/local/lib/python2.7/site-packages/pandas/core/window.pyc in mean(self, **kwargs)
    651 
    652     def mean(self, **kwargs):
--> 653         return self._apply('roll_mean', 'mean', **kwargs)
    654 
    655     _shared_docs['median'] = dedent("""

/home/mike/modernpandas/local/lib/python2.7/site-packages/pandas/core/window.pyc in _apply(self, func, name, window, center, check_minp, how, **kwargs)
    558                 result = np.apply_along_axis(calc, self.axis, values)
    559             else:
--> 560                 result = calc(values)
    561 
    562             if center:

/home/mike/modernpandas/local/lib/python2.7/site-packages/pandas/core/window.pyc in calc(x)
    553 
    554                 def calc(x):
--> 555                     return func(x, window, min_periods=self.min_periods)
    556 
    557             if values.ndim > 1:

/home/mike/modernpandas/local/lib/python2.7/site-packages/pandas/core/window.pyc in func(arg, window, min_periods)
    540                     # GH #12373: rolling functions error on float32 data
    541                     return cfunc(com._ensure_float64(arg),
--> 542                                  window, minp, **kwargs)
    543 
    544             # calculation function

pandas/algos.pyx in pandas.algos.roll_mean (pandas/algos.c:28921)()

pandas/algos.pyx in pandas.algos._check_minp (pandas/algos.c:19103)()

ValueError: min_periods must be >= 0

In [174]: pd.Series([]).rolling(-1).mean() # Never raises
Out[174]: Series([], dtype: float64)

In [175]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-71-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.0.2
setuptools: 20.1.1
Cython: 0.24
numpy: 1.11.0
scipy: 0.17.1
statsmodels: 0.6.1
xarray: None
IPython: 4.1.1
sphinx: None
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: 1.3.2
bottleneck: None
tables: 3.2.2
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: None

@mikegraham mikegraham changed the title Series.rolling/DataFrame.rolling don't fully check inputs, have odd behavior when used with invalid inputs Series.rolling/DataFrame.rolling don't fully check arguments, have odd behavior when used with invalid inputs Jun 7, 2016
@jreback jreback added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels Jun 7, 2016
@jreback jreback added this to the 0.18.2 milestone Jun 7, 2016
@jreback
Copy link
Contributor

jreback commented Jun 7, 2016

this particular one is pretty trivial to check when Rolling is created; OTOH, some errors will still be raised when a function is called e.g. kurt/skew. But this would be an improvement.

@priyankjain
Copy link
Contributor

priyankjain commented Jun 12, 2016

As part of validation that happens towards the end of initialization, we can check whether the window is a natural number. @jreback Don't get you when you say "some errors will still be raised for kurt/skew". The Rolling object should never be created when passed window is not >0.

@jreback
Copy link
Contributor

jreback commented Jun 12, 2016

kurt/skew require even more min_periods, but you don't know the calling methods until run-time.

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

Successfully merging a pull request may close this issue.

3 participants