gpt4 book ai didi

python - 不规则时间序列上的 Pandas 滑动/滚动窗口

转载 作者:太空宇宙 更新时间:2023-11-04 06:41:38 25 4
gpt4 key购买 nike

请原谅糟糕的风格和低效的解决方案。非常感谢所有帮助。

上下文:

尝试在一年的时间里找出 6 周时间段内的最佳循环性能增益率。性能被衡量为在任何给定时间段内为一个循环记录产生的最大努力,即 1、5、20 分钟努力等...

任务:

  1. 创建滚动窗口
  2. 每个窗口的最佳拟合趋势线
  3. 保持窗口对应于最大的正斜率

数据:

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's对在不规则时间序列上计算 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

我从这里去哪里?

最佳答案

如今,pandas.DataFrame.rolling可以处理不规则的时间序列。

关于python - 不规则时间序列上的 Pandas 滑动/滚动窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40408436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com