Reputation: 23
This is my initial df
China USA
2009 NaN -9
2010 1.2 8
2011 1.5 NaN
2014 5.0 NaN
2015 NaN 8
and then I used reindex method to add some index
frame2=frame1.reindex([2009,2010,2011,2012,2013,2014,2015,2016],method="ffill")
I expected frame2 to pass from
China USA
2009 NaN -9
2010 1.2 8
2011 1.5 NaN
2012 NaN NaN
2013 NaN NaN
2014 5.0 NaN
2015 NaN 8
2016 NaN NaN
to
China USA
2009 NaN -9
2010 1.2 8
2011 1.5 8
2012 1.5 8
2013 1.5 8
2014 5.0 8
2015 5.0 8
2016 5.0 8
but it returned
China USA
2009 NaN -9
2010 1.2 8
2011 1.5 NaN
2012 1.5 NaN
2013 1.5 NaN
2014 5.0 NaN
2015 NaN 8
2016 NaN 8
it only filled some entries of my df..
Upvotes: 2
Views: 71
Reputation: 294338
In a sense, it did forward fill. It brought forward the NaN
from 2015
to 2016
in China
as well as from 2011
through 2013
in USA
.
Instead, do a ffill
after the reindex
frame1.reindex([2009,2010,2011,2012,2013,2014,2015,2016]).ffill()
China USA
2009 NaN -9.0
2010 1.2 8.0
2011 1.5 8.0
2012 1.5 8.0
2013 1.5 8.0
2014 5.0 8.0
2015 5.0 8.0
2016 5.0 8.0
# Original
China USA
2009 NaN -9
2010 1.2 8
2011 1.5 NaN
2014 5.0 NaN
2015 NaN 8
Your method did what it was supposed to. If took values found at existing indices and propagated them forward for all new indices.
frame1.reindex([2009,2010,2011,2012,2013,2014,2015,2016],method="ffill")
China USA
2009 NaN -9.0
2010 1.2 8.0
2011 1.5 NaN <- From Original
2012 1.5 NaN <- And forward filled
2013 1.5 NaN <- And here
2014 5.0 NaN <- was there in original
2015 From Original -> NaN 8.0
2016 And forward filled -> NaN 8.0
Upvotes: 3