Description
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
import pyarrow as pa
ser1 = pd.Series([1,2,3,None,10], dtype=pd.Int64Dtype())
ser2 = pd.Series([1,2,3,pd.NA,10], dtype=pd.Int64Dtype())
ser3 = pd.Series([1,2,3,pd.NA,10], dtype=pd.ArrowDtype(pa.int64()))
for ser in ser1, ser2, ser3:
print(f"initial dtype: {ser.dtype}")
ser_mapped = ser.map(lambda x: x)
print(f"post map dtype: {ser_mapped.dtype}")
ser_filtered = ser.dropna().map(lambda x: x)
print(f"dropna map dtype: {ser_mapped.dtype}")
ser2.map(type)
ser2.dropna().map(type)
"""
output:
for ser1:
initial dtype: Int64
post map dtype: float64
dropna map dtype: float64
for ser2:
initial dtype: Int64
post map dtype: float64
dropna map dtype: float64
for ser3:
initial dtype: int64[pyarrow]
post map dtype: float64
dropna map dtype: float64
"""
Issue Description
Using map on a Series with dtype Int64Dtype or int64[dtype] will coerce values to float if it contains any missing values.
>>> ser2.map(type)
0 <class 'float'>
1 <class 'float'>
2 <class 'float'>
3 <class 'float'>
4 <class 'float'>
dtype: object
>>> ser2.dropna().map(type)
0 <class 'int'>
1 <class 'int'>
2 <class 'int'>
4 <class 'int'>
dtype: object
Expected Behavior
Series.map() should not coerce into float64 with these dtypes.
As stated in the documentation on working with missing data:
NA for StringDtype, Int64Dtype (and other bit widths), Float64Dtype
(and other bit widths), :class:
BooleanDtype and ArrowDtype. These types will maintain the original data type of the data.
Installed Versions
pd.show_versions()
/usr/lib/python3.12/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")
INSTALLED VERSIONS
commit : db11e25
python : 3.12.1.final.0
python-bits : 64
OS : Linux
OS-release : 6.6.13-200.fc39.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Sat Jan 20 18:03:28 UTC 2024
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_IE.UTF-8
LOCALE : en_IE.UTF-8
pandas : 3.0.0.dev0+197.gdb11e25d2b
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.2.1
Cython : None
pytest : 7.4.3
hypothesis : None
sphinx : 7.2.6
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.0.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.19.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None