Skip to content

BUG: astype("str") converts NA values to strings #44156

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
3 tasks done
alexreg opened this issue Oct 23, 2021 · 5 comments
Closed
3 tasks done

BUG: astype("str") converts NA values to strings #44156

alexreg opened this issue Oct 23, 2021 · 5 comments
Labels
Astype Bug Dtype Conversions Unexpected or buggy dtype conversions Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate NA - MaskedArrays Related to pd.NA and nullable extension arrays Strings String extension data type and string data

Comments

@alexreg
Copy link
Contributor

alexreg commented Oct 23, 2021

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

pd.Series(["foo", "bar", pd.NA]).astype("str") // ["foo", "bar", "<NA>"]
pd.Series(["foo", "bar", np.NaN]).astype("str") // ["foo", "bar", "nan"]
pd.Series(["foo", "bar", None]).astype("str") // ["foo", "bar", "None"]

Issue Description

NA/null values are converted to their string representation when doing .astype("str"), which seems like odd/undesirable behaviour.

Expected Behavior

import pandas as pd
import numpy as np

pd.Series(["foo", "bar", pd.NA]).astype("str") // ["foo", "bar", pd.NA]
pd.Series(["foo", "bar", np.NaN]).astype("str") // ["foo", "bar", np.NaN]
pd.Series(["foo", "bar", None]).astype("str") // ["foo", "bar", None]

Installed Versions

INSTALLED VERSIONS

commit : 9b2bb73
python : 3.9.7.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.4.0.dev0+952.g9b2bb732f0
numpy : 1.21.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3
setuptools : 58.2.0
Cython : 0.29.24
pytest : 6.2.5
hypothesis : 6.23.4
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : None
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : None
xlrd : 2.0.1
xlwt : None
numba : None

@alexreg alexreg added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 23, 2021
@jreback
Copy link
Contributor

jreback commented Oct 23, 2021

there is an open PR that addresses pls search for the issue and link it

@alexreg
Copy link
Contributor Author

alexreg commented Oct 23, 2021

@jreback Is this the one? #41412

I don't have privileges to actually link the issue to the PR, as far as I'm aware.

@jreback
Copy link
Contributor

jreback commented Oct 23, 2021

@alexreg thanks now
does this duplicate #40839 ?

@alexreg
Copy link
Contributor Author

alexreg commented Oct 23, 2021

@jreback I believe so, but I'm by no means 100% sure. Maybe best to leave this open for now so the PR author can add a test case for this issue too? If that's alright with you...

@lithomas1 lithomas1 added Dtype Conversions Unexpected or buggy dtype conversions Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Strings String extension data type and string data and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 28, 2021
@jbrockmendel jbrockmendel added the NA - MaskedArrays Related to pd.NA and nullable extension arrays label Dec 17, 2021
@asishm
Copy link
Contributor

asishm commented Feb 13, 2022

duplicate of #25353

also #37034 introduced tests that explicitly test this behavior

@jreback jreback closed this as completed Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astype Bug Dtype Conversions Unexpected or buggy dtype conversions Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate NA - MaskedArrays Related to pd.NA and nullable extension arrays Strings String extension data type and string data
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants