Skip to content

Dataframe replace empty string casts Int64 columns to float #25438

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

Closed
miroli opened this issue Feb 25, 2019 · 2 comments · Fixed by #44940
Closed

Dataframe replace empty string casts Int64 columns to float #25438

miroli opened this issue Feb 25, 2019 · 2 comments · Fixed by #44940
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. replace replace method
Milestone

Comments

@miroli
Copy link

miroli commented Feb 25, 2019

Code Sample

df = pd.DataFrame({
    'a': [1, 2, 3, np.nan], 
    'b': ['some', 'strings', 'here', 'he']
})

df['a'] = df['a'].astype('Int64')

df.replace('', np.nan).dtypes  # Column a is now float

Problem description

The new Int64Dtype does not behave as expected here. If you do a global replacement of empty strings to NaN's, it seems weird that this would have the side effect of casting ints to floats.

Expected Output

Expected:

a     Int64
b    object
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

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

pandas: 0.24.1
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.16.1
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: 2.4.11
xlrd: 1.2.0
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.2.12
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@jreback
Copy link
Contributor

jreback commented Feb 25, 2019

since this is an optional dtype currently there are number of edge cases - welcome a PR to fix this

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Feb 25, 2019 via email

@gfyoung gfyoung added Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. labels Feb 27, 2019
@jbrockmendel jbrockmendel added the replace replace method label Sep 17, 2020
@mroeschke mroeschke added the Bug label Jun 27, 2021
@jreback jreback added this to the 1.4 milestone Dec 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants