7
7
8
8
import pandas as pd
9
9
from pandas .api .extensions import register_extension_dtype
10
- from pandas .core .arrays import integer_array , period_array
10
+ from pandas .core .arrays import PandasArray , integer_array , period_array
11
11
from pandas .tests .extension .decimal import (
12
12
DecimalArray , DecimalDtype , to_decimal )
13
13
import pandas .util .testing as tm
14
14
15
15
16
16
@pytest .mark .parametrize ("data, dtype, expected" , [
17
17
# Basic NumPy defaults.
18
- ([1 , 2 ], None , np .array ([1 , 2 ])),
19
- ([1 , 2 ], object , np .array ([1 , 2 ], dtype = object )),
18
+ ([1 , 2 ], None , PandasArray ( np .array ([1 , 2 ]) )),
19
+ ([1 , 2 ], object , PandasArray ( np .array ([1 , 2 ], dtype = object ) )),
20
20
([1 , 2 ], np .dtype ('float32' ),
21
- np .array ([1. , 2.0 ], dtype = np .dtype ('float32' ))),
22
- (np .array ([1 , 2 ]), None , np .array ([1 , 2 ])),
21
+ PandasArray ( np .array ([1. , 2.0 ], dtype = np .dtype ('float32' ) ))),
22
+ (np .array ([1 , 2 ]), None , PandasArray ( np .array ([1 , 2 ]) )),
23
23
24
24
# String alias passes through to NumPy
25
- ([1 , 2 ], 'float32' , np .array ([1 , 2 ], dtype = 'float32' )),
25
+ ([1 , 2 ], 'float32' , PandasArray ( np .array ([1 , 2 ], dtype = 'float32' ) )),
26
26
27
27
# Period alias
28
28
([pd .Period ('2000' , 'D' ), pd .Period ('2001' , 'D' )], 'Period[D]' ,
34
34
35
35
# Datetime (naive)
36
36
([1 , 2 ], np .dtype ('datetime64[ns]' ),
37
- np .array ([1 , 2 ], dtype = 'datetime64[ns]' )),
37
+ PandasArray ( np .array ([1 , 2 ], dtype = 'datetime64[ns]' ) )),
38
38
# TODO(DatetimeArray): add here
39
39
40
40
# Category
51
51
52
52
# IntegerNA
53
53
([1 , None ], 'Int16' , integer_array ([1 , None ], dtype = 'Int16' )),
54
- (pd .Series ([1 , 2 ]), None , np .array ([1 , 2 ], dtype = np .int64 )),
54
+ (pd .Series ([1 , 2 ]), None , PandasArray ( np .array ([1 , 2 ], dtype = np .int64 ) )),
55
55
56
56
# Index
57
- (pd .Index ([1 , 2 ]), None , np .array ([1 , 2 ], dtype = np .int64 )),
57
+ (pd .Index ([1 , 2 ]), None , PandasArray ( np .array ([1 , 2 ], dtype = np .int64 ) )),
58
58
59
59
# Series[EA] returns the EA
60
60
(pd .Series (pd .Categorical (['a' , 'b' ], categories = ['a' , 'b' , 'c' ])),
64
64
# "3rd party" EAs work
65
65
([decimal .Decimal (0 ), decimal .Decimal (1 )], 'decimal' , to_decimal ([0 , 1 ])),
66
66
67
- # 2D ndarrays pass through
68
- (np .array ([[1 , 2 ], [3 , 4 ]]), None , np .array ([[1 , 2 ], [3 , 4 ]])),
69
- ([[1 , 2 ], [3 , 4 ]], None , np .array ([[1 , 2 , ], [3 , 4 ]])),
70
-
71
67
# pass an ExtensionArray, but a different dtype
72
68
(period_array (['2000' , '2001' ], freq = 'D' ),
73
69
'category' ,
@@ -82,15 +78,15 @@ def test_array_copy():
82
78
a = np .array ([1 , 2 ])
83
79
# default is to copy
84
80
b = pd .array (a )
85
- assert np .shares_memory (a , b ) is False
81
+ assert np .shares_memory (a , b . _ndarray ) is False
86
82
87
83
# copy=True
88
84
b = pd .array (a , copy = True )
89
- assert np .shares_memory (a , b ) is False
85
+ assert np .shares_memory (a , b . _ndarray ) is False
90
86
91
87
# copy=False
92
88
b = pd .array (a , copy = False )
93
- assert a is b
89
+ assert np . shares_memory ( a , b . _ndarray ) is True
94
90
95
91
96
92
@pytest .mark .parametrize ('data, expected' , [
@@ -112,10 +108,24 @@ def test_array_inference(data, expected):
112
108
])
113
109
def test_array_inference_fails (data ):
114
110
result = pd .array (data )
115
- expected = np .array (data , dtype = object )
116
- tm .assert_numpy_array_equal (result , expected )
111
+ expected = PandasArray (np .array (data , dtype = object ))
112
+ tm .assert_extension_array_equal (result , expected )
113
+
114
+
115
+ @pytest .mark .parametrize ("data" , [
116
+ np .array ([[1 , 2 ], [3 , 4 ]]),
117
+ [[1 , 2 ], [3 , 4 ]],
118
+ ])
119
+ def test_nd_raises (data ):
120
+ with pytest .raises (ValueError , match = 'PandasArray must be 1-dimensional' ):
121
+ pd .array (data )
117
122
118
123
124
+ def test_scalar_raises ():
125
+ with pytest .raises (ValueError ,
126
+ match = "Cannot pass scalar '1'" ):
127
+ pd .array (1 )
128
+
119
129
# ---------------------------------------------------------------------------
120
130
# A couple dummy classes to ensure that Series and Indexes are unboxed before
121
131
# getting to the EA classes.
@@ -169,9 +179,3 @@ def test_array_not_registered(registry_without_decimal):
169
179
result = pd .array (data , dtype = DecimalDtype )
170
180
expected = DecimalArray ._from_sequence (data )
171
181
tm .assert_equal (result , expected )
172
-
173
-
174
- def test_scalar_raises ():
175
- with pytest .raises (ValueError ,
176
- match = "Cannot pass scalar '1'" ):
177
- pd .array (1 )
0 commit comments