【发布时间】:2022-01-24 09:25:44
【问题描述】:
如下例所示,目标是将多索引时间序列重新采样为特定长度和频率。我已经达到了这个目标,但不幸的是通过.apply 电话。 apply 无限期地减慢我的代码速度。
除了多处理之外,还有更有效的方法吗?
import pandas as pd
#---------------------------------------------------------
# The input Data
#---------------------------------------------------------
input = pd.DataFrame([
['E', '2020-03-07', '2020-03-04', 0.3],
['E', '2020-03-07', '2020-03-06', 0.1],
['D', '2020-03-09', '2020-03-05', 0.5],
['D', '2020-03-09', '2020-03-06', 0.6],
],
columns=['id','y_date', 'x_date','a'])
input['x_date'] = pd.to_datetime(input['x_date'])
input['y_date'] = pd.to_datetime(input['y_date'])
#---------------------------------------------------------
# some paramters for the function groupwise_asfreq
#---------------------------------------------------------
input_t_dim = 6 # the desiered length of the back padded timeseries
missing_value = -1 # the value to fill for missing values
#---------------------------------------------------------
# the resampling and passing function
#---------------------------------------------------------
def groupwise_asfreq(group):
# resample the available data into the desiered interval e.g. 12h
freqenced = group.resample('12h', ).mean() # force a result with mean()
# take the resampled data and reindex them with a constucted date_range
padded=freqenced.reindex(pd.date_range(end=freqenced.index.max(),freq='12h',periods=input_t_dim, name='x_date'),fill_value=missing_value)
return padded
#---------------------------------------------------------
# the "convinient" apply
#---------------------------------------------------------
# use the unfortunate apply
output = input.set_index('x_date').groupby(['id','y_date']).apply(groupwise_asfreq)
# fill the remaining missing values
output = output.fillna(missing_value)
#---------------------------------------------------------
# Resulting DataFrame
#---------------------------------------------------------
a
id y_date x_date
D 2020-03-09 2020-03-03 12:00:00 -1.0
2020-03-04 00:00:00 -1.0
2020-03-04 12:00:00 -1.0
2020-03-05 00:00:00 0.5
2020-03-05 12:00:00 -1.0
2020-03-06 00:00:00 0.6
E 2020-03-07 2020-03-03 12:00:00 -1.0
2020-03-04 00:00:00 0.3
2020-03-04 12:00:00 -1.0
2020-03-05 00:00:00 -1.0
2020-03-05 12:00:00 -1.0
2020-03-06 00:00:00 0.1```
【问题讨论】:
-
为什么会有 nans?以为他们都应该用-1来填充
标签: pandas time-series padding resampling