File tree 3 files changed +13
-8
lines changed
3 files changed +13
-8
lines changed Original file line number Diff line number Diff line change @@ -385,6 +385,9 @@ def time_to_numpy_double_copy(self):
385
385
def time_to_numpy_copy (self ):
386
386
self .ser .to_numpy (copy = True )
387
387
388
+ def time_to_numpy_float_with_nan (self ):
389
+ self .ser .to_numpy (dtype = "float64" , na_value = np .nan )
390
+
388
391
389
392
class Replace :
390
393
param_names = ["num_to_replace" ]
Original file line number Diff line number Diff line change @@ -195,6 +195,7 @@ Performance improvements
195
195
- Performance improvement in :meth: `MultiIndex.set_levels ` and :meth: `MultiIndex.set_codes ` when ``verify_integrity=True `` (:issue: `51873 `)
196
196
- Performance improvement in :func: `factorize ` for object columns not containing strings (:issue: `51921 `)
197
197
- Performance improvement in :class: `Series ` reductions (:issue: `52341 `)
198
+ - Performance improvement in :meth: `Series.to_numpy ` when dtype is a numpy float dtype and ``na_value `` is ``np.nan `` (:issue: `52430 `)
198
199
-
199
200
200
201
.. ---------------------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -573,25 +573,26 @@ def to_numpy(
573
573
f"to_numpy() got an unexpected keyword argument '{ bad_keys } '"
574
574
)
575
575
576
- if na_value is not lib .no_default :
577
- values = self ._values
576
+ fillna = (
577
+ na_value is not lib .no_default
578
+ # no need to fillna with np.nan if we already have a float dtype
579
+ and not (na_value is np .nan and np .issubdtype (self .dtype , np .floating ))
580
+ )
581
+
582
+ values = self ._values
583
+ if fillna :
578
584
if not can_hold_element (values , na_value ):
579
585
# if we can't hold the na_value asarray either makes a copy or we
580
586
# error before modifying values. The asarray later on thus won't make
581
587
# another copy
582
588
values = np .asarray (values , dtype = dtype )
583
589
else :
584
590
values = values .copy ()
585
-
586
591
values [np .asanyarray (self .isna ())] = na_value
587
- else :
588
- values = self ._values
589
592
590
593
result = np .asarray (values , dtype = dtype )
591
594
592
- if (copy and na_value is lib .no_default ) or (
593
- not copy and using_copy_on_write ()
594
- ):
595
+ if (copy and not fillna ) or (not copy and using_copy_on_write ()):
595
596
if np .shares_memory (self ._values [:2 ], result [:2 ]):
596
597
# Take slices to improve performance of check
597
598
if using_copy_on_write () and not copy :
You can’t perform that action at this time.
0 commit comments