Skip to content

Commit 7a05f16

Browse files
committed
fix conversion of nan to string
1 parent 76b5764 commit 7a05f16

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

pandas/_libs/lib.pyx

+10-8
Original file line numberDiff line numberDiff line change
@@ -651,27 +651,29 @@ cpdef ndarray[object] ensure_string_array(
651651
cdef:
652652
Py_ssize_t i = 0, n = len(arr)
653653

654-
arr = np.asarray(arr) # PERF: need a numpy array to ensure fast access
655654
result = np.asarray(arr, dtype="object")
656655

657656
if copy and result is arr:
658657
result = result.copy()
659658

659+
arr = np.asarray(arr) # PERF: need a numpy array to ensure fast access
660+
660661
for i in range(n):
661-
val = arr[i]
662+
arr_val = arr[i]
663+
res_val = result[i]
662664

663-
if isinstance(val, str):
665+
if not checknull(res_val) and isinstance(arr_val, str):
664666
continue
665667

666-
if not checknull(val):
667-
result[i] = str(val)
668+
if not checknull(res_val):
669+
result[i] = str(arr_val)
668670
else:
669671
if convert_na_value:
670-
val = na_value
672+
arr_val = na_value
671673
if skipna:
672-
result[i] = val
674+
result[i] = arr_val
673675
else:
674-
result[i] = str(val)
676+
result[i] = str(arr_val)
675677

676678
return result
677679

0 commit comments

Comments
 (0)