Replace value with NaN in a Series?

2,942 views
Skip to first unread message

Damien Moore

unread,
May 7, 2013, 11:29:42 AM5/7/13
to pyd...@googlegroups.com
I see that there is fillna to replace NaNs in a Series with a value, but what's the most efficient way to do the reverse? i.e. convert a certain value (e.g. zero) to NaN?

Jeff

unread,
May 7, 2013, 11:41:18 AM5/7/13
to pyd...@googlegroups.com

In [1]: s = Series([1.,3.,5.,np.nan,7.,7.,9.])

In [2]: s
Out[2]:
0     1
1     3
2     5
3   NaN
4     7
5     7
6     9
dtype: float64

In [3]: s.fillna(0)
Out[3]:
0    1
1    3
2    5
3    0
4    7
5    7
6    9
dtype: float64

In [4]: y = s.fillna(0)

In [5]: y[y==7] = np.nan

In [6]: y
Out[6]:
0     1
1     3
2     5
3     0
4   NaN
5   NaN
6     9
dtype: float64



On Tuesday, May 7, 2013 11:29:42 AM UTC-4, Damien Moore wrote:

Damien Moore

unread,
May 7, 2013, 5:31:18 PM5/7/13
to pyd...@googlegroups.com
Thanks. It looks like 

s[s==2] = None

also works.

But:

>>> s=pandas.Series([1,2,3])
>>> s[s==2] = pandas.np.NaN
Traceback (most recent call last):
...
ValueError: Cannot assign nan to integer series. 

Is it possible to have a missing value for integer, or other non-float? (It seems the answer is maybe in a DataFrame?)

Jeff

unread,
May 7, 2013, 5:52:59 PM5/7/13
to pyd...@googlegroups.com
The None are converted to np.nan internally
 
no, integer series cannot have NaN (a numpy limitation),
if you do this is a dataframe, what happens is that they will be converted to floats (a series
right now will throw an error)
 
you can however specify dtype=object to include NaN in an otherwise integer series, but
then you lose all efficiency so not recommended
Reply all
Reply to author
Forward
0 new messages