4
4
import pandas as pd
5
5
import pandas ._testing as tm
6
6
from pandas .api .types import is_integer
7
- from pandas .core .arrays import IntegerArray , integer_array
7
+ from pandas .core .arrays import IntegerArray
8
8
from pandas .core .arrays .integer import Int8Dtype , Int32Dtype , Int64Dtype
9
9
10
10
11
+ @pytest .fixture (params = [pd .array , IntegerArray ._from_sequence ])
12
+ def constructor (request ):
13
+ return request .param
14
+
15
+
11
16
def test_uses_pandas_na ():
12
17
a = pd .array ([1 , None ], dtype = Int64Dtype ())
13
18
assert a [1 ] is pd .NA
@@ -65,7 +70,7 @@ def test_integer_array_constructor():
65
70
mask = np .array ([False , False , False , True ], dtype = "bool" )
66
71
67
72
result = IntegerArray (values , mask )
68
- expected = integer_array ([1 , 2 , 3 , np .nan ], dtype = "int64 " )
73
+ expected = pd . array ([1 , 2 , 3 , np .nan ], dtype = "Int64 " )
69
74
tm .assert_extension_array_equal (result , expected )
70
75
71
76
msg = r".* should be .* numpy array. Use the 'pd.array' function instead"
@@ -82,21 +87,6 @@ def test_integer_array_constructor():
82
87
IntegerArray (values )
83
88
84
89
85
- @pytest .mark .parametrize (
86
- "a, b" ,
87
- [
88
- ([1 , None ], [1 , np .nan ]),
89
- ([None ], [np .nan ]),
90
- ([None , np .nan ], [np .nan , np .nan ]),
91
- ([np .nan , np .nan ], [np .nan , np .nan ]),
92
- ],
93
- )
94
- def test_integer_array_constructor_none_is_nan (a , b ):
95
- result = integer_array (a )
96
- expected = integer_array (b )
97
- tm .assert_extension_array_equal (result , expected )
98
-
99
-
100
90
def test_integer_array_constructor_copy ():
101
91
values = np .array ([1 , 2 , 3 , 4 ], dtype = "int64" )
102
92
mask = np .array ([False , False , False , True ], dtype = "bool" )
@@ -110,6 +100,21 @@ def test_integer_array_constructor_copy():
110
100
assert result ._mask is not mask
111
101
112
102
103
+ @pytest .mark .parametrize (
104
+ "a, b" ,
105
+ [
106
+ ([1 , None ], [1 , np .nan ]),
107
+ ([None ], [np .nan ]),
108
+ ([None , np .nan ], [np .nan , np .nan ]),
109
+ ([np .nan , np .nan ], [np .nan , np .nan ]),
110
+ ],
111
+ )
112
+ def test_to_integer_array_none_is_nan (a , b ):
113
+ result = pd .array (a , dtype = "Int64" )
114
+ expected = pd .array (b , dtype = "Int64" )
115
+ tm .assert_extension_array_equal (result , expected )
116
+
117
+
113
118
@pytest .mark .parametrize (
114
119
"values" ,
115
120
[
@@ -129,42 +134,46 @@ def test_to_integer_array_error(values):
129
134
msg = (
130
135
r"(:?.* cannot be converted to an IntegerDtype)"
131
136
r"|(:?values must be a 1D list-like)"
137
+ r"|(Cannot pass scalar)"
132
138
)
139
+ with pytest .raises ((ValueError , TypeError ), match = msg ):
140
+ pd .array (values , dtype = "Int64" )
141
+
133
142
with pytest .raises (TypeError , match = msg ):
134
- integer_array (values )
143
+ IntegerArray . _from_sequence (values )
135
144
136
145
137
- def test_to_integer_array_inferred_dtype ():
146
+ def test_to_integer_array_inferred_dtype (constructor ):
138
147
# if values has dtype -> respect it
139
- result = integer_array (np .array ([1 , 2 ], dtype = "int8" ))
148
+ result = constructor (np .array ([1 , 2 ], dtype = "int8" ))
140
149
assert result .dtype == Int8Dtype ()
141
- result = integer_array (np .array ([1 , 2 ], dtype = "int32" ))
150
+ result = constructor (np .array ([1 , 2 ], dtype = "int32" ))
142
151
assert result .dtype == Int32Dtype ()
143
152
144
153
# if values have no dtype -> always int64
145
- result = integer_array ([1 , 2 ])
154
+ result = constructor ([1 , 2 ])
146
155
assert result .dtype == Int64Dtype ()
147
156
148
157
149
- def test_to_integer_array_dtype_keyword ():
150
- result = integer_array ([1 , 2 ], dtype = "int8 " )
158
+ def test_to_integer_array_dtype_keyword (constructor ):
159
+ result = constructor ([1 , 2 ], dtype = "Int8 " )
151
160
assert result .dtype == Int8Dtype ()
152
161
153
162
# if values has dtype -> override it
154
- result = integer_array (np .array ([1 , 2 ], dtype = "int8" ), dtype = "int32 " )
163
+ result = constructor (np .array ([1 , 2 ], dtype = "int8" ), dtype = "Int32 " )
155
164
assert result .dtype == Int32Dtype ()
156
165
157
166
158
167
def test_to_integer_array_float ():
159
- result = integer_array ([1.0 , 2.0 ])
160
- expected = integer_array ([1 , 2 ])
168
+ result = IntegerArray . _from_sequence ([1.0 , 2.0 ])
169
+ expected = pd . array ([1 , 2 ], dtype = "Int64" )
161
170
tm .assert_extension_array_equal (result , expected )
162
171
163
172
with pytest .raises (TypeError , match = "cannot safely cast non-equivalent" ):
164
- integer_array ([1.5 , 2.0 ])
173
+ IntegerArray . _from_sequence ([1.5 , 2.0 ])
165
174
166
175
# for float dtypes, the itemsize is not preserved
167
- result = integer_array (np .array ([1.0 , 2.0 ], dtype = "float32" ))
176
+ result = IntegerArray . _from_sequence (np .array ([1.0 , 2.0 ], dtype = "float32" ))
168
177
assert result .dtype == Int64Dtype ()
169
178
170
179
@@ -176,10 +185,12 @@ def test_to_integer_array_float():
176
185
([False , True , np .nan ], [0 , 1 , np .nan ], Int64Dtype (), Int64Dtype ()),
177
186
],
178
187
)
179
- def test_to_integer_array_bool (bool_values , int_values , target_dtype , expected_dtype ):
180
- result = integer_array (bool_values , dtype = target_dtype )
188
+ def test_to_integer_array_bool (
189
+ constructor , bool_values , int_values , target_dtype , expected_dtype
190
+ ):
191
+ result = constructor (bool_values , dtype = target_dtype )
181
192
assert result .dtype == expected_dtype
182
- expected = integer_array (int_values , dtype = target_dtype )
193
+ expected = pd . array (int_values , dtype = target_dtype )
183
194
tm .assert_extension_array_equal (result , expected )
184
195
185
196
@@ -193,7 +204,7 @@ def test_to_integer_array_bool(bool_values, int_values, target_dtype, expected_d
193
204
)
194
205
def test_to_integer_array (values , to_dtype , result_dtype ):
195
206
# convert existing arrays to IntegerArrays
196
- result = integer_array (values , dtype = to_dtype )
207
+ result = IntegerArray . _from_sequence (values , dtype = to_dtype )
197
208
assert result .dtype == result_dtype ()
198
- expected = integer_array (values , dtype = result_dtype ())
209
+ expected = pd . array (values , dtype = result_dtype ())
199
210
tm .assert_extension_array_equal (result , expected )
0 commit comments