Question

I want read in weekday data and then reindex the data to fill the weekend with Friday's data. I have tried the following code but it will not reindex the data. Set_index produces a length error message.

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')

The data read in has 41 rows and the generated date values have 61 rows. Any suggestions?

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
Was it helpful?

Solution

Reindexing just by the (1D) timeseries or as a Series this works (in 0.10.1):

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

.

With date_rng as the DataFrame I get TypeError: Cannot compare Timestamp with 0, I suspect this could be a bug, but I'm not entirely sure what the expected behaviour should be...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top