使用另一个时间序列的索引重新采样一个时间序列

Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
本文介绍了使用另一个时间序列的索引重新采样一个时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有 2 个具有相同列但不同日期时间索引的数据框.我想重新采样其中一个以使用另一个的索引,并在另一个索引中没有数据的任何日期从一个转发填充数据.

I have 2 data frames with identical columns but different datetime indices. I want to resample one of them to use the index of the other and forward fill data from the one on any dates in the index of the other in which there wasn't data for.

import pandas as pd
import numpy as np
from datetime import datetime as dt

a_values = np.random.randn(4, 4)
a_index = [dt(2012, 3, 16), dt(2012, 3, 19), dt(2012, 3, 20), dt(2012, 3, 21)]
a = pd.DataFrame(data=a_values, index=a_index)

b_values = np.trunc(np.random.randn(3, 4) * 1000)
b_index = [dt(2012, 3, 16), dt(2012, 3, 19), dt(2012, 3, 21)]
b = pd.DataFrame(data=b_values, index=b_index)

c_insert = a.ix['2012-03-20']
c = b.append(c_insert).sort()
c.ix['2012-03-20'] = c.ix['2012-03-19']

'a' 表示我想将其索引用作重采样参考的数据框.'b' 代表我想要重新采样和转发填充数据的数据框.'c' 代表我想要的结果.

'a' represents the data frame whose index I'd like to use as the resampling reference. 'b' represents the data frame I'd like to resample and forward fill data. 'c' represents what I'd like the results to look like.

请注意,b"缺少a"中存在的2012-03-20"索引.c"使用索引2012-03-19"的b"列中的数据填充索引2012-03-20"的列

Notice that 'b' is missing the '2012-03-20' index that exists in 'a'. 'c' populates the columns for index '2012-03-20' with the data in the columns from 'b' for index '2012-03-19'

pandas 是否具有执行此操作的功能.

Does pandas have the functionality to do this.

提前致谢.

皮尔

推荐答案

要通过引用索引重新采样,请使用 reindex.

To resample by a reference index, use reindex.

In [11]: b.reindex(a.index, method='ffill')
Out[11]: 
               0     1     2     3
2012-03-16  -926  -625   736   457
2012-03-19 -1024   742   732 -1020
2012-03-20 -1024   742   732 -1020
2012-03-21  1090 -1163  1652   -94

这篇关于使用另一个时间序列的索引重新采样一个时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)
Compute a compounded return series in Python(在 Python 中计算复合回报序列)