49
49
validate_insert_loc ,
50
50
)
51
51
52
- from pandas .core .dtypes .astype import (
53
- astype_array ,
54
- astype_nansafe ,
55
- )
52
+ from pandas .core .dtypes .astype import astype_array
56
53
from pandas .core .dtypes .cast import (
57
54
construct_1d_arraylike_from_scalar ,
58
55
find_common_type ,
@@ -445,7 +442,7 @@ def __init__(
445
442
# NumPy may raise a ValueError on data like [1, []]
446
443
# we retry with object dtype here.
447
444
if dtype is None :
448
- dtype = object
445
+ dtype = np . dtype ( object )
449
446
data = np .atleast_1d (np .asarray (data , dtype = dtype ))
450
447
else :
451
448
raise
@@ -464,10 +461,7 @@ def __init__(
464
461
if isinstance (data , type (self )) and sparse_index is None :
465
462
sparse_index = data ._sparse_index
466
463
# error: Argument "dtype" to "asarray" has incompatible type
467
- # "Union[ExtensionDtype, dtype[Any], Type[object], None]"; expected
468
- # "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any, int],
469
- # Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any,
470
- # Any]]]"
464
+ # "Union[ExtensionDtype, dtype[Any], None]"; expected "None"
471
465
sparse_values = np .asarray (
472
466
data .sp_values , dtype = dtype # type: ignore[arg-type]
473
467
)
@@ -487,21 +481,18 @@ def __init__(
487
481
if fill_value is NaT :
488
482
fill_value = np .datetime64 ("NaT" , "ns" )
489
483
data = np .asarray (data )
490
- sparse_values , sparse_index , fill_value = make_sparse (
491
- # error: Argument "dtype" to "make_sparse " has incompatible type
492
- # "Union[ExtensionDtype, dtype[Any], Type[object], None]"; expected
493
- # "Union[str, dtype[Any], None ]"
484
+ sparse_values , sparse_index , fill_value = _make_sparse (
485
+ # error: Argument "dtype" to "_make_sparse " has incompatible type
486
+ # "Union[ExtensionDtype, dtype[Any], None]"; expected
487
+ # "Optional[ dtype[Any]]"
494
488
data ,
495
489
kind = kind ,
496
490
fill_value = fill_value ,
497
491
dtype = dtype , # type: ignore[arg-type]
498
492
)
499
493
else :
500
494
# error: Argument "dtype" to "asarray" has incompatible type
501
- # "Union[ExtensionDtype, dtype[Any], Type[object], None]"; expected
502
- # "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any, int],
503
- # Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any,
504
- # Any]]]"
495
+ # "Union[ExtensionDtype, dtype[Any], None]"; expected "None"
505
496
sparse_values = np .asarray (data , dtype = dtype ) # type: ignore[arg-type]
506
497
if len (sparse_values ) != sparse_index .npoints :
507
498
raise AssertionError (
@@ -1288,7 +1279,9 @@ def astype(self, dtype: AstypeArg | None = None, copy: bool = True):
1288
1279
dtype = self .dtype .update_dtype (dtype )
1289
1280
subtype = pandas_dtype (dtype ._subtype_with_str )
1290
1281
subtype = cast (np .dtype , subtype ) # ensured by update_dtype
1291
- sp_values = astype_nansafe (self .sp_values , subtype , copy = copy )
1282
+ values = ensure_wrapped_if_datetimelike (self .sp_values )
1283
+ sp_values = astype_array (values , subtype , copy = copy )
1284
+ sp_values = np .asarray (sp_values )
1292
1285
1293
1286
return self ._simple_new (sp_values , self .sp_index , dtype )
1294
1287
@@ -1828,11 +1821,11 @@ def _formatter(self, boxed: bool = False):
1828
1821
return None
1829
1822
1830
1823
1831
- def make_sparse (
1824
+ def _make_sparse (
1832
1825
arr : np .ndarray ,
1833
1826
kind : SparseIndexKind = "block" ,
1834
1827
fill_value = None ,
1835
- dtype : NpDtype | None = None ,
1828
+ dtype : np . dtype | None = None ,
1836
1829
):
1837
1830
"""
1838
1831
Convert ndarray to sparse format
@@ -1882,7 +1875,10 @@ def make_sparse(
1882
1875
index = make_sparse_index (length , indices , kind )
1883
1876
sparsified_values = arr [mask ]
1884
1877
if dtype is not None :
1885
- sparsified_values = astype_nansafe (sparsified_values , dtype = pandas_dtype (dtype ))
1878
+ sparsified_values = ensure_wrapped_if_datetimelike (sparsified_values )
1879
+ sparsified_values = astype_array (sparsified_values , dtype = dtype )
1880
+ sparsified_values = np .asarray (sparsified_values )
1881
+
1886
1882
# TODO: copy
1887
1883
return sparsified_values , index , fill_value
1888
1884
0 commit comments