获取错误"ValueError: index must be monotonic"



我有一些股票SRE的分钟数据,这是一个csv文件。我已经导入了它,并使用数据创建了一个数据帧。然后我创建了一个滚动的20天移动平均线,但我得到了一个错误。代码为:

import pandas as pd
ticker = 'SRE'
df = pd.read_csv('/Volumes/Seagate Portable/S&P 500 List/{}.txt'.format(ticker))
df.columns = ['Extra', 'Dates', 'Open', 'High', 'Low', 'Close', 'Volume']
df.drop(['Extra', 'Open', 'High', 'Volume', 'Low'], axis=1, inplace=True)
df.Dates = pd.to_datetime(df.Dates)
df.set_index('Dates', inplace=True)
df = df.between_time('9:30', '16:00')
df[f'MA {ticker}'] = df.rolling('20d').mean()

虽然我得到了错误:

ValueError                                Traceback (most recent call last)
<ipython-input-51-2c18a3d320d8> in <module>
6 df.set_index('Dates', inplace=True)
7 df = df.between_time('9:30', '16:00')
----> 8 df[f'MA {ticker}'] = df.rolling('20d').mean()
9 
10 # data[f'MA {ticker}'] = pd.Series
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in rolling(self, window, min_periods, center, win_type, on, axis, closed)
11234             )
11235 
> 11236         return Rolling(
11237             self,
11238             window=window,
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/window/rolling.py in __init__(self, obj, window, min_periods, center, win_type, axis, on, closed, **kwargs)
111         self.win_freq = None
112         self.axis = obj._get_axis_number(axis) if axis is not None else None
--> 113         self.validate()
114 
115     @property
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/window/rolling.py in validate(self)
1897         ):
1898 
-> 1899             self._validate_monotonic()
1900 
1901             # we don't allow center
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/window/rolling.py in _validate_monotonic(self)
1938         """
1939         if not (self._on.is_monotonic_increasing or self._on.is_monotonic_decreasing):
-> 1940             self._raise_monotonic_error()
1941 
1942     def _raise_monotonic_error(self):
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/window/rolling.py in _raise_monotonic_error(self)
1944         if self.on is None:
1945             formatted = "index"
-> 1946         raise ValueError(f"{formatted} must be monotonic")
1947 
1948     def _validate_freq(self):
ValueError: index must be monotonic

df.sort_index(inplace = True)就在df.rolling('20D')行之前。

TimeSeries上的.rolling()函数要求行按时间(-index(排序,即时间(-inindex(应稳定增加。这样做是为了防止用户没有预料到的巨大计算成本。只需事先按索引进行排序就可以满足这一要求。

相关内容

最新更新