Skip to content

pandas.index.copy remove index name information when specifying new dtype #32013

Closed
@asepwhite

Description

@asepwhite

Code Sample, a copy-pastable example if possible

>>> import pandas as pd
>>> named_idx = pd.Index([17.3, 69.221, 33.1, 15.5, 19.3, 74.8, 10, 5.5]).set_names('old_name')
>>> named_idx
Float64Index([17.3, 69.221, 33.1, 15.5, 19.3, 74.8, 10.0, 5.5], dtype='float64', name='old_name')
>>> named_idx.copy(name='new_name', dtype='int64')
Int64Index([17, 69, 33, 15, 19, 74, 10, 5], dtype='int64')
>>> named_idx.copy(dtype='int64')
Int64Index([17, 69, 33, 15, 19, 74, 10, 5], dtype='int64')

Problem description

When set new dtype parameter in function pandas.index.copy somehow it remove name information on new object

Expected Output

import pandas as pd
named_idx = pd.Index([17.3, 69.221, 33.1, 15.5, 19.3, 74.8, 10, 5.5]).set_names('old_name')
named_idx
Float64Index([17.3, 69.221, 33.1, 15.5, 19.3, 74.8, 10.0, 5.5], dtype='float64', name='old_name')
named_idx.copy(name='new_name', dtype='int64')
Int64Index([17, 69, 33, 15, 19, 74, 10, 5], dtype='int64', name='new_name')
named_idx.copy(dtype='int64')
Int64Index([17, 69, 33, 15, 19, 74, 10, 5], dtype='int64', name='old_name')

Output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS

{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}

{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions