Search code examples
pandasfillna

fillNa(0) instead producing None


My question is just below the code snippet, below:

I have raw sensor time series data that .. is getting really close to be usable now :)

enter image description here

locDf = locationDf.copy()
locDf.set_index('date', inplace=True)

locDfs = {}
for user, user_loc_dc in locDf.groupby('user'):
    locDfs[user] = user_loc_dc.resample('15T').agg('max').bfill()

aDf = appDf.copy()
aDf.set_index('date', inplace=True)

userLocAppDfs = {}
appDfs = []
for user, a2_df in aDf.groupby('user'):
    userDf = a2_df.resample('15T').agg('min')

    userDf.reset_index(inplace=True)
    userDf = pd.crosstab(index=userDf['date'], columns=userDf['app'], values=userDf['metric'], aggfunc=np.mean).fillna(0, downcast='infer')

    userDf['user'] = user

    userDf.reset_index(inplace=True)
    userDf.set_index('date', inplace=True)

    appDfs.append(userDf)
    userLocAppDfs[user] = userDf

    loDf = locDfs[user]
    loDf.reset_index(inplace=True)
    loDf = pd.crosstab([loDf.date, loDf.user], loDf.location)
    loDf.reset_index(inplace=True)

    loDf.set_index('date', inplace=True)
    loDf.drop('user', axis=1, inplace=True)

    userLocAppDfs[user] = userLocAppDfs[user].join(loDf, how='outer')
    userLocAppDfs[user]['user'].fillna(user, inplace=True)

    #for app in a2_df['app'].unique():
    #    userLocAppDfs[user][app] = userLocAppDfs[user][app].fillna(0, inplace=True)


userLocAppDfs['user_1'].head(5)

Question

If I uncomment those last two lines to try to fill the NaN's at the start, I dont' get zeros. I get None. :( Can anyone tell me why?

None

I'd like to.. you know, get 0's there:

2017-08-28 00:00:00 0   0   user_1  0.0 0.0 0.0 1.0 0.0
2017-08-28 00:15:00 0   0   user_1  0.0 0.0 1.0 0.0 0.0
2017-08-28 00:30:00 0   0   user_1  0.0 0.0 1.0 0.0 0.0
2017-08-28 00:45:00 0   0   user_1  0.0 0.0 1.0 0.0 0.0
2017-08-28 01:00:00 0   0   user_1  0.0 0.0 1.0 0.0 0.0

The last step will be for me to get the rolling average of those app_* numbers, so that I get a curve.


Solution

  • Try

    for app in a2_df['app'].unique():
        userLocAppDfs[user][app].fillna(0, inplace=True)
        # or userLocAppDfs[user][app] = userLocAppDfs[user][app].fillna(0)
    

    So it is because you have specified inplace = True and at the same time you assign it back.

    Note that df.fillna(0, inplace=True) will not return a value. Rather it will directly modify the originaldf. Try print(df.fillna(0, inplace=True)), it will give you None. So what you've done above was assigning None to column apps.