-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: stack doesn't preserve lower precision floating dtypes #51602
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8437249
BUG: stack doesn't preserve lower precision floating dtypes
lithomas1 679c467
Merge branch 'main' of https://github.com/pandas-dev/pandas into stac…
lithomas1 397d5ce
Merge branch 'main' into stack-upcasts-nans
lithomas1 cae36be
Merge branch 'main' of https://github.com/pandas-dev/pandas into stac…
lithomas1 15eb161
simplify
lithomas1 fd68eba
Update reshape.py
lithomas1 b95a1cf
Merge branch 'main' into stack-upcasts-nans
lithomas1 6123430
Merge branch 'main' into stack-upcasts-nans
lithomas1 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -747,7 +747,18 @@ def _convert_level_number(level_num: int, columns: Index): | |
if slice_len != levsize: | ||
chunk = this.loc[:, this.columns[loc]] | ||
chunk.columns = level_vals_nan.take(chunk.columns.codes[-1]) | ||
value_slice = chunk.reindex(columns=level_vals_used).values | ||
# Override fill value to prevent upcasting to float64 | ||
# if we have lower precision floats | ||
common_type = find_common_type(chunk.dtypes.tolist()) | ||
if np.issubdtype(common_type, np.floating): | ||
fill_value = common_type.type(np.nan) | ||
elif is_extension_array_dtype(common_type): | ||
fill_value = common_type.na_value | ||
else: | ||
fill_value = np.nan | ||
value_slice = chunk.reindex( | ||
columns=level_vals_used, fill_value=fill_value | ||
).values | ||
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. This is something we'll have to figure out at some point, right now this coerces EA dtypes to object. But ok for another pr since we had this problem before as well... |
||
else: | ||
subset = this.iloc[:, loc] | ||
dtype = find_common_type(subset.dtypes.tolist()) | ||
|
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
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.
Any reason for not using common_dtype.na_value?
Right now "Float32" would add np.nan, but this should be pd.NA, Same for Int64 for example
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.
The default fill_value is np.nan for reindex, even for EAs. That's probably something to fix elsewhere, though (IIRC, the fill value gets converted somewhere in the EA methods, maybe take? It's been a while since I looked into it though).
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.
But why can't we supply the correct one?
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.
Updated.