Skip to content

BUG: Handling NaN case before dtype conversion #40638 #41027

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
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,14 @@ def to_numpy( # type: ignore[override]
copy: bool = False,
na_value=lib.no_default,
) -> np.ndarray:
result = np.asarray(self._ndarray, dtype=dtype)
temp = self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason for another temporary array, can just use result


if (copy or na_value is not lib.no_default) and result is self._ndarray:
if na_value is not lib.no_default:
temp.fillna(method="bfill")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bfill? Also, this line won't have any effect anyway because inplace=False by default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And na_value is no longer being used to fill


result = np.asarray(temp._ndarray, dtype=dtype)

if (copy or na_value is not lib.no_default) and result is temp._ndarray:
result = result.copy()

if na_value is not lib.no_default:
Expand Down