-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: conversion of empty DataFrame to SparseDtype (#33113) #33118
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
Changes from 12 commits
6ac82f8
e76f1fc
8a96a47
a4aad14
d42709b
708fdc7
5b1fe9f
738739e
a9e7e15
9c7f06b
49280cb
369735b
f0f099c
dcdf11f
e2d4241
7bc6255
95fd399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5548,6 +5548,9 @@ def astype( | |
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,) | ||
return self._constructor(new_data).__finalize__(self, method="astype") | ||
|
||
if not results: | ||
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. maybe this should be in 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. although then the issue mentioned in #33113 (comment) would not be fixed. maybe keep this where it is and a test for #33113 (comment) 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. Let's try! 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. Sorry, did not see your second comment. Ok I'll do that. Anyhow if we reach the "concat" part of the code and 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. Although, one could argue that 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. @simonjayhawkins Actually starting to think the other issue was not an issue but an error on the author's part. See #33113 (comment). Awaiting your answer to proceed. 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 think the other issue is relevant. on master and in this PR (latest commit)...
this PR before moving #33118 (comment) ...
so it appears astype with an empty dict is a valid op with a copy returned. so this should also work on an empty DataFrame (even if created with empty dict). these sort of circumstances can arise on data pipelines and having consistency reduces the complexity of downstream code. so I think need to move the changes back and add a test 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. Ok I will revert the changes back and add a test. I still find it very strange to call 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. DataFrame.astype can also accept a dict of column name -> data type as the dtype argument, https://pandas.pydata.org/docs/dev/reference/api/pandas.DataFrame.astype.html 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. Ahhh that makes a lot of sense now. |
||
return self.copy() | ||
|
||
# GH 19920: retain column metadata after concat | ||
result = pd.concat(results, axis=1, copy=False) | ||
result.columns = self.columns | ||
|
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.
IIUC this fix is only for DataFrame. Series.astype works fine on master?
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.
Good point. I tried and
Series.astype
works fine onmaster
. Will fix the entry.