当我对 Pandas 时间序列进行重新采样以减少数据点数量时,每个结果数据点的时间戳位于每个重新采样箱的开始处。当使用不同的重采样率过度绘制图形时,这会导致数据发生明显的变化。无论重采样率如何,如何将重采样数据的时间戳“居中”在其容器中?
我现在得到的是(重新采样到一小时时):
In [12]: d_r.head()
Out[12]:
2017-01-01 00:00:00 0.330567
2017-01-01 01:00:00 0.846968
2017-01-01 02:00:00 0.965027
2017-01-01 03:00:00 0.629218
2017-01-01 04:00:00 -0.002522
Freq: H, dtype: float64
我想要的是:
In [12]: d_r.head()
Out[12]:
2017-01-01 00:30:00 0.330567
2017-01-01 01:30:00 0.846968
2017-01-01 02:30:00 0.965027
2017-01-01 03:30:00 0.629218
2017-01-01 04:30:00 -0.002522
Freq: H, dtype: float64
MWE 显示明显的转变:
#!/usr/bin/env python3
Minimal working example:
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import seaborn
seaborn.set()
plt.ion()
# sample data
t = pd.date_range('2017-01-01 00:00', '2017-01-01 10:00', freq='1min')
d = pd.Series(np.sin(np.linspace(0, 7, len(t))), index=t)
d_r = d.resample('1h').mean()
d.plot()
d_r.plot()
最佳答案
我一般不知道如何使用中点。有 label
参数,但只有选项 right
和 left
。但是,在具体情况下,您可以使用 loffset
参数显式偏移重采样时间戳:
d.resample('1h', loffset='30min').mean()
(编辑:使用 30min
而不是 30T
,因为这样更具可读性: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases )
关于python - 重采样时间序列的中心日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47395119/