28
28
from pandas ._typing import Dtype
29
29
30
30
from pandas .core .dtypes .common import (
31
- is_datetime64_dtype ,
32
- is_datetime64tz_dtype ,
33
31
is_float_dtype ,
34
32
is_integer_dtype ,
35
- is_period_dtype ,
36
33
is_sequence ,
37
- is_timedelta64_dtype ,
38
34
is_unsigned_integer_dtype ,
39
35
pandas_dtype ,
40
36
)
41
- from pandas .core .dtypes .dtypes import IntervalDtype
42
37
43
38
import pandas as pd
44
39
from pandas import (
112
107
)
113
108
from pandas .core .arrays import (
114
109
BaseMaskedArray ,
115
- DatetimeArray ,
116
110
ExtensionArray ,
117
111
PandasArray ,
118
- PeriodArray ,
119
- TimedeltaArray ,
120
- period_array ,
121
112
)
122
113
from pandas .core .arrays ._mixins import NDArrayBackedExtensionArray
114
+ from pandas .core .construction import extract_array
123
115
124
116
if TYPE_CHECKING :
125
117
from pandas import (
161
153
+ BYTES_DTYPES
162
154
)
163
155
156
+ NARROW_NP_DTYPES = [
157
+ np .float16 ,
158
+ np .float32 ,
159
+ np .int8 ,
160
+ np .int16 ,
161
+ np .int32 ,
162
+ np .uint8 ,
163
+ np .uint16 ,
164
+ np .uint32 ,
165
+ ]
166
+
164
167
NULL_OBJECTS = [None , np .nan , pd .NaT , float ("nan" ), pd .NA , Decimal ("NaN" )]
165
168
NP_NAT_OBJECTS = [
166
169
cls ("NaT" , unit )
@@ -257,13 +260,6 @@ def box_expected(expected, box_cls, transpose=True):
257
260
# single-row special cases in datetime arithmetic
258
261
expected = expected .T
259
262
expected = pd .concat ([expected ] * 2 , ignore_index = True )
260
- elif box_cls is PeriodArray :
261
- # the PeriodArray constructor is not as flexible as period_array
262
- expected = period_array (expected )
263
- elif box_cls is DatetimeArray :
264
- expected = DatetimeArray (expected )
265
- elif box_cls is TimedeltaArray :
266
- expected = TimedeltaArray (expected )
267
263
elif box_cls is np .ndarray or box_cls is np .array :
268
264
expected = np .array (expected )
269
265
elif box_cls is to_array :
@@ -274,21 +270,16 @@ def box_expected(expected, box_cls, transpose=True):
274
270
275
271
276
272
def to_array (obj ):
273
+ """
274
+ Similar to pd.array, but does not cast numpy dtypes to nullable dtypes.
275
+ """
277
276
# temporary implementation until we get pd.array in place
278
277
dtype = getattr (obj , "dtype" , None )
279
278
280
- if is_period_dtype (dtype ):
281
- return period_array (obj )
282
- elif is_datetime64_dtype (dtype ) or is_datetime64tz_dtype (dtype ):
283
- return DatetimeArray ._from_sequence (obj )
284
- elif is_timedelta64_dtype (dtype ):
285
- return TimedeltaArray ._from_sequence (obj )
286
- elif isinstance (obj , pd .core .arrays .BooleanArray ):
287
- return obj
288
- elif isinstance (dtype , IntervalDtype ):
289
- return pd .core .arrays .IntervalArray (obj )
290
- else :
291
- return np .array (obj )
279
+ if dtype is None :
280
+ return np .asarray (obj )
281
+
282
+ return extract_array (obj , extract_numpy = True )
292
283
293
284
294
285
# -----------------------------------------------------------------------------
0 commit comments