Skip to content

df.astype(dict) drops 'names' on a multiindex #27795

Closed
@tunnij

Description

@tunnij

Code Sample, a copy-pastable example if possible

import pandas as pd
df = pd.DataFrame(
    data=[1., 2., 3.],
    columns=pd.MultiIndex.from_tuples(
        [('A', 'X')],
        names=['Top', 'Bottom'],
    )
)
df2 = df.astype({('A', 'X'): 'int'})
print(df.columns.names)
print(df2.columns.names)

Problem description

df2 should have the same column.names as df. What actually happens is that the column.names become [None, None].

If instead the definition of df2 becomes:

df2 = df.astype('int')

Then the expected behavior is observed.

Expected Output

['Top', 'Bottom']

Output of pd.show_versions()

[paste the output of ``pd.show_versions()`` here below this line] INSTALLED VERSIONS ------------------ commit: None python: 2.7.15.final.0 python-bits: 64 OS: Linux OS-release: 3.10.0-327.36.3.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.22.0
pytest: 4.6.2
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.28.5
numpy: 1.14.5
scipy: 1.0.1
pyarrow: None
xarray: 0.11.3
IPython: 5.8.0
sphinx: 1.8.5
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.9
feather: None
matplotlib: 2.2.3
openpyxl: 2.6.2
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.8
lxml: 4.2.4
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.3.5
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions