Skip to content
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

BUG: .map & .apply would convert element type for extension array. #60766

Open
2 of 3 tasks
colinfang opened this issue Jan 22, 2025 · 5 comments
Open
2 of 3 tasks

BUG: .map & .apply would convert element type for extension array. #60766

colinfang opened this issue Jan 22, 2025 · 5 comments
Assignees
Labels
Apply Apply, Aggregate, Transform, Map Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@colinfang
Copy link

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

s = pd.Series([1, 2, None], dtype="Int32")
s.apply(lambda x: print(x))
s.map(lambda x: print(x))
# print 1.0 2.0 nan instead of 1 2 <NA>

Issue Description

s has dtype Int32Dtype which can encode nullable integers

However, when we use .apply(f) or .map(f), the element passed to f becomes float, and pd.NA becomes np.nan

Expected Behavior

f should receive unconverted value, as if we are doing f(s.iloc[0]), f(s.iloc[1]), ...

Installed Versions

tested with Pandas 2.2.3

@colinfang colinfang added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 22, 2025
@rhshadrach
Copy link
Member

rhshadrach commented Jan 22, 2025

Thanks for the report! This is due to #55058, but I don't see much discussion there nor issues that change closes. It does seem surprising to me that one gets np.nan here. If we remove the method on BaseMaskedArray entirely, I see expected behavior (at least in this case). That includes the use of apply (which uses map internally).

cc @WillAyd @mroeschke @phofl

@rhshadrach rhshadrach added ExtensionArray Extending pandas with custom dtypes or arrays. NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Discussion Requires discussion from core team before further action Apply Apply, Aggregate, Transform, Map and removed ExtensionArray Extending pandas with custom dtypes or arrays. Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 22, 2025
@WillAyd
Copy link
Member

WillAyd commented Jan 22, 2025

Agreed this looks strange - the function should be receiving the arguments as integers / pd.NA

@rhshadrach rhshadrach removed the Needs Discussion Requires discussion from core team before further action label Jan 22, 2025
@Gnot727 Gnot727 mentioned this issue Feb 17, 2025
5 tasks
@KevsterAmp
Copy link
Contributor

take

@KevsterAmp
Copy link
Contributor

@rhshadrach - What method are you referring on BaseMaskedArray to be removed? Thanks

@rhshadrach
Copy link
Member

map

pedromfdiogo added a commit to pedromfdiogo/pandas that referenced this issue Mar 28, 2025
…tension array.

The Int32Dtype type allows representing integers with support for null values (pd.NA). However, when using .map(f) or .apply(f), the elements passed to f are converted to float64, and pd.NA is transformed into np.nan.

This happens because .map() and .apply() internally use numpy, which automatically converts the data to float64, even when the original type is Int32Dtype.

The fix (just remove the method to_numpy()) ensures that when using .map() or .apply(), the elements in the series retain their original type (Int32, Float64, boolean, etc.), preventing unnecessary conversions to float64 and ensuring that pd.NA remains correctly handled.
pedromfdiogo added a commit to pedromfdiogo/pandas that referenced this issue Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants