@@ -45,39 +45,40 @@ def __init__(self, n: int):
45
45
def shape (self ):
46
46
return (self .n ,)
47
47
48
+ def to_array (self , dtype : DtypeObj , fill_value = lib .no_default ) -> ArrayLike :
49
+ """
50
+ Helper function to create the actual all-NA array from the NullArrayProxy
51
+ object.
52
+
53
+ Parameters
54
+ ----------
55
+ arr : NullArrayProxy
56
+ dtype : the dtype for the resulting array
57
+ fill_value : scalar NA-like value
58
+ By default uses the ExtensionDtype's na_value or np.nan. For numpy
59
+ arrays, this can be overridden to be something else (eg None).
60
+
61
+ Returns
62
+ -------
63
+ np.ndarray or ExtensionArray
64
+ """
65
+ if is_extension_array_dtype (dtype ):
66
+ empty = dtype .construct_array_type ()._from_sequence ([], dtype = dtype )
67
+ indexer = - np .ones (self .n , dtype = np .intp )
68
+ return empty .take (indexer , allow_fill = True )
69
+ else :
70
+ # when introducing missing values, int becomes float, bool becomes object
71
+ if is_integer_dtype (dtype ):
72
+ dtype = np .dtype ("float64" )
73
+ elif is_bool_dtype (dtype ):
74
+ dtype = np .dtype (object )
48
75
49
- def _array_from_proxy (arr , dtype : DtypeObj , fill_value = lib .no_default ):
50
- """
51
- Helper function to create the actual all-NA array from the NullArrayProxy object.
52
-
53
- Parameters
54
- ----------
55
- arr : NullArrayProxy
56
- dtype : the dtype for the resulting array
57
- fill_value : scalar NA-like value
58
- By default uses the ExtensionDtype's na_value or np.nan. For numpy
59
- arrays, this can be overridden to be something else (eg None).
60
-
61
- Returns
62
- -------
63
- np.ndarray or ExtensionArray
64
- """
65
- if is_extension_array_dtype (dtype ):
66
- return dtype .construct_array_type ()._from_sequence (
67
- [dtype .na_value ] * arr .n , dtype = dtype
68
- )
69
- else :
70
- if is_integer_dtype (dtype ):
71
- dtype = np .dtype ("float64" )
72
- elif is_bool_dtype (dtype ):
73
- dtype = np .dtype (object )
74
-
75
- if fill_value is lib .no_default :
76
- fill_value = na_value_for_dtype (dtype )
76
+ if fill_value is lib .no_default :
77
+ fill_value = na_value_for_dtype (dtype )
77
78
78
- arr = np .empty (arr .n , dtype = dtype )
79
- arr .fill (fill_value )
80
- return ensure_wrapped_if_datetimelike (arr )
79
+ arr = np .empty (self .n , dtype = dtype )
80
+ arr .fill (fill_value )
81
+ return ensure_wrapped_if_datetimelike (arr )
81
82
82
83
83
84
def _cast_to_common_type (arr : ArrayLike , dtype : DtypeObj ) -> ArrayLike :
@@ -86,7 +87,7 @@ def _cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike:
86
87
cases.
87
88
"""
88
89
if isinstance (arr , NullArrayProxy ):
89
- return _array_from_proxy ( arr , dtype )
90
+ return arr . to_array ( dtype )
90
91
91
92
if (
92
93
is_categorical_dtype (arr .dtype )
@@ -194,7 +195,7 @@ def is_nonempty(x) -> bool:
194
195
return np .concatenate (to_concat , axis = axis )
195
196
196
197
197
- def concat_arrays (to_concat ):
198
+ def concat_arrays (to_concat ) -> ArrayLike :
198
199
"""
199
200
Alternative for concat_compat but specialized for use in the ArrayManager.
200
201
@@ -225,9 +226,7 @@ def concat_arrays(to_concat):
225
226
else :
226
227
target_dtype = to_concat_no_proxy [0 ].dtype
227
228
to_concat = [
228
- _array_from_proxy (arr , target_dtype )
229
- if isinstance (arr , NullArrayProxy )
230
- else arr
229
+ arr .to_array (target_dtype ) if isinstance (arr , NullArrayProxy ) else arr
231
230
for arr in to_concat
232
231
]
233
232
@@ -247,7 +246,7 @@ def concat_arrays(to_concat):
247
246
else :
248
247
target_dtype = to_concat_no_proxy [0 ].dtype
249
248
to_concat = [
250
- _array_from_proxy ( arr , target_dtype ) if isinstance (arr , NullArrayProxy ) else arr
249
+ arr . to_array ( target_dtype ) if isinstance (arr , NullArrayProxy ) else arr
251
250
for arr in to_concat
252
251
]
253
252
@@ -462,7 +461,7 @@ def _concat_datetime(to_concat, axis=0):
462
461
# ensure_wrapped_if_datetimelike ensures that astype(object) wraps
463
462
# in Timestamp/Timedelta
464
463
to_concat = [
465
- _array_from_proxy ( arr , dtype = object , fill_value = None )
464
+ arr . to_array ( object , fill_value = None )
466
465
if isinstance (arr , NullArrayProxy )
467
466
else arr
468
467
for arr in to_concat
@@ -475,7 +474,7 @@ def _concat_datetime(to_concat, axis=0):
475
474
to_concat = [x .reshape (1 , - 1 ) if x .ndim == 1 else x for x in to_concat ]
476
475
else :
477
476
to_concat = [
478
- _array_from_proxy ( arr , dtype = to_concat_no_proxy [0 ].dtype )
477
+ arr . to_array ( to_concat_no_proxy [0 ].dtype )
479
478
if isinstance (arr , NullArrayProxy )
480
479
else arr
481
480
for arr in to_concat
0 commit comments