ENH: support monotonic_decreasing on rolling · Issue #19248 · pandas-dev/pandas (original) (raw)

import pandas as pd

index = [ pd.Timestamp('20130101 09:00:00'), pd.Timestamp('20130101 09:00:02'), pd.Timestamp('20130101 09:00:03'), pd.Timestamp('20130101 09:00:05'), pd.Timestamp('20130101 09:00:06') ]

df = pd.DataFrame({'price': [3, 4, 4, 2, 1]}, index=reversed(index)) df.rolling('2s').min()

I'm trying to work with time series data frames in reversed order. It seems pandas is failing due to index must be monotonic, although the index is clearly mononotonic decreasing.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-77eb98350c02> in <module>()
----> 1 df.rolling('1s').mean()

~/.local/share/virtualenvs/src-l39fhq8N/lib/python3.6/site-packages/pandas/core/generic.py in rolling(self, window, min_periods, freq, center, win_type, on, axis, closed)
   7065                                    min_periods=min_periods, freq=freq,
   7066                                    center=center, win_type=win_type,
-> 7067                                    on=on, axis=axis, closed=closed)
   7068 
   7069         cls.rolling = rolling

~/.local/share/virtualenvs/src-l39fhq8N/lib/python3.6/site-packages/pandas/core/window.py in rolling(obj, win_type, **kwds)
   2067         return Window(obj, win_type=win_type, **kwds)
   2068 
-> 2069     return Rolling(obj, **kwds)
   2070 
   2071 

~/.local/share/virtualenvs/src-l39fhq8N/lib/python3.6/site-packages/pandas/core/window.py in __init__(self, obj, window, min_periods, freq, center, win_type, axis, on, closed, **kwargs)
     84         self.win_freq = None
     85         self.axis = obj._get_axis_number(axis) if axis is not None else None
---> 86         self.validate()
     87 
     88     @property

~/.local/share/virtualenvs/src-l39fhq8N/lib/python3.6/site-packages/pandas/core/window.py in validate(self)
   1102                                          timedelta))):
   1103 
-> 1104             self._validate_monotonic()
   1105             freq = self._validate_freq()
   1106 

~/.local/share/virtualenvs/src-l39fhq8N/lib/python3.6/site-packages/pandas/core/window.py in _validate_monotonic(self)
   1134             formatted = self.on or 'index'
   1135             raise ValueError("{0} must be "
-> 1136                              "monotonic".format(formatted))
   1137 
   1138     def _validate_freq(self):

ValueError: index must be monotonic

The expected output should be that the min should be calculated from the 2s previous samples

                     price
2013-01-01 09:00:00    3.0
2013-01-01 09:00:02    4.0
2013-01-01 09:00:03    2.0
2013-01-01 09:00:05    1.0
2013-01-01 09:00:06    1.0