jeudi 23 juin 2016

How to align indexes of many dataframes and fill in respective missing values in Pandas?

I have 4 dataframes with data of similar datetime indexes, however in each of them there are few missing lines and I know that the gaps can be filled using previous known data. I would like to 'align' these dataframes so that they have union of indexes of all dataframes and to fill in missing values. I know how to do it for 2 dataframes: df1, df2 = df1.align(df2, axis=0, method='pad'), but what is the good way to do it for more than 2? I have tried this and it does work: df1 = pd.DataFrame({'values': 1}, index=pd.DatetimeIndex(['2016-06-01', '2016-06-03'])) df2 = pd.DataFrame({'values': 2}, index=pd.DatetimeIndex(['2016-06-02', '2016-06-04', '2016-06-07'])) df3 = pd.DataFrame({'values': 3}, index=pd.DatetimeIndex(['2016-06-01', '2016-06-05'])) commonIndex = df1.index.join(df2.index, how='outer').join(df3.index, how='outer') for d in [df1, df2, df3]: d = d.reindex(index=commonIndex, method='pad') However when I try same approach on my real data, it gives error: "ValueError: index must be monotonic increasing or decreasing". It is price data, and index looks like this: DatetimeIndex(['2014-03-24 00:00:00', '2014-03-24 00:01:00', '2014-03-24 00:02:00', '2014-03-24 00:03:00', '2014-03-24 00:04:00', '2014-03-24 00:05:00', '2014-03-24 00:06:00', '2014-03-24 00:07:00', '2014-03-24 00:08:00', '2014-03-24 00:09:00', ... '2014-10-10 17:51:00', '2014-10-10 17:52:00', '2014-10-10 17:53:00', '2014-10-10 17:54:00', '2014-10-10 17:55:00', '2014-10-10 17:56:00', '2014-10-10 17:57:00', '2014-10-10 17:58:00', '2014-10-10 17:59:00', '2014-10-10 18:00:00'], dtype='datetime64[ns]', name=u'datetime', length=139671, freq=None) As far as I understand it should be increasing. Not sure about 'monotonic', but I assumed that it wasn't a constraint as demonstrated in above example (there were still missing dates). Any help is greatly appreciated, and forgive me if I'm not using the correct terminology. P.S. When I iterate through list of dataframes it looks like they are not actually saved in df1, df2, df3 after applying reindexing. How do I fix that?

Aucun commentaire:

Enregistrer un commentaire