Skip to content

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

Open
@jorisvandenbossche

Description

@jorisvandenbossche

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Dtype ConversionsUnexpected or buggy dtype conversionsIndexRelated to the Index class or subclassesStringsString extension data type and string data

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions