请原谅我的风格不太好,解决方案也不够高效。非常感谢您的所有帮助。
背景:
尝试在一年的时间内,隔离出6周内最佳的骑车表现提升率。性能是通过测量任何给定时间段内产生的最大功率来衡量的,例如1、5、20分钟的努力等等...
任务:
- 创建滚动窗口
- 为每个窗口创建最佳趋势线
- 保留对应于最大正斜率的窗口
ap1 = np.array([[datetime(2015, 10, 17, 12, 45, 13),
datetime(2015, 10, 18, 11, 56, 35),
datetime(2015, 10, 20, 9, 24, 52),
datetime(2015, 10, 23, 9, 27, 12),
datetime(2015, 10, 24, 12, 26, 33)],
[281.0, 343.0, 270.0, 312.0, 320.0],
[246.0, 305.0, 260.0, 283.0, 289.0],
[236.0, 250.0, 239.0, 257.0, 245.0]], dtype=object)
问题:我目前卡在任务1上。我一直在尝试遵循user2689410对于计算不规则时间序列的rolling_mean的响应。我希望能够使用他的数据切片方法。
我只想将数据集切成45天的滚动间隔。以下是进展:
from pandas import Series, DataFrame
import pandas as pd
from datetime import datetime, timedelta
import numpy as np
idx = ap1[0]
idx = pd.Index(idx)
ap1=np.transpose(ap1)
ap1=pd.DataFrame(ap1, index = idx, columns = ['date', 'cp1', 'cp2', 'cp3'])
ap2=ap1.drop('date', 1)
ap2 = DataFrame(ap2.copy())
idx = Series(ap2.index.to_pydatetime(), index=ap2.index)
for colname, col in ap2.iteritems():
dslice = col[idx-pd.tseries.frequencies.to_offset('42D').delta:idx]
for循环给我报错:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/local/lib64/python2.7/site-packages/pandas/core/series.py", line 642, in __getitem__
return self._get_with(key)
File "/usr/local/lib64/python2.7/site-packages/pandas/core/series.py", line 647, in _get_with
indexer = self.index._convert_slice_indexer(key, kind='getitem')
File "/usr/local/lib64/python2.7/site-packages/pandas/indexes/base.py", line 1208, in _convert_slice_indexer
indexer = self.slice_indexer(start, stop, step, kind=kind)
File "/usr/local/lib64/python2.7/site-packages/pandas/tseries/index.py", line 1497, in slice_indexer
return Index.slice_indexer(self, start, end, step, kind=kind)
File "/usr/local/lib64/python2.7/site-packages/pandas/indexes/base.py", line 2962, in slice_indexer
kind=kind)
File "/usr/local/lib64/python2.7/site-packages/pandas/indexes/base.py", line 3141, in slice_locs
start_slice = self.get_slice_bound(start, 'left', kind)
File "/usr/local/lib64/python2.7/site-packages/pandas/indexes/base.py", line 3084, in get_slice_bound
slc = self.get_loc(label)
File "/usr/local/lib64/python2.7/site-packages/pandas/tseries/index.py", line 1419, in get_loc
stamp = Timestamp(key, tz=self.tz)
File "pandas/tslib.pyx", line 405, in pandas.tslib.Timestamp.__new__ (pandas/tslib.c:9932)
File "pandas/tslib.pyx", line 1475, in pandas.tslib.convert_to_tsobject (pandas/tslib.c:26432)
TypeError: Cannot convert input to Timestamp
我接下来该怎么办?