-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
BUG: fillna with DataFrame input should preserve dtype when possible #61742
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
base: main
Are you sure you want to change the base?
Conversation
Since we now operate column-wise and use This also preserves extension dtypes like Let me know if this behavior change is fine, happy to update the test or tweak the logic based on what’s preferred! |
# restore original dtype if fallback to object occurred | ||
if lhs.dtype == rhs.dtype and filled.dtype == object: | ||
try: | ||
filled = filled.astype(lhs.dtype) |
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.
id expect this to be handled by Series.where. is it not?
@@ -7145,7 +7145,24 @@ def fillna( | |||
else: | |||
new_data = self._mgr.fillna(value=value, limit=limit, inplace=inplace) | |||
elif isinstance(value, ABCDataFrame) and self.ndim == 2: | |||
new_data = self.where(self.notna(), value)._mgr | |||
filled_columns = {} | |||
for col in 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.
doing this column-by-column is going to mean a performance hit for non-object cases. i suspect we need to do this at the Block level in order to avoid that
This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this. |
When filling a DataFrame with another DataFrame using
fillna
, columns with matching dtypes were being unnecessarily cast toobject
due to use ofnp.where
.This PR updates the logic to use pandas’
Series.where
, which is dtype-safe and respects extension and datetime types.