Skip to content

BUG: NA are coerced in to NaN by concat #61303

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

Open
3 tasks done
davetapley opened this issue Apr 17, 2025 · 4 comments
Open
3 tasks done

BUG: NA are coerced in to NaN by concat #61303

davetapley opened this issue Apr 17, 2025 · 4 comments
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@davetapley
Copy link
Contributor

davetapley commented Apr 17, 2025

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

from pandas import NA, DataFrame, concat

a = DataFrame.from_dict({'val': [1], 'na': [NA]})
b = DataFrame.from_dict({'val': [2], 'na': [NA]})

print("a and b:")
print(a)
print(b)

print("concat:")
ab = concat([a, b])
print(ab)

Issue Description

NA are coerced in to NaN by concat:

a and b:
   val    na
0    1  <NA>
   val    na
0    2  <NA>
concat:
   val   na
0    1  NaN
0    2  NaN

Expected Behavior

The NA stay as NA.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.2
python-bits : 64
OS : Linux
OS-release : 6.8.0-1021-azure
Version : #25~22.04.1-Ubuntu SMP Thu Jan 16 21:37:09 UTC 2025
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.2.3
numpy : 1.26.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 24.0
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : 5.2.2
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 17.0.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.11.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

@davetapley davetapley added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 17, 2025
@davetapley davetapley changed the title BUG: BUG: NA are coerced in to NaN by `concat. Apr 17, 2025
@davetapley davetapley changed the title BUG: NA are coerced in to NaN by `concat. BUG: NA are coerced in to NaN by concat Apr 17, 2025
@arthurlw
Copy link
Contributor

arthurlw commented Apr 17, 2025

Thanks for raising this! Confirmed on main. This happens because DataFrame.from_dict({'na': [NA]}) gives the na column an object dtype, so concat falls back to NumPy’s object‐array logic, which doesn’t know about pd.NA and yields NaN instead. To preserve pd.NA you can first cast to a true nullable dtype. E.g.

a = pd.DataFrame.from_dict({'val': [1], 'na': pd.array([pd.NA], dtype='Int64')})
b = pd.DataFrame.from_dict({'val': [2], 'na': pd.array([pd.NA], dtype='Int64')})

This seems related to PDEP-16.

@yuanx749
Copy link
Contributor

Note that in documentation:

Currently, pandas does not use those data types using NA by default in a DataFrame or Series, so you need to specify the dtype explicitly. An easy way to convert to those dtypes is explained in the conversion section.

So you can convert the df like a = a.convert_dtypes().

@rhshadrach rhshadrach added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode Dtype Conversions Unexpected or buggy dtype conversions PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 19, 2025
@rhshadrach
Copy link
Member

Agreed with @arthurlw and @yuanx749 with the current workaround, but this coercion from NA to np.nan should also not happen. However work on this should wait for PDEP-16.

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 Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

4 participants