Skip to content

BUG: Index.insert should coerce to the appropriate type #16277

@jreback

Description

@jreback

xref to #14535 (PR to fix, but stale)

# NG, must be float dtype
pd.Index([1]).insert(1, 1.0)
# Int64Index([1, 1], dtype='int64')

# NG, must be object dtype
pd.DatetimeIndex(['2011-01-01']).insert(1, 1)
# TypeError: cannot insert DatetimeIndex with incompatible label

Expected

pd.Index([1]).insert(1, 1.0)
# Float64Index([1.0, 1.0], dtype='float64')

pd.DatetimeIndex(['2011-01-01']).insert(1, 1)
# Index([2011-01-01 00:00:00, 1], dtype='object')

Metadata

Metadata

Assignees

No one assigned

    Labels

    Dtype ConversionsUnexpected or buggy dtype conversionsReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions