熊猫,数据框set_index&重新索引

时间:2013-03-27 22:41:25

标签: python pandas reindex

我希望在工作日数据中读取数据,然后重新索引数据,以便用周五的数据填充周末。我尝试了以下代码,但它不会重新索引数据。 Set_index生成长度错误消息。

import pandas as pd

def fill_dataframe(filename):
    dataf = pd.read_csv(filename, header= None, index_col = [0])
return(dataf)

rng = pd.date_range('10/1/2010', periods=61)
date_rng = pd.DataFrame(rng,index = rng)

data_1.reindex(date_rng, method = 'ffill')

读入的数据有41行,生成的日期值有61行。有什么建议吗?

data read in by csv (1st 7 rows)
        X0     X1
10/1/2010  71.27
10/4/2010  70.33
10/5/2010  72.94
10/6/2010  74.15
10/7/2010  71.40
10/8/2010  72.58
10/11/2010  72.66

dates generated by rng in the second Data Frame (first 11 rows)
                         0
2010-10-01 2010-10-01 00:00:00
2010-10-02 2010-10-02 00:00:00
2010-10-03 2010-10-03 00:00:00
2010-10-04 2010-10-04 00:00:00
2010-10-05 2010-10-05 00:00:00
2010-10-06 2010-10-06 00:00:00
2010-10-07 2010-10-07 00:00:00
2010-10-08 2010-10-08 00:00:00
2010-10-09 2010-10-09 00:00:00
2010-10-10 2010-10-10 00:00:00
2010-10-11 2010-10-11 00:00:00

1 个答案:

答案 0 :(得分:3)

仅通过(1D)时间序列重新索引或作为一个系列工作(在0.10.1中):

data_1.reindex(rng, method = 'ffill')
data_1.reindex(Series(rng, index=rng), method = 'ffill')

使用date_rng作为DataFrame我得到TypeError:无法将Timestamp与0进行比较,我怀疑这可能是一个错误,但我不完全确定预期的行为应该是什么...... < / em>的