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 · 2 comments
Open
2 of 3 tasks

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

colinfang opened this issue Jan 22, 2025 · 2 comments
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
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

No branches or pull requests

3 participants