熊猫:从csv的每日数据生成每小时数据

时间:2016-10-10 20:44:00

标签: python pandas dataframe

我有一个CSV文件,其日常数据如下:

DateTime        Price
10/3/2016 0:00  2.84
9/30/2016 0:00  2.84
9/29/2016 0:00  2.98
9/28/2016 0:00  3.07

我想为上面的每一天创建24小时价格系列,每小时价格将与我从csv文件中读取的每日价格相同。

我正在做以下事情:

import pandas as pd
price4mCSV = pd.read_csv(r'C:\Price.csv', index_col='DateTime', parse_dates=['DateTime']).asfreq('1H', method='ffill')

但是,price4mCSV为空。

1 个答案:

答案 0 :(得分:0)

试试这个:

In [125]: df.set_index('DateTime').resample('H').pad()
Out[125]:
                     Price
DateTime
2016-09-28 00:00:00   3.07
2016-09-28 01:00:00   3.07
2016-09-28 02:00:00   3.07
2016-09-28 03:00:00   3.07
2016-09-28 04:00:00   3.07
2016-09-28 05:00:00   3.07
2016-09-28 06:00:00   3.07
2016-09-28 07:00:00   3.07
2016-09-28 08:00:00   3.07
2016-09-28 09:00:00   3.07
2016-09-28 10:00:00   3.07
2016-09-28 11:00:00   3.07
2016-09-28 12:00:00   3.07
2016-09-28 13:00:00   3.07
2016-09-28 14:00:00   3.07
2016-09-28 15:00:00   3.07
2016-09-28 16:00:00   3.07
2016-09-28 17:00:00   3.07
2016-09-28 18:00:00   3.07
2016-09-28 19:00:00   3.07
2016-09-28 20:00:00   3.07
2016-09-28 21:00:00   3.07
2016-09-28 22:00:00   3.07
2016-09-28 23:00:00   3.07
2016-09-29 00:00:00   2.98
...                    ...
2016-10-02 00:00:00   2.84
2016-10-02 01:00:00   2.84
2016-10-02 02:00:00   2.84
2016-10-02 03:00:00   2.84
2016-10-02 04:00:00   2.84
2016-10-02 05:00:00   2.84
2016-10-02 06:00:00   2.84
2016-10-02 07:00:00   2.84
2016-10-02 08:00:00   2.84
2016-10-02 09:00:00   2.84
2016-10-02 10:00:00   2.84
2016-10-02 11:00:00   2.84
2016-10-02 12:00:00   2.84
2016-10-02 13:00:00   2.84
2016-10-02 14:00:00   2.84
2016-10-02 15:00:00   2.84
2016-10-02 16:00:00   2.84
2016-10-02 17:00:00   2.84
2016-10-02 18:00:00   2.84
2016-10-02 19:00:00   2.84
2016-10-02 20:00:00   2.84
2016-10-02 21:00:00   2.84
2016-10-02 22:00:00   2.84
2016-10-02 23:00:00   2.84
2016-10-03 00:00:00   2.84

[121 rows x 1 columns]

或者如果您不想将DateTime作为索引:

df.set_index('DateTime').resample('H').pad()