如何根据索引将Pandas的数据帧/序列分割成小时块?

2024-10-02 18:17:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从包含多天数据(从2018年5月1日到2018年5月18日)的数据帧中保存6h块的绘图。你知道吗

我的数据帧“EperDtPanda”有以下格式:

                               ldr
timestamp                         
2018-05-01T00:00:03.454+02:00  972
2018-05-01T00:00:08.532+02:00  972
2018-05-01T00:00:13.462+02:00  973
2018-05-01T00:00:18.467+02:00  973
2018-05-01T00:00:23.472+02:00  968
2018-05-01T00:00:28.480+02:00  972
2018-05-01T00:00:33.487+02:00  973
2018-05-01T00:00:38.484+02:00  970

我的索引类型为:“timestamp”

我正在绘制整个数据周期,使用以下代码:

indicies = map(lambda t: np.datetime64(t), EperEtPanda.index)
newIndextValues = map(lambda v: v[0], EperEtPanda.values)

ts = pd.Series(newIndextValues, index=indicies)
series2 = ts.resample('H').mean()
plt.plot(series2.index, series2.values)
plt.xticks(rotation='vertical');

我附上了18天的数据图。plot of whole period of 18 days

现在我想把这个图切成6h的图,然后保存这些图。 下面是我用来将图形分割为6h块的代码:

startDate = '2018-05-01T00:00:00+02:00'
endDate = '2018-05-18T00:00:00+02:00'
blockLength = 6
i = 0

while (str_to_ts(startDate) < str_to_ts(endDate)):
    mask = (EperEtPanda.index >= str_to_ts(startDate)) & (EperEtPanda.index <= (str_to_ts(startDate) + timedelta(hours=blockLength)))
    EperDtPanda6h = EperDtPanda.loc[mask]
    slice6h = EperDtPanda6h.plot()
    slice6h.get_figure().savefig('figure6h' + i + '.png')
    startDate = str_to_ts(startDate) + timedelta(hours=blockLength)
    i += 1

str \u to \u ts是一个函数,可将stings转换为时间戳:

str_to_ts =  udf (lambda d: datetime.strptime(d, "%Y-%m-%dT%H:%M:%S.%f+02:00"), TimestampType())

但它不起作用。。你知道吗

有人知道怎么做吗?你知道吗


Tags: to数据lambda代码mapindextimestampts
1条回答
网友
1楼 · 发布于 2024-10-02 18:17:56

我想你可以做到:

# to ensure timestamp type for indexes (not necessary if it's already the case for you)
EperEtPanda.index = pd.to_datetime(EperEtPanda.index)
# start and end date as timestamps
startDate = pd.to_datetime('2018-05-01T00:00:00+02:00')
endDate = pd.to_datetime('2018-05-18T00:00:00+02:00')
# create all the time between start and end with a freq of 6 hours
list_time = pd.date_range(startDate, endDate, freq='6H') 
# loop for with zip to have both start_time and end_time
i = 0
for start_time, end_time in zip(list_time[:-1], list_time[1:]):
    # select the 6h range with .loc and slice()
    EperDtPanda6h = EperDtPanda.loc[slice(start_time, end_time),'ldr']
    # plot and save
    EperDtPanda6h.plot().get_figure().savefig('figure6h' + i + '.png')
    i += 1

希望对你有用

相关问题 更多 >