11
11
import pandas ._testing as tm
12
12
from pandas .api .extensions import register_extension_dtype
13
13
from pandas .api .types import is_scalar
14
+ from pandas .arrays import (
15
+ BooleanArray ,
16
+ DatetimeArray ,
17
+ IntegerArray ,
18
+ IntervalArray ,
19
+ SparseArray ,
20
+ StringArray ,
21
+ TimedeltaArray ,
22
+ )
14
23
from pandas .core .arrays import PandasArray , integer_array , period_array
15
24
from pandas .tests .extension .decimal import DecimalArray , DecimalDtype , to_decimal
16
25
19
28
"data, dtype, expected" ,
20
29
[
21
30
# Basic NumPy defaults.
22
- ([1 , 2 ], None , pd . arrays . IntegerArray ._from_sequence ([1 , 2 ])),
31
+ ([1 , 2 ], None , IntegerArray ._from_sequence ([1 , 2 ])),
23
32
([1 , 2 ], object , PandasArray (np .array ([1 , 2 ], dtype = object ))),
24
33
(
25
34
[1 , 2 ],
26
35
np .dtype ("float32" ),
27
36
PandasArray (np .array ([1.0 , 2.0 ], dtype = np .dtype ("float32" ))),
28
37
),
29
- (
30
- np .array ([1 , 2 ], dtype = "int64" ),
31
- None ,
32
- pd .arrays .IntegerArray ._from_sequence ([1 , 2 ]),
33
- ),
38
+ (np .array ([1 , 2 ], dtype = "int64" ), None , IntegerArray ._from_sequence ([1 , 2 ]),),
34
39
# String alias passes through to NumPy
35
40
([1 , 2 ], "float32" , PandasArray (np .array ([1 , 2 ], dtype = "float32" ))),
36
41
# Period alias
49
54
(
50
55
[1 , 2 ],
51
56
np .dtype ("datetime64[ns]" ),
52
- pd .arrays .DatetimeArray ._from_sequence (
53
- np .array ([1 , 2 ], dtype = "datetime64[ns]" )
54
- ),
57
+ DatetimeArray ._from_sequence (np .array ([1 , 2 ], dtype = "datetime64[ns]" )),
55
58
),
56
59
(
57
60
np .array ([1 , 2 ], dtype = "datetime64[ns]" ),
58
61
None ,
59
- pd .arrays .DatetimeArray ._from_sequence (
60
- np .array ([1 , 2 ], dtype = "datetime64[ns]" )
61
- ),
62
+ DatetimeArray ._from_sequence (np .array ([1 , 2 ], dtype = "datetime64[ns]" )),
62
63
),
63
64
(
64
65
pd .DatetimeIndex (["2000" , "2001" ]),
65
66
np .dtype ("datetime64[ns]" ),
66
- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
67
+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
67
68
),
68
69
(
69
70
pd .DatetimeIndex (["2000" , "2001" ]),
70
71
None ,
71
- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
72
+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
72
73
),
73
74
(
74
75
["2000" , "2001" ],
75
76
np .dtype ("datetime64[ns]" ),
76
- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
77
+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
77
78
),
78
79
# Datetime (tz-aware)
79
80
(
80
81
["2000" , "2001" ],
81
82
pd .DatetimeTZDtype (tz = "CET" ),
82
- pd . arrays . DatetimeArray ._from_sequence (
83
+ DatetimeArray ._from_sequence (
83
84
["2000" , "2001" ], dtype = pd .DatetimeTZDtype (tz = "CET" )
84
85
),
85
86
),
86
87
# Timedelta
87
88
(
88
89
["1H" , "2H" ],
89
90
np .dtype ("timedelta64[ns]" ),
90
- pd . arrays . TimedeltaArray ._from_sequence (["1H" , "2H" ]),
91
+ TimedeltaArray ._from_sequence (["1H" , "2H" ]),
91
92
),
92
93
(
93
94
pd .TimedeltaIndex (["1H" , "2H" ]),
94
95
np .dtype ("timedelta64[ns]" ),
95
- pd . arrays . TimedeltaArray ._from_sequence (["1H" , "2H" ]),
96
+ TimedeltaArray ._from_sequence (["1H" , "2H" ]),
96
97
),
97
98
(
98
99
pd .TimedeltaIndex (["1H" , "2H" ]),
99
100
None ,
100
- pd . arrays . TimedeltaArray ._from_sequence (["1H" , "2H" ]),
101
+ TimedeltaArray ._from_sequence (["1H" , "2H" ]),
101
102
),
102
103
# Category
103
104
(["a" , "b" ], "category" , pd .Categorical (["a" , "b" ])),
110
111
(
111
112
[pd .Interval (1 , 2 ), pd .Interval (3 , 4 )],
112
113
"interval" ,
113
- pd . arrays . IntervalArray .from_tuples ([(1 , 2 ), (3 , 4 )]),
114
+ IntervalArray .from_tuples ([(1 , 2 ), (3 , 4 )]),
114
115
),
115
116
# Sparse
116
- ([0 , 1 ], "Sparse[int64]" , pd . arrays . SparseArray ([0 , 1 ], dtype = "int64" )),
117
+ ([0 , 1 ], "Sparse[int64]" , SparseArray ([0 , 1 ], dtype = "int64" )),
117
118
# IntegerNA
118
119
([1 , None ], "Int16" , integer_array ([1 , None ], dtype = "Int16" )),
119
120
(pd .Series ([1 , 2 ]), None , PandasArray (np .array ([1 , 2 ], dtype = np .int64 ))),
120
121
# String
121
- (["a" , None ], "string" , pd .arrays .StringArray ._from_sequence (["a" , None ])),
122
- (
123
- ["a" , None ],
124
- pd .StringDtype (),
125
- pd .arrays .StringArray ._from_sequence (["a" , None ]),
126
- ),
122
+ (["a" , None ], "string" , StringArray ._from_sequence (["a" , None ])),
123
+ (["a" , None ], pd .StringDtype (), StringArray ._from_sequence (["a" , None ]),),
127
124
# Boolean
128
- ([True , None ], "boolean" , pd .arrays .BooleanArray ._from_sequence ([True , None ])),
129
- (
130
- [True , None ],
131
- pd .BooleanDtype (),
132
- pd .arrays .BooleanArray ._from_sequence ([True , None ]),
133
- ),
125
+ ([True , None ], "boolean" , BooleanArray ._from_sequence ([True , None ])),
126
+ ([True , None ], pd .BooleanDtype (), BooleanArray ._from_sequence ([True , None ]),),
134
127
# Index
135
128
(pd .Index ([1 , 2 ]), None , PandasArray (np .array ([1 , 2 ], dtype = np .int64 ))),
136
129
# Series[EA] returns the EA
@@ -181,31 +174,28 @@ def test_array_copy():
181
174
period_array (["2000" , "2001" ], freq = "D" ),
182
175
),
183
176
# interval
184
- (
185
- [pd .Interval (0 , 1 ), pd .Interval (1 , 2 )],
186
- pd .arrays .IntervalArray .from_breaks ([0 , 1 , 2 ]),
187
- ),
177
+ ([pd .Interval (0 , 1 ), pd .Interval (1 , 2 )], IntervalArray .from_breaks ([0 , 1 , 2 ]),),
188
178
# datetime
189
179
(
190
180
[pd .Timestamp ("2000" ), pd .Timestamp ("2001" )],
191
- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
181
+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
192
182
),
193
183
(
194
184
[datetime .datetime (2000 , 1 , 1 ), datetime .datetime (2001 , 1 , 1 )],
195
- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
185
+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
196
186
),
197
187
(
198
188
np .array ([1 , 2 ], dtype = "M8[ns]" ),
199
- pd . arrays . DatetimeArray (np .array ([1 , 2 ], dtype = "M8[ns]" )),
189
+ DatetimeArray (np .array ([1 , 2 ], dtype = "M8[ns]" )),
200
190
),
201
191
(
202
192
np .array ([1 , 2 ], dtype = "M8[us]" ),
203
- pd . arrays . DatetimeArray (np .array ([1000 , 2000 ], dtype = "M8[ns]" )),
193
+ DatetimeArray (np .array ([1000 , 2000 ], dtype = "M8[ns]" )),
204
194
),
205
195
# datetimetz
206
196
(
207
197
[pd .Timestamp ("2000" , tz = "CET" ), pd .Timestamp ("2001" , tz = "CET" )],
208
- pd . arrays . DatetimeArray ._from_sequence (
198
+ DatetimeArray ._from_sequence (
209
199
["2000" , "2001" ], dtype = pd .DatetimeTZDtype (tz = "CET" )
210
200
),
211
201
),
@@ -214,30 +204,30 @@ def test_array_copy():
214
204
datetime .datetime (2000 , 1 , 1 , tzinfo = cet ),
215
205
datetime .datetime (2001 , 1 , 1 , tzinfo = cet ),
216
206
],
217
- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ], tz = cet ),
207
+ DatetimeArray ._from_sequence (["2000" , "2001" ], tz = cet ),
218
208
),
219
209
# timedelta
220
210
(
221
211
[pd .Timedelta ("1H" ), pd .Timedelta ("2H" )],
222
- pd . arrays . TimedeltaArray ._from_sequence (["1H" , "2H" ]),
212
+ TimedeltaArray ._from_sequence (["1H" , "2H" ]),
223
213
),
224
214
(
225
215
np .array ([1 , 2 ], dtype = "m8[ns]" ),
226
- pd . arrays . TimedeltaArray (np .array ([1 , 2 ], dtype = "m8[ns]" )),
216
+ TimedeltaArray (np .array ([1 , 2 ], dtype = "m8[ns]" )),
227
217
),
228
218
(
229
219
np .array ([1 , 2 ], dtype = "m8[us]" ),
230
- pd . arrays . TimedeltaArray (np .array ([1000 , 2000 ], dtype = "m8[ns]" )),
220
+ TimedeltaArray (np .array ([1000 , 2000 ], dtype = "m8[ns]" )),
231
221
),
232
222
# integer
233
- ([1 , 2 ], pd . arrays . IntegerArray ._from_sequence ([1 , 2 ])),
234
- ([1 , None ], pd . arrays . IntegerArray ._from_sequence ([1 , None ])),
223
+ ([1 , 2 ], IntegerArray ._from_sequence ([1 , 2 ])),
224
+ ([1 , None ], IntegerArray ._from_sequence ([1 , None ])),
235
225
# string
236
- (["a" , "b" ], pd . arrays . StringArray ._from_sequence (["a" , "b" ])),
237
- (["a" , None ], pd . arrays . StringArray ._from_sequence (["a" , None ])),
226
+ (["a" , "b" ], StringArray ._from_sequence (["a" , "b" ])),
227
+ (["a" , None ], StringArray ._from_sequence (["a" , None ])),
238
228
# Boolean
239
- ([True , False ], pd . arrays . BooleanArray ._from_sequence ([True , False ])),
240
- ([True , None ], pd . arrays . BooleanArray ._from_sequence ([True , None ])),
229
+ ([True , False ], BooleanArray ._from_sequence ([True , False ])),
230
+ ([True , None ], BooleanArray ._from_sequence ([True , None ])),
241
231
],
242
232
)
243
233
def test_array_inference (data , expected ):
0 commit comments