Python 重采样时间序列的中心日期时间

Python 重采样时间序列的中心日期时间,python,pandas,Python,Pandas,当我对时间序列进行重采样以减少数据点的数量时,每个结果数据点的时间戳都位于每个重采样箱的开始处。当使用不同的重采样率对图形进行过抽签时,这会导致数据的明显移动。无论重采样率如何,如何将重采样数据的时间戳“居中” 我现在得到的是(当重新采样到一小时时): 我想要的是: 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

当我对时间序列进行重采样以减少数据点的数量时,每个结果数据点的时间戳都位于每个重采样箱的开始处。当使用不同的重采样率对图形进行过抽签时,这会导致数据的明显移动。无论重采样率如何,如何将重采样数据的时间戳“居中”

我现在得到的是(当重新采样到一小时时):

我想要的是:

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()

在索引中添加30分钟的timedelta怎么样

df.index = df.index + datetime.timedelta(minutes=30)

一般来说,我不知道如何使用中点。有
label
-参数,但它只有
right
left
选项。但是,在这种具体情况下,您可以使用
loffset
-参数显式偏移重采样时间戳:

d.resample('1h', loffset='30min').mean()

(编辑:使用
30min
而不是
30T
,因为这样可读性更好:)

loffset可以工作,但它并不理想,因为我将重新采样到几个不同的比例(分钟、小时、日),并且我必须手动指定每个偏移量。
d.resample('1h', loffset='30min').mean()