Home > Blockchain >  How to preserve NaNs in a pandas Series comparison?
How to preserve NaNs in a pandas Series comparison?

Time:09-07

Say I have a series s with some NaNs:

import numpy as np
import pandas as pd

s = pd.Series([1, 2, np.nan])

print(s)

0    1.0
1    2.0
2    NaN
dtype: float64

and I want to perform a comparison operation, but preserving NaNs. For example, say I want to check equality of each element to 2, producing:

0    False
1     True
2      NaN
dtype: object

But if I do s == 2, the NaN element evaluates to False:

print(s == 2)

0    False
1     True
2    False
dtype: bool

What's the best way to do this?

CodePudding user response:

A crude way would be to do a second pass through the equality check output and replace any rows where NaNs should be:

import numpy as np
import pandas as pd

s = pd.Series([1, 2, np.nan])

comp = s==2
comp[np.isnan(s)] = np.nan

print(comp)
0    False
1     True
2      NaN
dtype: object

CodePudding user response:

One method:

s.where((s==2) | (pd.isna(s)), other=False).replace(2, True)

0    False
1     True
2      NaN
dtype: object
  • Related