Search code examples
pythonpython-2.7dataframefillna

fillna does fill the dataframe in the NaN cells


What am I missing? fillna doesn't fill NaN values:

#filling multi columns df with values..

df.fillna(method='ffill', inplace=True)
df.fillna(method='bfill', inplace=True)

#just for kicks
df = df.fillna(method='ffill')
df = df.fillna(method='bfill')

#retun true
print df.isnull().values.any()

I verified it - I actually see NaN values in some first cells..

Edit So I'm trying to write it myself:

def bfill(df):
    for column in df:
        for cell in df[column]:
            if cell is not None:
                tmpValue = cell
                break

        for cell in df[column]:
            if cell is not None:
                break
            cell = tmpValue

However it doesn't work... Isn't the cell is by ref?


Solution

  • So, I don't know why but taking the fillna outside the function fixed it..

    Origen:

    def doWork(df):
      ...
      df = df.fillna(method='ffill')
      df = df.fillna(method='bfill')
    
    def main():
      ..
      doWork(df)
      print df.head(5) #shows NaN
    

    Solution:

    def doWork(df):
      ...
    
    def main():
      ..
      doWork(df)
    
      df = df.fillna(method='ffill')
      df = df.fillna(method='bfill')
    
      print df.head(5) #no NaN