我想读入工作日数据,然后重新编制数据索引,用周五的数据填充周末。我已经尝试了下面的代码,但它不会重新索引数据。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
最佳答案
仅按(1D)时间序列或作为一个系列(0.0.1):
data_1.reindex(rng, method = 'ffill')
data_1.reindex(Series(rng, index=rng), method = 'ffill')
.
以
date_rng
作为数据帧,我得到TypeError:无法将时间戳与0进行比较,我怀疑这可能是个错误,但我不完全确定预期的行为应该是什么。。。关于python - Pandas ,数据框set_index和重新索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15670937/