Skip to content

Commit 37e99cf

Browse files
committed
BUG: Handling NaN case before dtype conversion pandas-dev#40638
1 parent 568be9a commit 37e99cf

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pandas/core/arrays/numpy_.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,17 @@ def to_numpy( # type: ignore[override]
355355
copy: bool = False,
356356
na_value=lib.no_default,
357357
) -> np.ndarray:
358-
result = np.asarray(self._ndarray, dtype=dtype)
359358

360-
if (copy or na_value is not lib.no_default) and result is self._ndarray:
361-
result = result.copy()
359+
temp = self
362360

363361
if na_value is not lib.no_default:
364-
result[self.isna()] = na_value
362+
temp.fillna(None)
363+
364+
result = np.asarray(temp._ndarray, dtype=dtype)
365+
366+
if (copy or na_value is not lib.no_default) and result is temp._ndarray:
367+
result = result.copy()
368+
365369

366370
return result
367371

0 commit comments

Comments
 (0)