mercredi 13 juillet 2016

Pandas reindex and fill missing values: "Index must be monotonic"

In answering this stackoverflow question, I found some interesting behavior when using a fill method while reindexing a dataframe. This old bug report in pandas says that df.reindex(newIndex,method='ffill') should be equivalent to df.reindex(newIndex).ffill(), but that is NOT the behavior I'm witnessing Here's a code snippet that illustrates the behavior df = pd.DataFrame({'values': 2}, index=pd.DatetimeIndex(['2016-06-02', '2016-05-04', '2016-06-03'])) newIndex = pd.DatetimeIndex(['2016-05-04', '2016-06-01', '2016-06-02', '2016-06-03', '2016-06-05']) print(df.reindex(newIndex).ffill()) print(df.reindex(newIndex, method='ffill')) The first print statement works as expected. The second raises a ValueError: index must be monotonic increasing or decreasing What's going on here?

Aucun commentaire:

Enregistrer un commentaire