fix: handle non-numpy dtypes in PandasIndex.concat() and join()#11409
Open
C1-BA-B1-F3 wants to merge 3 commits into
Open
fix: handle non-numpy dtypes in PandasIndex.concat() and join()#11409C1-BA-B1-F3 wants to merge 3 commits into
C1-BA-B1-F3 wants to merge 3 commits into
Conversation
Problem: When a MultiIndex level contains tuple-valued entries (e.g., (1,1)), selecting with a nested tuple key like ((1,1), 2) incorrectly preserved the dimension instead of collapsing it to a scalar result. Root cause: _is_nested_tuple() was checking for 'tuple' in addition to 'list' and 'slice', which caused it to misidentify tuple-valued keys as nested selection tuples. Fix: Remove 'tuple' from the isinstance check in _is_nested_tuple() so that only 'list' and 'slice' are treated as indicators of nested selections. Tuple- valued keys in MultiIndex levels are now correctly handled as scalar key values. Added regression test for selecting with nested tuple keys on MultiIndex with tuple-valued levels.
When concatenating indexes with mixed string types (e.g., numpy string dtype and pandas StringDtype), np.result_type() fails because it cannot interpret extension dtypes. This fix checks if all dtypes are valid numpy dtypes before calling np.result_type(), falling back to object dtype if not. Fixes GH#11317
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes an issue where
concatfails when mixing string coordinates from different sources (e.g., numpy string dtype and pandas StringDtype).Problem
When concatenating DataArrays where one has a numpy string dtype coordinate and another has a pandas StringDtype coordinate (introduced by pd.Index in pandas 3.0), np.result_type() fails with:
TypeError: Cannot interpret '' as a data type
Root Cause
In PandasIndex.concat() and PandasIndex.join(), np.result_type() is called with coordinate dtypes without checking if they are valid numpy dtypes first. Pandas extension dtypes (like StringDtype) are not valid numpy dtypes.
Fix
Fixes GH#11317
Tests