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.reinde
Solution 1:
Some element of reindex
requires the incoming index to be sorted. I'm deducing that when method
is passed, it fails to presort the incoming index and subsequently fails. I'm drawing this conclusion based on the fact that this works:
print df.sort_index().reindex(newIndex.sort_values(), method='ffill')
Solution 2:
It seems that this needs to be done on the columns as well.
In[76]: frame = DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'c', 'd'],columns=['Ohio', 'Texas', 'California'])
In[77]: frame.reindex(index=['a','b','c','d'],method='ffill',columns=states)
---> ValueError: index must be monotonic increasing or decreasing
In[78]: frame.reindex(index=['a','b','c','d'],method='ffill',columns=states.sort())
Out[78]:
Ohio Texas California
a 012
b 012
c 345
d 678
Post a Comment for "Pandas Reindex And Fill Missing Values: "index Must Be Monotonic""