Skip to content

Problem with DataFrame.replace using None #26050

Closed
@josegcpa

Description

@josegcpa

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

ar = np.random.normal(size=[100,10])
df = pd.DataFrame(ar).astype(str)

df.replace('0',None)

Problem description

Every time I run something similar to this (replacing, in a string DataFrame/DataFrame column, a specific string with None), the columns get filled with the element directly above it in the column; this creates a lot of confusion since None is, pretty much, THE "pythonic" way of signalling the absence of a variable/whatever. It works fine if replaced with np.nan, but if anything, I would expect them to work very approximately the same.

Expected Output

The expected output would be all strings matching the expression in replace to either be replaced with either None or NaN, or at least a warning message alerting for this behaviour.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Darwin
OS-release: 18.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.24.1
pytest: None
pip: 19.0.3
setuptools: 40.6.2
Cython: None
numpy: 1.16.0
scipy: 1.2.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: 4.7.1
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions