-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Problem with DataFrame.replace using None #26050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please provide a code sample that can reproduce the issue clearly |
The code sample can reproduce the issue clearly. But to make an even more obvious issue: import pandas as pd
import numpy as np
k = np.array([['0','0','0','0'],['0','1','0','1'],['0','0','0','0']])
pd.DataFrame(k).replace('0',None) Expected output
Actual output
|
OK thanks - the second example is much clearer. It is interesting that this way of calling replace will get you what you want: pd.DataFrame(k).replace({'0': None}) So something might be off with inference. In any case investigation and PRs are always welcome |
The issue here is that the The workaround here is to use
A little bit of a quibble, but I'd disagree with this to a certain extent. This might be true in the context of the Python language itself, but My advice is to always use |
closing as duplicate of #19998. ping to reopen if I'm missing something. |
@simonjayhawkins why it's closed? the problem still remain. |
see #26050 (comment) |
Code Sample, a copy-pastable example if possible
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 eitherNone
orNaN
, 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
The text was updated successfully, but these errors were encountered: