8

Given a pd.Series, I would like to replace null values with a list. That is, given:

import numpy as np
import pandas as pd
ser = pd.Series([0,1,np.nan])

I want a function that would return

0        0
1        1
2    [nan]

But if I try using the natural function for this, namely fillna:

result = ser.fillna([np.nan])

but I get the error

TypeError: "value" parameter must be a scalar or dict, but you passed a "list"

Any suggestions of a simple way to acheive this?

splinter
  • 3,727
  • 8
  • 37
  • 82
  • 1
    This is bug , https://github.com/pandas-dev/pandas/issues/3435, BTW, why you need this ? – BENY Nov 13 '17 at 15:18

4 Answers4

12

Use apply, because fillna working with scalars only:

print (ser.apply(lambda x: [np.nan] if pd.isnull(x) else x))
0        0
1        1
2    [nan]
dtype: object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
3

You can change to object

ser=ser.astype('object')

Then assign the list np.nan

ser.loc[ser.isnull()]=[[np.nan]]
BENY
  • 317,841
  • 20
  • 164
  • 234
2

I ended up using

ser.loc[ser.isnull()] = ser.loc[ser.isnull()].apply(lambda x: [np.nan]) 

because pd.isnull(x) would give me ambiguous truth values error ( i have other lists in my series too ). This is a combination of YOBEN_S' and jezrael's answer.

user__42
  • 543
  • 4
  • 13
0

fillna can take a Series, and a list can be converted to a Series. Wrapping your list in pd.Series() worked for me:

result = ser.fillna(pd.Series([np.nan]))

result
0    0.0
1    1.0
2    NaN
dtype: float64
aaronpenne
  • 580
  • 5
  • 10