Skip to content

Commit 694849d

Browse files
BUG: astype_unicode astype_str turn a np.nan to empty string (pandas-dev#20377)
1 parent ddb904f commit 694849d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pandas/_libs/lib.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,8 @@ cpdef ndarray[object] astype_unicode(ndarray arr):
465465
for i in range(n):
466466
# we can use the unsafe version because we know `result` is mutable
467467
# since it was created from `np.empty`
468-
util.set_value_at_unsafe(result, i, unicode(arr[i]))
468+
arr_i = arr[i]
469+
util.set_value_at_unsafe(result, i, unicode(arr_i) if arr_i is not np.nan else '')
469470

470471
return result
471472

@@ -478,7 +479,8 @@ cpdef ndarray[object] astype_str(ndarray arr):
478479
for i in range(n):
479480
# we can use the unsafe version because we know `result` is mutable
480481
# since it was created from `np.empty`
481-
util.set_value_at_unsafe(result, i, str(arr[i]))
482+
arr_i = arr[i]
483+
util.set_value_at_unsafe(result, i, str(arr_i) if arr_i is not np.nan else '')
482484

483485
return result
484486

0 commit comments

Comments
 (0)