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/API: preserve dtype in Index append() #60159

Open
jorisvandenbossche opened this issue Oct 31, 2024 · 3 comments
Open

BUG/API: preserve dtype in Index append() #60159

jorisvandenbossche opened this issue Oct 31, 2024 · 3 comments
Labels
Dtype Conversions Unexpected or buggy dtype conversions Index Related to the Index class or subclasses Strings String extension data type and string data
Milestone

Comments

@jorisvandenbossche
Copy link
Member

If you combine two Index objects using append, if they are both object dtype to start with, it seems we still infer the dtype for the result instead of preserving object dtype:

idx1 = pd.Index(["b", "a"], dtype=object)
idx2 = pd.Index(["c", "d"], dtype=object)

>>> idx1.append(idx2)
Index(['b', 'a', 'c', 'd'], dtype='object')

>>> pd.options.future.infer_string = True
>>> idx1.append(idx2)
Index(['b', 'a', 'c', 'd'], dtype='str')    # <-- upcast to string

I would expect that to preserve the dtype of the calling / passed Index (or at least its "common" dtype, which in this case clearly is object dtype)

@jorisvandenbossche jorisvandenbossche added Dtype Conversions Unexpected or buggy dtype conversions Strings String extension data type and string data Index Related to the Index class or subclasses labels Oct 31, 2024
@jorisvandenbossche jorisvandenbossche added this to the 3.0 milestone Oct 31, 2024
@rhshadrach
Copy link
Member

Hopefully a rare case, but what about the behavior of:

idx1 = pd.Index([1, 2], dtype=object)
idx2 = pd.Index([3, 4])
print(idx1.append(idx2))

I would think this should also be object, as otherwise we get values-dependent behavior.

@Swati-Sneha
Copy link
Contributor

take

@Swati-Sneha Swati-Sneha removed their assignment Nov 5, 2024
@jorisvandenbossche
Copy link
Member Author

I would think this should also be object, as otherwise we get values-dependent behavior.

Indeed, I would also expect object dtype in that case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Index Related to the Index class or subclasses Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

3 participants