Skip to content

BUG: DataFrame.reset_index() discards MultiIndex dtypes if DataFrame is empty #35589

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
batterseapower opened this issue Aug 6, 2020 · 2 comments
Closed
2 of 3 tasks
Labels
Bug Duplicate Report Duplicate issue or pull request Needs Info Clarification about behavior needed to assess issue

Comments

@batterseapower
Copy link
Contributor

batterseapower commented Aug 6, 2020

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas. (tried on v1.0.5 -- the latest in Conda -- and checked the release notes for the new version to make sure they don't mention anything related)
  • (optional) I have confirmed this bug exists on the master branch of pandas.

Code Sample, a copy-pastable example

x = pd.DataFrame.from_dict({
    'A': pd.Categorical.from_codes([1], categories=['G', 'H']),
    'B': np.array([0], dtype='i8'),
})

x.dtypes # category and int64

# This is the bug:
x.iloc[:0].set_index(['A', 'B']).reset_index().dtypes # float64 and float64

# These cases all work correctly i.e. the dtypes are category and int64
x.set_index(['A', 'B']).reset_index().dtypes
x.set_index('A').reset_index().dtypes
x.iloc[:0].set_index('A').reset_index().dtypes

# This is CategoricalDtype as expected, so the MultiIndex construction is correct: it's just the `reset_index()` which is throwing away the info
x.iloc[:0].set_index(['A', 'B']).index.get_level_values('A').dtype

Problem description

reset_index shouldn't throw away dtype information in the special case where the DataFrame has zero rows. Sometimes it's hard to avoid throwing away info when you manipulate zero-element DataFrames, but in this case there is no reason to do it.

Expected Output

category and int64 dtypes from reset_index in all cases

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.936

pandas : 1.0.5
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.2.0.post20200714
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.5.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@batterseapower batterseapower added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 6, 2020
@TomAugspurger
Copy link
Contributor

This sounds like #19602. Can you try with pandas 1.1?

@TomAugspurger TomAugspurger added Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 6, 2020
@TomAugspurger
Copy link
Contributor

Just tried with master, and I see

# This is the bug:
x.iloc[:0].set_index(['A', 'B']).reset_index().dtypes # float64 and float64

## -- End pasted text --
Out[3]:
A    category
B       int64
dtype: object

so I think we're good. LMK if not and we'll reopen.

@TomAugspurger TomAugspurger added the Duplicate Report Duplicate issue or pull request label Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

2 participants