Skip to content
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

BUG: replacing a string with None causes an unexpected forward fill #36984

Closed
2 of 3 tasks
PedroGFonseca opened this issue Oct 8, 2020 · 5 comments · Fixed by #45081
Closed
2 of 3 tasks

BUG: replacing a string with None causes an unexpected forward fill #36984

PedroGFonseca opened this issue Oct 8, 2020 · 5 comments · Fixed by #45081
Labels
Docs replace replace method
Milestone

Comments

@PedroGFonseca
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
df = pd.DataFrame(np.zeros((3, 3)))
df.iloc[2, 2] = ''
df = df.replace('', None)
print(df.iloc[2, 2] == '') # prints False
print(df.iloc[2, 2] == df.iloc[1, 2]) # prints True

Problem description

[this should explain why the current behaviour is a problem and why the expected output is a better solution]

Expected Output

Replacing a string with a None should introduce a None, not forward fill the previous result.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.6.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.3
numpy : 1.19.2
pytz : 2018.9
dateutil : 2.8.1
pip : 20.2
setuptools : 49.2.0
Cython : None
pytest : 5.4.3
hypothesis : None
sphinx : 1.8.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.9.0
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.16
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@PedroGFonseca PedroGFonseca added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 8, 2020
@PedroGFonseca
Copy link
Author

Note: replacing with np.nan does not cause this problem.

@asishm
Copy link
Contributor

asishm commented Oct 8, 2020

this is documented behavior.

to replace with None, you need to do df.replace({'': None}) per the docs

image

perhaps making it a bit more clearer in the docs might help?

@dsaxton dsaxton removed the Needs Triage Issue that has not been reviewed by a pandas team member label Oct 9, 2020
@PedroGFonseca
Copy link
Author

Thanks for the fast answer. I would suggest that it may cause some silent errors, given that it does not fail.

The behavior does indeed make sense, and it was 100% my mistake, but I fear that having "pad" as the default method may result in unexpected behaviors. Would it not make sense for it to not have a default method, and make the user make an explicit decision?

@asishm
Copy link
Contributor

asishm commented Oct 9, 2020

I would agree that the behavior is confusing. Not sure what the rationale was for this specific scenario.

A behavior change would probably need to be discussed (and then maybe go through a deprecation cycle if there is consensus).

@Jimsparkle
Copy link

Just come here to mention that this is very confusing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants