1111import pandas ._testing as tm
1212from pandas .api .extensions import register_extension_dtype
1313from 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+ )
1423from pandas .core .arrays import PandasArray , integer_array , period_array
1524from pandas .tests .extension .decimal import DecimalArray , DecimalDtype , to_decimal
1625
1928 "data, dtype, expected" ,
2029 [
2130 # Basic NumPy defaults.
22- ([1 , 2 ], None , pd . arrays . IntegerArray ._from_sequence ([1 , 2 ])),
31+ ([1 , 2 ], None , IntegerArray ._from_sequence ([1 , 2 ])),
2332 ([1 , 2 ], object , PandasArray (np .array ([1 , 2 ], dtype = object ))),
2433 (
2534 [1 , 2 ],
2635 np .dtype ("float32" ),
2736 PandasArray (np .array ([1.0 , 2.0 ], dtype = np .dtype ("float32" ))),
2837 ),
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 ]),),
3439 # String alias passes through to NumPy
3540 ([1 , 2 ], "float32" , PandasArray (np .array ([1 , 2 ], dtype = "float32" ))),
3641 # Period alias
4954 (
5055 [1 , 2 ],
5156 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]" )),
5558 ),
5659 (
5760 np .array ([1 , 2 ], dtype = "datetime64[ns]" ),
5861 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]" )),
6263 ),
6364 (
6465 pd .DatetimeIndex (["2000" , "2001" ]),
6566 np .dtype ("datetime64[ns]" ),
66- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
67+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
6768 ),
6869 (
6970 pd .DatetimeIndex (["2000" , "2001" ]),
7071 None ,
71- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
72+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
7273 ),
7374 (
7475 ["2000" , "2001" ],
7576 np .dtype ("datetime64[ns]" ),
76- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
77+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
7778 ),
7879 # Datetime (tz-aware)
7980 (
8081 ["2000" , "2001" ],
8182 pd .DatetimeTZDtype (tz = "CET" ),
82- pd . arrays . DatetimeArray ._from_sequence (
83+ DatetimeArray ._from_sequence (
8384 ["2000" , "2001" ], dtype = pd .DatetimeTZDtype (tz = "CET" )
8485 ),
8586 ),
8687 # Timedelta
8788 (
8889 ["1H" , "2H" ],
8990 np .dtype ("timedelta64[ns]" ),
90- pd . arrays . TimedeltaArray ._from_sequence (["1H" , "2H" ]),
91+ TimedeltaArray ._from_sequence (["1H" , "2H" ]),
9192 ),
9293 (
9394 pd .TimedeltaIndex (["1H" , "2H" ]),
9495 np .dtype ("timedelta64[ns]" ),
95- pd . arrays . TimedeltaArray ._from_sequence (["1H" , "2H" ]),
96+ TimedeltaArray ._from_sequence (["1H" , "2H" ]),
9697 ),
9798 (
9899 pd .TimedeltaIndex (["1H" , "2H" ]),
99100 None ,
100- pd . arrays . TimedeltaArray ._from_sequence (["1H" , "2H" ]),
101+ TimedeltaArray ._from_sequence (["1H" , "2H" ]),
101102 ),
102103 # Category
103104 (["a" , "b" ], "category" , pd .Categorical (["a" , "b" ])),
110111 (
111112 [pd .Interval (1 , 2 ), pd .Interval (3 , 4 )],
112113 "interval" ,
113- pd . arrays . IntervalArray .from_tuples ([(1 , 2 ), (3 , 4 )]),
114+ IntervalArray .from_tuples ([(1 , 2 ), (3 , 4 )]),
114115 ),
115116 # Sparse
116- ([0 , 1 ], "Sparse[int64]" , pd . arrays . SparseArray ([0 , 1 ], dtype = "int64" )),
117+ ([0 , 1 ], "Sparse[int64]" , SparseArray ([0 , 1 ], dtype = "int64" )),
117118 # IntegerNA
118119 ([1 , None ], "Int16" , integer_array ([1 , None ], dtype = "Int16" )),
119120 (pd .Series ([1 , 2 ]), None , PandasArray (np .array ([1 , 2 ], dtype = np .int64 ))),
120121 # 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 ]),),
127124 # 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 ]),),
134127 # Index
135128 (pd .Index ([1 , 2 ]), None , PandasArray (np .array ([1 , 2 ], dtype = np .int64 ))),
136129 # Series[EA] returns the EA
@@ -181,31 +174,28 @@ def test_array_copy():
181174 period_array (["2000" , "2001" ], freq = "D" ),
182175 ),
183176 # 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 ]),),
188178 # datetime
189179 (
190180 [pd .Timestamp ("2000" ), pd .Timestamp ("2001" )],
191- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
181+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
192182 ),
193183 (
194184 [datetime .datetime (2000 , 1 , 1 ), datetime .datetime (2001 , 1 , 1 )],
195- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ]),
185+ DatetimeArray ._from_sequence (["2000" , "2001" ]),
196186 ),
197187 (
198188 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]" )),
200190 ),
201191 (
202192 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]" )),
204194 ),
205195 # datetimetz
206196 (
207197 [pd .Timestamp ("2000" , tz = "CET" ), pd .Timestamp ("2001" , tz = "CET" )],
208- pd . arrays . DatetimeArray ._from_sequence (
198+ DatetimeArray ._from_sequence (
209199 ["2000" , "2001" ], dtype = pd .DatetimeTZDtype (tz = "CET" )
210200 ),
211201 ),
@@ -214,30 +204,30 @@ def test_array_copy():
214204 datetime .datetime (2000 , 1 , 1 , tzinfo = cet ),
215205 datetime .datetime (2001 , 1 , 1 , tzinfo = cet ),
216206 ],
217- pd . arrays . DatetimeArray ._from_sequence (["2000" , "2001" ], tz = cet ),
207+ DatetimeArray ._from_sequence (["2000" , "2001" ], tz = cet ),
218208 ),
219209 # timedelta
220210 (
221211 [pd .Timedelta ("1H" ), pd .Timedelta ("2H" )],
222- pd . arrays . TimedeltaArray ._from_sequence (["1H" , "2H" ]),
212+ TimedeltaArray ._from_sequence (["1H" , "2H" ]),
223213 ),
224214 (
225215 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]" )),
227217 ),
228218 (
229219 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]" )),
231221 ),
232222 # 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 ])),
235225 # 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 ])),
238228 # 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 ])),
241231 ],
242232)
243233def test_array_inference (data , expected ):
0 commit comments