简体   繁体   English

使用datetimeindex重新采样/插值时间序列

[英]resample/interpolate time series with datetimeindex

I have two dataframes each containing one or more time series from the same time frame but sampled at different timestamps. 我有两个数据帧,每个数据帧都包含来自同一时间范围的一个或多个时间序列,但在不同的时间戳上采样。

I'd like to merge them into a single one resampled and interpolated with the index of the first. 我想将它们合并为一个重采样并插入第一个的索引。

Here's a sample of the first dataframe: 这是第一个数据框的示例:

                        a      b      c     d
2013-01-01 07:00:00  0.45  24.33   9.04  0.00
2013-01-01 08:00:00  0.55  23.11  11.60  0.06
2013-01-01 09:00:00  0.69  27.23  18.18  0.03
2013-01-01 10:00:00  0.64  26.58  31.46  0.06
2013-01-01 11:00:00  0.36  17.50  42.58  0.29
2013-01-01 12:00:00  0.32  15.39  50.30  0.17
2013-01-01 13:00:00  0.41  17.73  51.45  0.13
2013-01-01 14:00:00  0.50  19.48  50.50  0.05
2013-01-01 15:00:00  0.48  18.32  51.51  0.03
2013-01-01 16:00:00  0.50  18.49  50.70  0.02
2013-01-01 17:00:00  1.13  32.89  40.07  0.20
2013-01-01 18:00:00  1.81  59.64  16.59  0.37

And the second one: 第二个:

                        e
2013-01-01 06:15:00   9.0
2013-01-01 06:45:00   9.0
2013-01-01 06:55:00   9.0
2013-01-01 07:15:00   9.0
2013-01-01 07:45:00   9.0
2013-01-01 07:55:00   9.0
2013-01-01 08:15:00  10.0
2013-01-01 08:45:00  11.0
2013-01-01 08:55:00  11.0
2013-01-01 09:15:00  12.0
2013-01-01 09:45:00  13.0
2013-01-01 09:55:00  13.0
2013-01-01 10:15:00  14.0
2013-01-01 10:45:00  15.0
2013-01-01 10:55:00  15.0
2013-01-01 11:15:00  14.0
2013-01-01 11:45:00  14.0
2013-01-01 11:55:00  14.0
2013-01-01 12:15:00  14.0
2013-01-01 12:45:00  14.0
2013-01-01 12:55:00  14.0
2013-01-01 13:15:00  14.0
2013-01-01 13:45:00  14.0
2013-01-01 13:55:00  14.0
2013-01-01 14:15:00  14.0
2013-01-01 14:45:00  14.0
2013-01-01 14:55:00  14.0
2013-01-01 15:15:00  14.0
2013-01-01 15:45:00  13.0
2013-01-01 15:55:00  13.0
2013-01-01 16:15:00  13.0
2013-01-01 16:45:00  13.0
2013-01-01 16:55:00  13.0
2013-01-01 17:15:00  12.0
2013-01-01 17:45:00  12.0
2013-01-01 17:55:00  12.0
2013-01-01 18:15:00  11.0

In this case the second one is more granular but that won't be necessarily the case. 在这种情况下,第二个更为精细,但不一定如此。 I'd like to resample the second one with dates from the first. 我想用第一个的日期重新采样第二个。 Is this possible in an elegant pandas way? 以优雅的熊猫方式有可能吗?

I tried reindex with the full dataframes but it complains about duplicate axis. 我尝试使用完整数据帧reindex ,但它抱怨轴重复。 Maybe that's really my issue. 也许那真的是我的问题。

A simple new_df = pd.concat((df1,df2), axis=1) retains all information and timestamps. 一个简单的new_df = pd.concat((df1,df2), axis=1)保留所有信息和时间戳。 You can choose to resample new_df as wished. 您可以根据需要选择对new_df重新采样。

In this specific case, you can do: 在这种情况下,您可以执行以下操作:

pd.concat((df1, df2.groupby(df2.index.floor('H')).mean()), axis=1)

Output: 输出:

                        a       b       c       d       e
idx                     
2013-01-01 06:00:00     NaN     NaN     NaN     NaN     9.000000
2013-01-01 07:00:00     0.45    24.33   9.04    0.00    9.000000
2013-01-01 08:00:00     0.55    23.11   11.60   0.06    10.666667
2013-01-01 09:00:00     0.69    27.23   18.18   0.03    12.666667
2013-01-01 10:00:00     0.64    26.58   31.46   0.06    14.666667
2013-01-01 11:00:00     0.36    17.50   42.58   0.29    14.000000
2013-01-01 12:00:00     0.32    15.39   50.30   0.17    14.000000
2013-01-01 13:00:00     0.41    17.73   51.45   0.13    14.000000
2013-01-01 14:00:00     0.50    19.48   50.50   0.05    14.000000
2013-01-01 15:00:00     0.48    18.32   51.51   0.03    13.333333
2013-01-01 16:00:00     0.50    18.49   50.70   0.02    13.000000
2013-01-01 17:00:00     1.13    32.89   40.07   0.20    12.000000
2013-01-01 18:00:00     1.81    59.64   16.59   0.37    11.000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM