Skip to content

BUG: Converting NumPy-nullable dtypes to str #60123

Closed
@pkludig

Description

@pkludig

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

pd.DataFrame(
    {
        "a": [1, 2, None]
    },
    dtype="Int64"
).astype(str).astype("Int64")

Issue Description

When casting an Int64 NA-Value to a string it returns <NA> which is not well interpretable as a missing value for anything. It even fails in converting it back to an Int64 value.

Expected Behavior

Int64 NA-Values should be casted to None when casted to String, as this is the equivalent represantation in a string-column, similar to being convertet to a float NaN for float, which is also shown by the following example, which works:

import pandas as pd

pd.DataFrame(
    {
        "a": [1, 2, None]
    },
    dtype="Int64"
).astype(str).replace("<NA>", None).astype("Int64")

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.10.12.final.0
python-bits : 64
OS : Linux
OS-release : 6.8.0-47-generic
Version : #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Oct 2 16:16:55 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.0.3
pip : 24.0
Cython : None
pytest : 8.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.22.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
pyarrow : 15.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.28
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsPDEP missing valuesIssues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprintStringsString extension data type and string data

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions