Skip to content

BUG: Series.map() coerces Int64Dtype and int64[pyarrow] series which contain missing values to float64 #57189

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
weltenseglr opened this issue Jan 31, 2024 · 6 comments
Assignees
Labels
Arrow pyarrow functionality Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@weltenseglr
Copy link

weltenseglr commented Jan 31, 2024

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

@weltenseglr weltenseglr added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 31, 2024
@mroeschke mroeschke added NA - MaskedArrays Related to pd.NA and nullable extension arrays Arrow pyarrow functionality and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 1, 2024
@remiBoudreau
Copy link

take

@rohanjain101
Copy link
Contributor

#56606

I had the same issue, based on the issue above, it seemed to be intentional change in 2.2.X

@remiBoudreau
Copy link

@rohanjain101 Yeah you're right. I believe it changed from v2.1.x

return map_array(self, mapper, na_action=na_action)

to

def map(self, mapper, na_action=None):
return map_array(self.to_numpy(), mapper, na_action=None)

def map(self, mapper, na_action=None):
if is_numeric_dtype(self.dtype):
return map_array(self.to_numpy(), mapper, na_action=None)
else:
return super().map(mapper, na_action)

in v2.2.0. Guess you might have to cast it back to your wanted dtype @weltenseglr

@weltenseglr
Copy link
Author

Thanks for your feedback, @rohanjain101 @remiBoudreau. I think the documentation is outdated then?
Series.map() does not warn about this matter and missing data states that

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.

Casting back won't help in my case, as my mapper must not receive coerced values...

I'm trying to figure out how to maintain the original data type. Any ideas?

@rohanjain101
Copy link
Contributor

@weltenseglr You could use the workaround described in #56606 (comment)

Using the python map operator instead of Series.map. Atleast for now, this preserves the original type.

@weltenseglr
Copy link
Author

thanks, @rohanjain101.

I guess I will have to implement a workaround based on your suggestion or revert to a 2.1 release.
However, it still feels wrong that pandas' map casts nullable numerical dtypes to float64.

Is this going to change in pandas 3.0? I think this doesn't play well with the project's intentions regarding PDEP-10 and its benefits?

5j9 added a commit to 5j9/iranetf that referenced this issue May 24, 2024
More appropriate, also there is a bug in map/apply
that converts ints to floats and thus making URLs
invalid.
pandas-dev/pandas#57189
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

No branches or pull requests

4 participants