From 6c801a6445a193e6de6c203012dcbc96d5406026 Mon Sep 17 00:00:00 2001 From: Sudomarko Date: Sun, 18 Apr 2021 20:59:57 -0400 Subject: [PATCH 1/4] BUG: Handling NaN case before dtype conversion #40638 --- pandas/core/arrays/numpy_.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index 45656459792ba..ee04aa17b571d 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -355,13 +355,19 @@ def to_numpy( # type: ignore[override] copy: bool = False, na_value=lib.no_default, ) -> np.ndarray: - result = np.asarray(self._ndarray, dtype=dtype) - if (copy or na_value is not lib.no_default) and result is self._ndarray: - result = result.copy() + temp = self if na_value is not lib.no_default: - result[self.isna()] = na_value + temp = self.fillna(None) + + + + 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() + return result From 6716b65c6f06b3691c9e6430a033d56f74466dbe Mon Sep 17 00:00:00 2001 From: Sudomarko Date: Sun, 18 Apr 2021 22:59:47 -0400 Subject: [PATCH 2/4] BUG: adding bfill method to fillna --- pandas/core/arrays/numpy_.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index ee04aa17b571d..e4f2fefa53bc4 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -355,20 +355,16 @@ def to_numpy( # type: ignore[override] copy: bool = False, na_value=lib.no_default, ) -> np.ndarray: - temp = self if na_value is not lib.no_default: - temp = self.fillna(None) - - + temp.fillna(method='bfill') 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() - return result # ------------------------------------------------------------------------ From ca53bfc2dc9d8fbd75511bffb2339550d61c5ceb Mon Sep 17 00:00:00 2001 From: Sudomarko Date: Sun, 18 Apr 2021 23:10:02 -0400 Subject: [PATCH 3/4] Adding proper quotemarks --- pandas/core/arrays/numpy_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index e4f2fefa53bc4..70b80210f4090 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -358,7 +358,7 @@ def to_numpy( # type: ignore[override] temp = self if na_value is not lib.no_default: - temp.fillna(method='bfill') + temp.fillna(method="bfill") result = np.asarray(temp._ndarray, dtype=dtype) From d0fba53a49c977e5c184e16a05d17aa2f196d63d Mon Sep 17 00:00:00 2001 From: Sudomarko Date: Sun, 18 Apr 2021 23:58:50 -0400 Subject: [PATCH 4/4] Restoring na_values after conversion --- pandas/core/arrays/numpy_.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index 70b80210f4090..f23ab6b45b024 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -365,6 +365,9 @@ def to_numpy( # type: ignore[override] 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: + result[self.isna()] = na_value + return result # ------------------------------------------------------------------------