Skip to content

DatetimeIndex is converted to Index upon renaming of the columns #26248

@KatemaNL

Description

@KatemaNL

Code Sample, a copy-pastable example if possible

import pandas as pd
import pytz

df = pd.DataFrame(data={'metric': [1, 2, 3, 4, 5, 6]},
                  index=pd.DatetimeIndex(['2007-07-13 02:00', '2007-07-13 03:00', '2007-07-13 04:00',
                                          '2007-07-13 05:00', '2007-07-13 06:00', '2007-07-13 07:00'],
                                         freq='H', tz=pytz.utc))
print(f'Index before renaming:\n{df.index}') # Gives DatetimeIndex with timezone UTC and frequency H
df.rename(index=str, columns={'metric': 'metric_renamed'}, inplace=True)
print(f'Index after renaming:\n{df.index}') # Gives Index without timezone and frequency

Problem description

The class of the dataframe index changes (from DatetimeIndex to Index) and timezone and frequency information is lost, where only the names of the columns should have changed.

HINT:
Caused by _transform_index in pandas/core/internals/managers.py
If the index is not a MultiIndex, an Index is returned

Note: Pandas is upgraded to latest version (0.24.2)

Expected Output

The index of the dataframe should be left untouched.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-47-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
pandas: 0.24.2
pytest: None
pip: 19.0.1
setuptools: 40.6.2
Cython: None
numpy: 1.16.0
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.4.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions