3

我想读入工作日数据,然后重新索引数据以用周五的数据填充周末。我已经尝试了以下代码,但它不会重新索引数据。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
4

1 回答 1

3

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

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

.

作为date_rngDataFrame,我得到TypeError:无法将时间戳与0进行比较,我怀疑这可能是一个错误,但我不完全确定预期的行为应该是什么......

于 2013-03-28T00:52:37.173 回答