getting error "ValueError: index must be monotonic"
getting error "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)排序,即。 e.时间(-index)应该稳步增加。这样做是为了防止用户不期望的巨大计算成本。只需预先按索引排序即可满足该要求。
我有一些股票 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)排序,即。 e.时间(-index)应该稳步增加。这样做是为了防止用户不期望的巨大计算成本。只需预先按索引排序即可满足该要求。