reindex
and method=ffill
does implement for string. However, it requires the dataframe/series index and columns must be monotonic increase or decrease when you use method
option. frame.columns
is non-monotonic, so it fails.
Let try this example by making columns of frame
is in lexical order. method=ffill
works fine when both index
and column
are monotonic even though they are strings:
frame = pd.DataFrame(np.arange(9).reshape((3, 3)),index=['a', 'c', 'd'],columns=['California', 'Ohio', 'Texas'])
states = ['Texas', 'Utah', 'California']
frame.reindex(index=['a','b','c','d'],method='ffill',columns=states)
Out[876]:
Texas Utah California
a 2 2 0
b 2 2 0
c 5 5 3
d 8 8 6
If your index
and columns
are strings and not monotonic, you need to call .ffill
, .bfill
or .fillna
outside after of reindex
Note: this constraint on method
option is also applied to numeric index
. Just try create a dataframe with numeric non-monotonic index, reindex with method
will return the same error.
from docs:
method : {None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’}
Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index.