Skip to content

Commit e561c7d

Browse files
jorisvandenbosscheluckyvs1
authored andcommitted
CLN: remove internal usage of integer_array() (pandas-dev#38289)
1 parent 85d6cf4 commit e561c7d

17 files changed

+105
-124
lines changed

pandas/core/arrays/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pandas.core.arrays.categorical import Categorical
88
from pandas.core.arrays.datetimes import DatetimeArray
99
from pandas.core.arrays.floating import FloatingArray
10-
from pandas.core.arrays.integer import IntegerArray, integer_array
10+
from pandas.core.arrays.integer import IntegerArray
1111
from pandas.core.arrays.interval import IntervalArray
1212
from pandas.core.arrays.masked import BaseMaskedArray
1313
from pandas.core.arrays.numpy_ import PandasArray, PandasDtype
@@ -26,7 +26,6 @@
2626
"DatetimeArray",
2727
"FloatingArray",
2828
"IntegerArray",
29-
"integer_array",
3029
"IntervalArray",
3130
"PandasArray",
3231
"PandasDtype",

pandas/core/arrays/integer.py

+2-24
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,6 @@ def __from_arrow__(
121121
return IntegerArray._concat_same_type(results)
122122

123123

124-
def integer_array(values, dtype=None, copy: bool = False) -> "IntegerArray":
125-
"""
126-
Infer and return an integer array of the values.
127-
128-
Parameters
129-
----------
130-
values : 1D list-like
131-
dtype : dtype, optional
132-
dtype to coerce
133-
copy : bool, default False
134-
135-
Returns
136-
-------
137-
IntegerArray
138-
139-
Raises
140-
------
141-
TypeError if incompatible types
142-
"""
143-
values, mask = coerce_to_array(values, dtype=dtype, copy=copy)
144-
return IntegerArray(values, mask)
145-
146-
147124
def safe_cast(values, dtype, copy: bool):
148125
"""
149126
Safely cast the values to the dtype if they
@@ -360,7 +337,8 @@ def __abs__(self):
360337
def _from_sequence(
361338
cls, scalars, *, dtype=None, copy: bool = False
362339
) -> "IntegerArray":
363-
return integer_array(scalars, dtype=dtype, copy=copy)
340+
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
341+
return IntegerArray(values, mask)
364342

365343
@classmethod
366344
def _from_sequence_of_strings(

pandas/tests/arrays/integer/conftest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas.core.arrays import integer_array
4+
import pandas as pd
55
from pandas.core.arrays.integer import (
66
Int8Dtype,
77
Int16Dtype,
@@ -32,15 +32,15 @@ def dtype(request):
3232

3333
@pytest.fixture
3434
def data(dtype):
35-
return integer_array(
35+
return pd.array(
3636
list(range(8)) + [np.nan] + list(range(10, 98)) + [np.nan] + [99, 100],
3737
dtype=dtype,
3838
)
3939

4040

4141
@pytest.fixture
4242
def data_missing(dtype):
43-
return integer_array([np.nan, 1], dtype=dtype)
43+
return pd.array([np.nan, 1], dtype=dtype)
4444

4545

4646
@pytest.fixture(params=["data", "data_missing"])

pandas/tests/arrays/integer/test_arithmetic.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pandas as pd
99
import pandas._testing as tm
10-
from pandas.core.arrays import FloatingArray, integer_array
10+
from pandas.core.arrays import FloatingArray
1111
import pandas.core.ops as ops
1212

1313
# Basic test for the arithmetic array ops
@@ -131,10 +131,10 @@ def test_pow_scalar():
131131

132132

133133
def test_pow_array():
134-
a = integer_array([0, 0, 0, 1, 1, 1, None, None, None])
135-
b = integer_array([0, 1, None, 0, 1, None, 0, 1, None])
134+
a = pd.array([0, 0, 0, 1, 1, 1, None, None, None])
135+
b = pd.array([0, 1, None, 0, 1, None, 0, 1, None])
136136
result = a ** b
137-
expected = integer_array([1, 0, None, 1, 1, 1, 1, None, None])
137+
expected = pd.array([1, 0, None, 1, 1, 1, 1, None, None])
138138
tm.assert_extension_array_equal(result, expected)
139139

140140

@@ -149,7 +149,7 @@ def test_rpow_one_to_na():
149149

150150
@pytest.mark.parametrize("other", [0, 0.5])
151151
def test_numpy_zero_dim_ndarray(other):
152-
arr = integer_array([1, None, 2])
152+
arr = pd.array([1, None, 2])
153153
result = arr + np.array(other)
154154
expected = arr + other
155155
tm.assert_equal(result, expected)
@@ -265,7 +265,7 @@ def test_reduce_to_float(op):
265265
{
266266
"A": ["a", "b", "b"],
267267
"B": [1, None, 3],
268-
"C": integer_array([1, None, 3], dtype="Int64"),
268+
"C": pd.array([1, None, 3], dtype="Int64"),
269269
}
270270
)
271271

@@ -277,10 +277,7 @@ def test_reduce_to_float(op):
277277
result = getattr(df.groupby("A"), op)()
278278

279279
expected = pd.DataFrame(
280-
{
281-
"B": np.array([1.0, 3.0]),
282-
"C": pd.array([1, 3], dtype="Float64"),
283-
},
280+
{"B": np.array([1.0, 3.0]), "C": pd.array([1, 3], dtype="Float64")},
284281
index=pd.Index(["a", "b"], name="A"),
285282
)
286283
tm.assert_frame_equal(result, expected)

pandas/tests/arrays/integer/test_construction.py

+45-34
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
import pandas as pd
55
import pandas._testing as tm
66
from pandas.api.types import is_integer
7-
from pandas.core.arrays import IntegerArray, integer_array
7+
from pandas.core.arrays import IntegerArray
88
from pandas.core.arrays.integer import Int8Dtype, Int32Dtype, Int64Dtype
99

1010

11+
@pytest.fixture(params=[pd.array, IntegerArray._from_sequence])
12+
def constructor(request):
13+
return request.param
14+
15+
1116
def test_uses_pandas_na():
1217
a = pd.array([1, None], dtype=Int64Dtype())
1318
assert a[1] is pd.NA
@@ -65,7 +70,7 @@ def test_integer_array_constructor():
6570
mask = np.array([False, False, False, True], dtype="bool")
6671

6772
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")
6974
tm.assert_extension_array_equal(result, expected)
7075

7176
msg = r".* should be .* numpy array. Use the 'pd.array' function instead"
@@ -82,21 +87,6 @@ def test_integer_array_constructor():
8287
IntegerArray(values)
8388

8489

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-
10090
def test_integer_array_constructor_copy():
10191
values = np.array([1, 2, 3, 4], dtype="int64")
10292
mask = np.array([False, False, False, True], dtype="bool")
@@ -110,6 +100,21 @@ def test_integer_array_constructor_copy():
110100
assert result._mask is not mask
111101

112102

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+
113118
@pytest.mark.parametrize(
114119
"values",
115120
[
@@ -129,42 +134,46 @@ def test_to_integer_array_error(values):
129134
msg = (
130135
r"(:?.* cannot be converted to an IntegerDtype)"
131136
r"|(:?values must be a 1D list-like)"
137+
r"|(Cannot pass scalar)"
132138
)
139+
with pytest.raises((ValueError, TypeError), match=msg):
140+
pd.array(values, dtype="Int64")
141+
133142
with pytest.raises(TypeError, match=msg):
134-
integer_array(values)
143+
IntegerArray._from_sequence(values)
135144

136145

137-
def test_to_integer_array_inferred_dtype():
146+
def test_to_integer_array_inferred_dtype(constructor):
138147
# 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"))
140149
assert result.dtype == Int8Dtype()
141-
result = integer_array(np.array([1, 2], dtype="int32"))
150+
result = constructor(np.array([1, 2], dtype="int32"))
142151
assert result.dtype == Int32Dtype()
143152

144153
# if values have no dtype -> always int64
145-
result = integer_array([1, 2])
154+
result = constructor([1, 2])
146155
assert result.dtype == Int64Dtype()
147156

148157

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")
151160
assert result.dtype == Int8Dtype()
152161

153162
# 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")
155164
assert result.dtype == Int32Dtype()
156165

157166

158167
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")
161170
tm.assert_extension_array_equal(result, expected)
162171

163172
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])
165174

166175
# 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"))
168177
assert result.dtype == Int64Dtype()
169178

170179

@@ -176,10 +185,12 @@ def test_to_integer_array_float():
176185
([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype()),
177186
],
178187
)
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)
181192
assert result.dtype == expected_dtype
182-
expected = integer_array(int_values, dtype=target_dtype)
193+
expected = pd.array(int_values, dtype=target_dtype)
183194
tm.assert_extension_array_equal(result, expected)
184195

185196

@@ -193,7 +204,7 @@ def test_to_integer_array_bool(bool_values, int_values, target_dtype, expected_d
193204
)
194205
def test_to_integer_array(values, to_dtype, result_dtype):
195206
# convert existing arrays to IntegerArrays
196-
result = integer_array(values, dtype=to_dtype)
207+
result = IntegerArray._from_sequence(values, dtype=to_dtype)
197208
assert result.dtype == result_dtype()
198-
expected = integer_array(values, dtype=result_dtype())
209+
expected = pd.array(values, dtype=result_dtype())
199210
tm.assert_extension_array_equal(result, expected)

pandas/tests/arrays/integer/test_dtypes.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import pandas as pd
77
import pandas._testing as tm
8-
from pandas.core.arrays import integer_array
98
from pandas.core.arrays.integer import Int8Dtype, UInt32Dtype
109

1110

@@ -28,7 +27,7 @@ def test_preserve_dtypes(op):
2827
{
2928
"A": ["a", "b", "b"],
3029
"B": [1, None, 3],
31-
"C": integer_array([1, None, 3], dtype="Int64"),
30+
"C": pd.array([1, None, 3], dtype="Int64"),
3231
}
3332
)
3433

@@ -43,15 +42,15 @@ def test_preserve_dtypes(op):
4342
result = getattr(df.groupby("A"), op)()
4443

4544
expected = pd.DataFrame(
46-
{"B": np.array([1.0, 3.0]), "C": integer_array([1, 3], dtype="Int64")},
45+
{"B": np.array([1.0, 3.0]), "C": pd.array([1, 3], dtype="Int64")},
4746
index=pd.Index(["a", "b"], name="A"),
4847
)
4948
tm.assert_frame_equal(result, expected)
5049

5150

5251
def test_astype_nansafe():
5352
# see gh-22343
54-
arr = integer_array([np.nan, 1, 2], dtype="Int8")
53+
arr = pd.array([np.nan, 1, 2], dtype="Int8")
5554
msg = "cannot convert to 'uint32'-dtype NumPy array with missing values."
5655

5756
with pytest.raises(ValueError, match=msg):
@@ -69,7 +68,7 @@ def test_construct_index(all_data, dropna):
6968
else:
7069
other = all_data
7170

72-
result = pd.Index(integer_array(other, dtype=all_data.dtype))
71+
result = pd.Index(pd.array(other, dtype=all_data.dtype))
7372
expected = pd.Index(other, dtype=object)
7473

7574
tm.assert_index_equal(result, expected)
@@ -229,14 +228,14 @@ def test_construct_cast_invalid(dtype):
229228
msg = "cannot safely"
230229
arr = [1.2, 2.3, 3.7]
231230
with pytest.raises(TypeError, match=msg):
232-
integer_array(arr, dtype=dtype)
231+
pd.array(arr, dtype=dtype)
233232

234233
with pytest.raises(TypeError, match=msg):
235234
pd.Series(arr).astype(dtype)
236235

237236
arr = [1.2, 2.3, 3.7, np.nan]
238237
with pytest.raises(TypeError, match=msg):
239-
integer_array(arr, dtype=dtype)
238+
pd.array(arr, dtype=dtype)
240239

241240
with pytest.raises(TypeError, match=msg):
242241
pd.Series(arr).astype(dtype)

0 commit comments

Comments
 (0)