Zanam
Zanam

Reputation: 4807

Pandas: Generating hourly data from daily data from csv

I have a CSV file which has daily data as follows:

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

I wanted to create 24 hour price series for each day above and the hourly prices will be same as the daily price I read from the csv file.

I am doing the following:

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

However, price4mCSV is blank.

Upvotes: 1

Views: 3334

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

try this:

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]

or this if you don't want to have DateTime as an index:

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

Upvotes: 1

Related Questions