Skip to content

pandas.DataFrame.astype casts numerical nulls to string 'nan'  #28186

@shubhambmehta

Description

@shubhambmehta
import pandas as pd
import numpy as np

s = pd.Series([1, 2, np.nan])
print str(s.isnull().any())

s = s.astype(str)
print str(s.isnull().any())

##output
True 
False

Problem description

When casting a series from float to string, the nulls in float should also be nulls in string. But, astype method converts the nulls to string 'nan', and so it does not reflect as null.

Expected Output

True
True

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-154-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_IN
LOCALE: None.None

pandas: 0.24.1
pytest: 2.8.7
pip: 19.0.1
setuptools: 40.6.2
Cython: None
numpy: 1.16.1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.5.0
sphinx: None
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.4.3
feather: None
matplotlib: 1.5.1
openpyxl: 2.3.0
xlrd: 0.9.4
xlwt: 0.7.5
xlsxwriter: None
lxml.etree: 3.5.0
bs4: 4.4.1
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: 2.7.3.2 (dt dec pq3 ext lo64)
jinja2: 2.8
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