Skip to content

BUG: DataFrame.mode index dtype is not type stable #33321

Closed
@TomAugspurger

Description

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


Code Sample, a copy-pastable example

The .index.dtype is not stable for DataFrame.mode. It depends on whether the DataFrame is empty and possible the dtypes

In [60]: pd.DataFrame([], columns=['a', 'b']).mode().index.dtype
Out[60]: dtype('O')

Problem description

The index dtype should always be Int64, to match the non-empty case

Expected Output

In [61]: pd.DataFrame({"A": ['a']}).mode().index.dtype
Out[61]: dtype('int64')

Metadata

Metadata

Assignees

No one assigned

    Labels

    AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffBugDtype ConversionsUnexpected or buggy dtype conversions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions