Skip to content

BUG: DatetimeIndex gets converted to Index when operating on transpose of empty Dataframe #41382

Closed
@vishwa-raj

Description

@vishwa-raj
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


#------ pandas 1.2.* ------

import pandas as pd
df = pd.DataFrame(index = pd.DatetimeIndex([]))

print(df.T.sum().index) # output: Index([], dtype='object')
print(df.sum(axis = 1).index) # output: DatetimeIndex([], dtype='datetime64[ns]', freq=None)

Problem description

In pandas versions >= 1.2, if we take the sum of the transpose of an empty DataFrame with DatetimeIndex, it results in an output with normal Index instead of DatetimeIndex.

But if we sum the DataFrame with axis=1, which is an equivalent operation, the DatetimeIndex is preserved.

Expected Output

In pandas versions <= 1.1, the output correctly has a DatetimeIndex in both cases.

#------ pandas 1.1.* ------

import pandas as pd
df = pd.DataFrame(index = pd.DatetimeIndex([]))

print(df.T.sum().index) # output: DatetimeIndex([], dtype='datetime64[ns]', freq=None)
print(df.sum(axis = 1).index) # output: DatetimeIndex([], dtype='datetime64[ns]', freq=None)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.7.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.2.4
numpy : 1.19.5
pytz : 2019.3
dateutil : 2.8.1
pip : 21.1.1
setuptools : 56.1.0
Cython : 0.29.16
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.0
IPython : 7.23.1
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 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