Skip to content

BUG: Ensure series/frame mode() keeps int index #38732

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

Merged
merged 5 commits into from
Dec 29, 2020

Conversation

mzeitlin11
Copy link
Member

if data.empty:
data.index = Index([], dtype="int64")

return data
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty result case doesn't hit algorithms.mode, so those changes aren't enough to ensure correct dtype

df = DataFrame([], columns=["a", "b"])
result = df.mode()
expected = DataFrame([], columns=["a", "b"], index=Index([], dtype="int64"))
tm.assert_frame_equal(result, expected)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't find any DataFrame.mode testing - guessing it relies on correctness from Series.mode() testing (in pandas/tests/test_algos.py::TestMode). However, empty result case doesn't hit Series.mode, so some DataFrame testing seems necessary. Is there a better place for this test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We keep them in pandas/tests/frame/test_reductions.py

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks! Sorry for missing that

@mzeitlin11 mzeitlin11 added Dtype Conversions Unexpected or buggy dtype conversions Bug labels Dec 27, 2020
df = DataFrame([], columns=["a", "b"])
result = df.mode()
expected = DataFrame([], columns=["a", "b"], index=Index([], dtype="int64"))
tm.assert_frame_equal(result, expected)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We keep them in pandas/tests/frame/test_reductions.py

data = data.apply(f, axis=axis)
# Ensure index is type stable (should always use int index)
if data.empty:
data.index = Index([], dtype="int64")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this right for 32-bit machines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to int, thanks for catching that

@@ -954,7 +954,9 @@ def mode(values, dropna: bool = True) -> Series:
warn(f"Unable to sort modes: {err}")

result = _reconstruct_data(result, original.dtype, original)
return Series(result)
# Ensure index is type stable (should always use int index)
index = None if len(result) else Index([], dtype=int)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should use ibase.default_index instead of manual construction

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jreback jreback added this to the 1.3 milestone Dec 29, 2020
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm ping on green

@mzeitlin11
Copy link
Member Author

green

@jreback jreback merged commit 508d0d2 into pandas-dev:master Dec 29, 2020
@jreback
Copy link
Contributor

jreback commented Dec 29, 2020

thanks @mzeitlin11

@mzeitlin11 mzeitlin11 deleted the bug/frame_mode_dtype branch December 29, 2020 20:00
luckyvs1 pushed a commit to luckyvs1/pandas that referenced this pull request Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: DataFrame.mode index dtype is not type stable
3 participants