Saturnix
Saturnix

Reputation: 10574

fillna in series with None

pd.Series([1,1,1,1, "something", 1]).astype(float).cumsum().fillna(None)

I'd like to fill NaNs with None in the series above but it tells:

ValueError: Must specify a fill 'value' or 'method'.

All the solutions I've found are using a dataframe, but I need specifically on a Serie. How can I do this?

Upvotes: 1

Views: 265

Answers (1)

anky
anky

Reputation: 75120

Try with:

s=pd.to_numeric(pd.Series([1,1,1,1, "something", 1]),errors='coerce').cumsum()
s.mask(s.isnull(),None)

0       1
1       2
2       3
3       4
4    None
5       5

Note: pd.Series([1,1,1,1, "something", 1]).astype(float).cumsum() wouldn't work without errors='coerce in pd.to_numeric. Also dtype returned is dtype: object

Upvotes: 2

Related Questions