thorwhalen
thorwhalen

Reputation: 2282

Python pandas replace by None actually replaces with previous value

pandas' replace function replaces targeted values with another, as expected:

>>> import pandas as pd
>>>
>>>
>>> t = pd.Series([10,20,30])
>>> t
0    10
1    20
2    30
dtype: int64
>>> t.replace(to_replace=20, value=222)
0     10
1    222
2     30
dtype: int64
>>> from numpy import nan; t.replace(to_replace=20, value=nan)
0    10.0
1     NaN
2    30.0
dtype: float64

But when asking to replace by None, it replaces by the previous value.

>>> t.replace(to_replace=20, value=None)
0    10
1    10
2    30
dtype: int64

What is the rationale behind this, if any?

Upvotes: 0

Views: 50

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61920

This is because to_replace is a scalar and value is None. That behaviour is described in the documentation:

The method to use when for replacement, when to_replace is a scalar, list or tuple and value is None.

The default method is pad, if you take a look at the code. This probably happens because None is often used to express the absence of the parameter.

Upvotes: 1

Related Questions