Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
vishwa-raj opened this issue May 8, 2021 · 3 comments · Fixed by #41436
Closed
2 of 3 tasks
Labels
Datetime Datetime data dtype good first issue Needs Tests Unit test(s) needed to prevent regressions

Comments

@vishwa-raj
Copy link

  • 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

@vishwa-raj vishwa-raj added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 8, 2021
@phofl
Copy link
Member

phofl commented May 8, 2021

This is fixed on master, may need tests

DatetimeIndex([], dtype='datetime64[ns]', freq=None)

@phofl phofl added good first issue Needs Tests Unit test(s) needed to prevent regressions Datetime Datetime data dtype and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 8, 2021
@phofl
Copy link
Member

phofl commented May 8, 2021

Fixed in 48a5853

@vishwa-raj
Copy link
Author

@phofl Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants