Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fillna(None) inconsistent with fillna(1) or fillna(np.NaN) #1972

Closed
PhE opened this issue Sep 26, 2012 · 3 comments
Closed

fillna(None) inconsistent with fillna(1) or fillna(np.NaN) #1972

PhE opened this issue Sep 26, 2012 · 3 comments

Comments

@PhE
Copy link

PhE commented Sep 26, 2012

Migrated from StackOverflow comment : http://stackoverflow.com/questions/12598520/set-index-on-multiple-columns-with-one-empty-column

    df = DataFrame([
    dict(a=1, p=0), 
    dict(a=2, m=10), 
    dict(a=3, m=11, p=20), 
    dict(a=4, m=12, p=21)
    ], columns=('a', 'm', 'p', 'x'))
. a m p x
0 1 NaN 0 NaN
1 2 10 NaN NaN
2 3 11 20 NaN
3 4 12 21 NaN

Applying fillna(None) turns one value to 0 !

df.fillna(None)

. a m p x
0 1 NaN 0 NaN
1 2 10 0 NaN
2 3 11 20 NaN
3 4 12 21 NaN

Applying fillna(1) works as expected :

df.fillna(1)

. a m p x
0 1 1 0 1
1 2 10 1 1
2 3 11 20 1
3 4 12 21 1

Applying fillna(np.NaN) works as expected :

df.fillna(np.NaN)

. a m p x
0 1 NaN 0 NaN
1 2 10 NaN NaN
2 3 11 20 NaN
3 4 12 21 NaN

Could be related to #1971

@changhiskhan
Copy link
Contributor

Note that fillna(None) is equivalent to fillna(), which means the value parameter is unused. Instead, it uses the method parameter which is by default forward fill. Which is why the NaN after the 0 in column p is filled.

@hayd
Copy link
Contributor

hayd commented Jan 4, 2013

Note: you can do this using the where method:

In [10]: df.where(pd.notnull(df), None)
Out[10]: 
   a     m     p     x
0  1  None     0  None
1  2    10  None  None
2  3    11    20  None
3  4    12    21  None

@thadd3us-abcellera
Copy link

Note: you can do this using the where method:

In [10]: df.where(pd.notnull(df), None)
Out[10]: 
   a     m     p     x
0  1  None     0  None
1  2    10  None  None
2  3    11    20  None
3  4    12    21  None

The df.where method no longer works at Pandas>=1.3. Instead, use:

df.replace({np.nan: None})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants