Skip to content

Series.replace not working on slices of heterogenoues types #29813

@bluenote10

Description

@bluenote10

Code Sample

import pandas as pd
import numpy as np 

c = pd.Series([
    np.nan,
    1,
    "hello",
])
c_replaced_1 = c[0:3].replace({np.nan: None})
c_replaced_2 = c[0:2].replace({np.nan: None})
print(c_replaced_1)
print(c_replaced_2)

Problem description

Series.replace does not work in the second case. In both cases one would expect that the np.nan gets replaced by None. However, the replace fails in the second case.

The actual output is:

0     None
1        1
2    hello
dtype: object
0    NaN
1    1.0
dtype: float64

Expected Output

0     None
1        1
2    hello
dtype: object
0    None
1    1.0
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.15.candidate.1
python-bits: 64
OS: Linux
OS-release: 4.15.0-70-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: None
pip: 19.2.3
setuptools: 41.2.0
Cython: None
numpy: 1.15.2
scipy: 1.2.2
pyarrow: None
xarray: None
IPython: 5.8.0
sphinx: None
patsy: 0.5.1
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: None
xlrd: 1.2.0
xlwt: None
xlsxwriter: 1.1.2
lxml.etree: 4.2.1
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.3
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateNeeds TestsUnit test(s) needed to prevent regressionsgood first issuereplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions