[英]Panda time series resample + adjusting values linearly
Using python and pandas, how do I resample a time series to even 5-min intervals (offset=zero min from whole hours) while also adjusting the values linearly?使用 python 和 pandas,我如何将时间序列重新采样到甚至 5 分钟的间隔(从整小时开始的偏移量 = 零分钟),同时线性调整值?
Hence, I want to turn this:因此,我想把这个:
value
00:01 2
00:05 10
00:11 22
00:14 28
into this:进入这个:
value
00:00 0
00:05 10
00:10 20
00:15 30
Please note how the "value"-column was adjusted.请注意“值”列是如何调整的。
PS.附言。
There is a lot of information about this everywhere on the inte.net, but I still wasn't able to find a function (sum, max, mean, etc, or write my own functino) that could accompish what I wanted to do. inte.net 上到处都有很多关于此的信息,但我仍然无法找到可以完成我想做的事情的 function(求和、最大值、均值等,或编写我自己的函数)。
I have reconsidered the code because the requirement was omitted from the comments.我重新考虑了代码,因为注释中省略了该要求。 Create a new data frame by combining the original data frame with a data frame that is extended to one minute.通过将原始数据框与扩展到一分钟的数据框组合来创建新的数据框。 I linearly interpolated the new data frame and extracted the results in 5-minute increments.我对新数据框进行线性插值,并以 5 分钟为增量提取结果。 This is my understanding of the process.这是我理解的过程。 If I'm wrong, please give me another answer.如果我错了,请给我另一个答案。
import pandas as pd
import numpy as np
import io
data = '''
time value
00:01 2
00:05 10
00:11 22
00:14 28
00:18 39
'''
df = pd.read_csv(io.StringIO(data), sep='\s+')
df['time'] = pd.to_datetime(df['time'], format='%H:%M')
time_rng = pd.date_range(df['time'][0], df['time'][4], freq='1min')
df2 = pd.DataFrame({'time':time_rng})
df2 = df2.merge(df, on='time', how='outer')
df2 = df2.set_index('time').interpolate('time')
df2.asfreq('5min')
value
time
1900-01-01 00:01:00 2.0
1900-01-01 00:06:00 12.0
1900-01-01 00:11:00 22.0
1900-01-01 00:16:00 33.5
You can use datetime and time module to get the sequence of time intervals.您可以使用 datetime 和 time 模块来获取时间间隔的序列。 Then use pandas to convert the dictionary into a dataframe. Here's the code to do that.然后使用 pandas 将字典转换为 dataframe。这是执行此操作的代码。
import time, datetime
import pandas as pd
#set the dictionary as time and value
data = {'Time':[],'Value':[]}
#set a to 00:00 (HH:MM)
a = datetime.datetime(1,1,1,0,0,0)
#loop through the code to create 60 mins. You can increase loop if you want more values
#skip by 5 to get your 5 minute interval
for i in range (0,61,5):
# add the time and value into the dictionary
data['Time'].append(a.strftime('%H:%M'))
data['Value'].append(i*2)
#add 5 minutes to your date-time variable
a += datetime.timedelta(minutes=5)
#now that you have all the values in dictionary 'data', convert to DataFrame
df = pd.DataFrame.from_dict(data)
#print the dataframe
print (df)
#for your reference, I also printed the dictionary
print (data)
The dictionary will look as follows:字典将如下所示:
{'Time': ['00:00', '00:05', '00:10', '00:15', '00:20', '00:25', '00:30', '00:35', '00:40', '00:45', '00:50', '00:55', '01:00'], 'Value': [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]}
The dataframe will look as follows: dataframe 将如下所示:
Time Value
0 00:00 0
1 00:05 10
2 00:10 20
3 00:15 30
4 00:20 40
5 00:25 50
6 00:30 60
7 00:35 70
8 00:40 80
9 00:45 90
10 00:50 100
11 00:55 110
12 01:00 120
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.