got data like that in a csv
id,date,price
A,2020-05-04 15:00:00,1.2
B,2020-05-04 15:05:00,1.3
A,2020-05-04 16:07:00,1.4
B,2020-05-04 16:07:00,1.5
so there is a time series for A and another one for B in same DataFrame. I created dataframe and put index like df.set_index(["id","date"])
now I want to upsample to minute so that result corresponds to a csv looking like that:
id,date,price
A,2020-05-04 15:00:00,1.2
A,2020-05-04 15:01:00,1.2
...
A,2020-05-04 16:06:00,1.2
A,2020-05-04 16:07:00,1.4
B,2020-05-04 15:05:00,1.3
B,2020-05-04 15:06:00,1.3
...
B,2020-05-04 16:06:00,1.3
B,2020-05-04 16:07:00,1.5
but dont get that df.resample("60S", level ="date").pad()
thing going, getting error that I can only use it for "Datetimeindex but it is Multiindex" when omitting level and putting level getting "Upsampling from level= or on= selection is not supported, use .set_index to explicitly set index to datetime-like" error...
I feel there must be a simple solution...?inst it very common to have multiple instances of a series in one dataset? For sure I could split dataframe and combine thereafter... There is also some post using grouper but that to me sounds overly complex, to it sounds like level parameter must be the solution somehow...
Create DatetimeIndex
and use DataFrame.groupby
chained with DataFrameGroupBy.resample
:
df = df.set_index("date").groupby('id').resample("60S").pad()
print (df)
id price
id date
A 2020-05-04 15:00:00 A 1.2
2020-05-04 15:01:00 A 1.2
2020-05-04 15:02:00 A 1.2
2020-05-04 15:03:00 A 1.2
2020-05-04 15:04:00 A 1.2
.. ...
B 2020-05-04 16:03:00 B 1.3
2020-05-04 16:04:00 B 1.3
2020-05-04 16:05:00 B 1.3
2020-05-04 16:06:00 B 1.3
2020-05-04 16:07:00 B 1.5
[131 rows x 2 columns]
For processing only price
column use:
df1 = df.set_index("date").groupby('id')['price'].resample("60S").pad().reset_index()
print (df1)
id date price
0 A 2020-05-04 15:00:00 1.2
1 A 2020-05-04 15:01:00 1.2
2 A 2020-05-04 15:02:00 1.2
3 A 2020-05-04 15:03:00 1.2
4 A 2020-05-04 15:04:00 1.2
.. .. ... ...
126 B 2020-05-04 16:03:00 1.3
127 B 2020-05-04 16:04:00 1.3
128 B 2020-05-04 16:05:00 1.3
129 B 2020-05-04 16:06:00 1.3
130 B 2020-05-04 16:07:00 1.5
[131 rows x 3 columns]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加