-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: in DataFrame.reset_index() only call maybe_upcast_putmask with ndarrays #36876
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
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fee7b1a
BUG: add check so maybe_upcast_putmask is only called with ndarray
arw2019 00e1c28
TST: add tests
arw2019 05c4c1e
DOC: add whatsnew
arw2019 f70368b
feedback: test roundtrip
arw2019 50dbb93
feedback: parametrize on both examples from OP
arw2019 17e73a5
move test to frame/test_alter_axes.py
arw2019 f55846f
fill mask in index with nan when not calling maybe_upcast_putmask
arw2019 c8174be
Merge branch 'master' into GH24206
arw2019 8d1df1f
restore the fix
arw2019 1242f4b
Merge remote-tracking branch 'upstream/master' into GH24206
arw2019 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,12 @@ | |
|
||
from pandas import ( | ||
Categorical, | ||
CategoricalIndex, | ||
DataFrame, | ||
DatetimeIndex, | ||
Index, | ||
IntervalIndex, | ||
MultiIndex, | ||
Series, | ||
Timestamp, | ||
cut, | ||
|
@@ -171,21 +173,6 @@ def test_assign_columns(self, float_frame): | |
tm.assert_series_equal(float_frame["C"], df["baz"], check_names=False) | ||
tm.assert_series_equal(float_frame["hi"], df["foo2"], check_names=False) | ||
|
||
def test_set_index_preserve_categorical_dtype(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this to sit with the only other categorical test (the one I'm adding) in |
||
# GH13743, GH13854 | ||
df = DataFrame( | ||
{ | ||
"A": [1, 2, 1, 1, 2], | ||
"B": [10, 16, 22, 28, 34], | ||
"C1": Categorical(list("abaab"), categories=list("bac"), ordered=False), | ||
"C2": Categorical(list("abaab"), categories=list("bac"), ordered=True), | ||
} | ||
) | ||
for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]: | ||
result = df.set_index(cols).reset_index() | ||
result = result.reindex(columns=df.columns) | ||
tm.assert_frame_equal(result, df) | ||
|
||
def test_rename_signature(self): | ||
sig = inspect.signature(DataFrame.rename) | ||
parameters = set(sig.parameters) | ||
|
@@ -266,3 +253,47 @@ def test_set_reset_index(self): | |
df = df.set_index("B") | ||
|
||
df = df.reset_index() | ||
|
||
|
||
class TestCategoricalIndex: | ||
def test_set_index_preserve_categorical_dtype(self): | ||
# GH13743, GH13854 | ||
df = DataFrame( | ||
{ | ||
"A": [1, 2, 1, 1, 2], | ||
"B": [10, 16, 22, 28, 34], | ||
"C1": Categorical(list("abaab"), categories=list("bac"), ordered=False), | ||
"C2": Categorical(list("abaab"), categories=list("bac"), ordered=True), | ||
} | ||
) | ||
for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]: | ||
result = df.set_index(cols).reset_index() | ||
result = result.reindex(columns=df.columns) | ||
tm.assert_frame_equal(result, df) | ||
|
||
@pytest.mark.parametrize( | ||
"codes", ([[0, 0, 1, 1], [0, 1, 0, 1]], [[0, 0, -1, 1], [0, 1, 0, 1]]) | ||
) | ||
def test_reindexing_with_missing_values(self, codes): | ||
# GH 24206 | ||
|
||
index = MultiIndex( | ||
[CategoricalIndex(["A", "B"]), CategoricalIndex(["a", "b"])], codes | ||
) | ||
data = {"col": range(len(index))} | ||
df = DataFrame(data=data, index=index) | ||
|
||
expected = DataFrame( | ||
{ | ||
"level_0": Categorical.from_codes(codes[0], categories=["A", "B"]), | ||
"level_1": Categorical.from_codes(codes[1], categories=["a", "b"]), | ||
"col": range(4), | ||
} | ||
) | ||
|
||
res = df.reset_index() | ||
tm.assert_frame_equal(res, expected) | ||
|
||
# roundtrip | ||
res = expected.set_index(["level_0", "level_1"]).reset_index() | ||
tm.assert_frame_equal(res, expected) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback added this since you last looked - we handle
nan
inmaybe_upcast_mask
if values is anndarray
else fill withnp.nan