Skip to content

BUG: Incorrect dtypes after resetting a multi-index for a data frame with no rows #33698

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
pkarol opened this issue Apr 21, 2020 · 3 comments
Closed
2 of 3 tasks
Labels

Comments

@pkarol
Copy link

pkarol commented Apr 21, 2020

  • 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1], 'b': [pd.Timestamp('2020-04-21')]})
>>> df = df[df['a'] == 0]
>>> df
Empty DataFrame
Columns: [a, b]
Index: []

>>> df['time'] = pd.Timestamp.now()
>>> df['index'] = 0
>>> df
Empty DataFrame
Columns: [a, b, time, index]
Index: []
>>> df.dtypes
a                 int64
b        datetime64[ns]
time     datetime64[ns]
index             int64
dtype: object

>>> df = df.set_index(['time', 'index'])
>>> df.dtypes
a             int64
b    datetime64[ns]
dtype: object
>>> df.index.get_level_values(0)
DatetimeIndex([], dtype='datetime64[ns]', name='time', freq=None)
>>> df.index.get_level_values(1)
Int64Index([], dtype='int64', name='index')

>>> df.reset_index().dtypes
time            float64
index           float64
a                 int64
b        datetime64[ns]
dtype: object

Problem description

The dtypes on a data frame multi-index with no rows are lost when the index is reset. As you can see from the above sample, setting the index preserves the correct dtypes in the multi-index levels. When the index is reset, the dtypes become float64. Instead, I would expect the dtypes for time and index to be datetime64[ns] and int64 when the index is reset.

Note: The behavior is correct when there is at least one row in the data frame and this bug only occurs when the data frame has no rows.

Expected Output

time     datetime64[ns]
index             int64
a                 int64
b        datetime64[ns]
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.6.10.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.0.0-29-generic
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : C.UTF-8
LOCALE           : en_US.UTF-8
pandas           : 1.0.3
numpy            : 1.16.2
pytz             : 2019.3
dateutil         : 2.8.1
pip              : None
setuptools       : 46.1.3.post20200325
Cython           : None
pytest           : 5.4.1
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : 3.2.1
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : 0.16.0
pytables         : None
pytest           : 5.4.1
pyxlsb           : None
s3fs             : None
scipy            : 1.2.1
sqlalchemy       : 1.3.15
tables           : None
tabulate         : None
xarray           : 0.14.1
xlrd             : 1.2.0
xlwt             : None
xlsxwriter       : None
numba            : 0.48.0
@pkarol pkarol added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 21, 2020
@TomAugspurger
Copy link
Contributor

Is this the same as #19602?

@pkarol
Copy link
Author

pkarol commented Apr 21, 2020

@TomAugspurger, yes, it appears to be the same issue.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Apr 21, 2020 via email

@pkarol pkarol closed this as completed Apr 21, 2020
@bashtage bashtage removed the Needs Triage Issue that has not been reviewed by a pandas team member label Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants