[英]How to slice pandas' dataframe / series into hours blocks based on index?
我想從包含多天數據(從01.05.2018到18.05.2018)的數據框中保存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
我的索引的類型是:“時間戳”
我使用以下代碼繪制整個數據周期:
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');
現在,我想將此圖切成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_to_ts是將字符串轉換為時間戳的函數:
str_to_ts = udf (lambda d: datetime.strptime(d, "%Y-%m-%dT%H:%M:%S.%f+02:00"), TimestampType())
但這不起作用..
任何人都有一個想法如何做到這一點?
我認為您可以:
# 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
希望這對你有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.