-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DataFrame.stack losing EA dtypes with multi-columns and mixed dtypes #51691
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
mroeschke
merged 9 commits into
pandas-dev:main
from
lukemanley:dataframe-stack-ea-dtypes
Mar 9, 2023
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
72b6c2e
BUG: DataFrame.stack losing EA dtypes for mixed dtype dataframes
lukemanley 01500f5
add test
lukemanley 9abdf45
whatsnew
lukemanley 0d7a428
Merge remote-tracking branch 'upstream/main' into dataframe-stack-ea-…
lukemanley e41d0b7
avoid copy
lukemanley e6b725a
Merge remote-tracking branch 'upstream/main' into dataframe-stack-ea-…
lukemanley d29980f
Merge remote-tracking branch 'upstream/main' into dataframe-stack-ea-…
lukemanley 6636a74
Merge remote-tracking branch 'upstream/main' into dataframe-stack-ea-…
lukemanley c689ac6
update test
lukemanley 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 |
---|---|---|
|
@@ -1146,6 +1146,27 @@ def test_stack_multi_columns_non_unique_index(self, index, columns): | |
expected_codes = np.asarray(new_index.codes) | ||
tm.assert_numpy_array_equal(stacked_codes, expected_codes) | ||
|
||
@pytest.mark.parametrize( | ||
"vals1, vals2, dtype1, dtype2, expected_dtype", | ||
[ | ||
([1, 2], [3.0, 4.0], "Int64", "Float64", "Float64"), | ||
([1, 2], ["foo", "bar"], "Int64", "string", "object"), | ||
], | ||
) | ||
def test_stack_multi_columns_mixed_extension_types( | ||
self, vals1, vals2, dtype1, dtype2, expected_dtype | ||
): | ||
# GH45740 | ||
df = DataFrame( | ||
{ | ||
("A", 1): Series(vals1, dtype=dtype1), | ||
("A", 2): Series(vals2, dtype=dtype2), | ||
} | ||
) | ||
result = df.stack() | ||
expected = df.astype(object).stack().astype(expected_dtype) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("level", [0, 1]) | ||
def test_unstack_mixed_extension_types(self, level): | ||
index = MultiIndex.from_tuples([("A", 0), ("A", 1), ("B", 1)], names=["a", "b"]) | ||
|
@@ -2181,9 +2202,11 @@ def test_stack_nullable_dtype(self): | |
df[df.columns[0]] = df[df.columns[0]].astype(pd.Float64Dtype()) | ||
result = df.stack("station") | ||
|
||
# TODO(EA2D): we get object dtype because DataFrame.values can't | ||
# be an EA | ||
expected = df.astype(object).stack("station") | ||
expected = ( | ||
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. For this 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. sure, updated. |
||
df.astype(object) | ||
.stack("station") | ||
.astype({"r": pd.Float64Dtype(), "t_mean": pd.Int64Dtype()}) | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_unstack_mixed_level_names(self): | ||
|
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.
this.iloc[:, loc]
? i think in case where loc is a slice this avoids a copyThere 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.
updated. thanks