Skip to content

CLN: remove internal usage of integer_array() #38289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pandas/core/arrays/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pandas.core.arrays.categorical import Categorical
from pandas.core.arrays.datetimes import DatetimeArray
from pandas.core.arrays.floating import FloatingArray
from pandas.core.arrays.integer import IntegerArray, integer_array
from pandas.core.arrays.integer import IntegerArray
from pandas.core.arrays.interval import IntervalArray
from pandas.core.arrays.masked import BaseMaskedArray
from pandas.core.arrays.numpy_ import PandasArray, PandasDtype
Expand All @@ -26,7 +26,6 @@
"DatetimeArray",
"FloatingArray",
"IntegerArray",
"integer_array",
"IntervalArray",
"PandasArray",
"PandasDtype",
Expand Down
26 changes: 2 additions & 24 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,6 @@ def __from_arrow__(
return IntegerArray._concat_same_type(results)


def integer_array(values, dtype=None, copy: bool = False) -> "IntegerArray":
"""
Infer and return an integer array of the values.
Parameters
----------
values : 1D list-like
dtype : dtype, optional
dtype to coerce
copy : bool, default False
Returns
-------
IntegerArray
Raises
------
TypeError if incompatible types
"""
values, mask = coerce_to_array(values, dtype=dtype, copy=copy)
return IntegerArray(values, mask)


def safe_cast(values, dtype, copy: bool):
"""
Safely cast the values to the dtype if they
Expand Down Expand Up @@ -360,7 +337,8 @@ def __abs__(self):
def _from_sequence(
cls, scalars, *, dtype=None, copy: bool = False
) -> "IntegerArray":
return integer_array(scalars, dtype=dtype, copy=copy)
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
return IntegerArray(values, mask)

@classmethod
def _from_sequence_of_strings(
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/arrays/integer/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from pandas.core.arrays import integer_array
import pandas as pd
from pandas.core.arrays.integer import (
Int8Dtype,
Int16Dtype,
Expand Down Expand Up @@ -32,15 +32,15 @@ def dtype(request):

@pytest.fixture
def data(dtype):
return integer_array(
return pd.array(
list(range(8)) + [np.nan] + list(range(10, 98)) + [np.nan] + [99, 100],
dtype=dtype,
)


@pytest.fixture
def data_missing(dtype):
return integer_array([np.nan, 1], dtype=dtype)
return pd.array([np.nan, 1], dtype=dtype)


@pytest.fixture(params=["data", "data_missing"])
Expand Down
17 changes: 7 additions & 10 deletions pandas/tests/arrays/integer/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays import FloatingArray, integer_array
from pandas.core.arrays import FloatingArray
import pandas.core.ops as ops

# Basic test for the arithmetic array ops
Expand Down Expand Up @@ -131,10 +131,10 @@ def test_pow_scalar():


def test_pow_array():
a = integer_array([0, 0, 0, 1, 1, 1, None, None, None])
b = integer_array([0, 1, None, 0, 1, None, 0, 1, None])
a = pd.array([0, 0, 0, 1, 1, 1, None, None, None])
b = pd.array([0, 1, None, 0, 1, None, 0, 1, None])
result = a ** b
expected = integer_array([1, 0, None, 1, 1, 1, 1, None, None])
expected = pd.array([1, 0, None, 1, 1, 1, 1, None, None])
tm.assert_extension_array_equal(result, expected)


Expand All @@ -149,7 +149,7 @@ def test_rpow_one_to_na():

@pytest.mark.parametrize("other", [0, 0.5])
def test_numpy_zero_dim_ndarray(other):
arr = integer_array([1, None, 2])
arr = pd.array([1, None, 2])
result = arr + np.array(other)
expected = arr + other
tm.assert_equal(result, expected)
Expand Down Expand Up @@ -265,7 +265,7 @@ def test_reduce_to_float(op):
{
"A": ["a", "b", "b"],
"B": [1, None, 3],
"C": integer_array([1, None, 3], dtype="Int64"),
"C": pd.array([1, None, 3], dtype="Int64"),
}
)

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

expected = pd.DataFrame(
{
"B": np.array([1.0, 3.0]),
"C": pd.array([1, 3], dtype="Float64"),
},
{"B": np.array([1.0, 3.0]), "C": pd.array([1, 3], dtype="Float64")},
index=pd.Index(["a", "b"], name="A"),
)
tm.assert_frame_equal(result, expected)
Expand Down
79 changes: 45 additions & 34 deletions pandas/tests/arrays/integer/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import pandas as pd
import pandas._testing as tm
from pandas.api.types import is_integer
from pandas.core.arrays import IntegerArray, integer_array
from pandas.core.arrays import IntegerArray
from pandas.core.arrays.integer import Int8Dtype, Int32Dtype, Int64Dtype


@pytest.fixture(params=[pd.array, IntegerArray._from_sequence])
def constructor(request):
return request.param


def test_uses_pandas_na():
a = pd.array([1, None], dtype=Int64Dtype())
assert a[1] is pd.NA
Expand Down Expand Up @@ -65,7 +70,7 @@ def test_integer_array_constructor():
mask = np.array([False, False, False, True], dtype="bool")

result = IntegerArray(values, mask)
expected = integer_array([1, 2, 3, np.nan], dtype="int64")
expected = pd.array([1, 2, 3, np.nan], dtype="Int64")
tm.assert_extension_array_equal(result, expected)

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


@pytest.mark.parametrize(
"a, b",
[
([1, None], [1, np.nan]),
([None], [np.nan]),
([None, np.nan], [np.nan, np.nan]),
([np.nan, np.nan], [np.nan, np.nan]),
],
)
def test_integer_array_constructor_none_is_nan(a, b):
result = integer_array(a)
expected = integer_array(b)
tm.assert_extension_array_equal(result, expected)


def test_integer_array_constructor_copy():
values = np.array([1, 2, 3, 4], dtype="int64")
mask = np.array([False, False, False, True], dtype="bool")
Expand All @@ -110,6 +100,21 @@ def test_integer_array_constructor_copy():
assert result._mask is not mask


@pytest.mark.parametrize(
"a, b",
[
([1, None], [1, np.nan]),
([None], [np.nan]),
([None, np.nan], [np.nan, np.nan]),
([np.nan, np.nan], [np.nan, np.nan]),
],
)
def test_to_integer_array_none_is_nan(a, b):
result = pd.array(a, dtype="Int64")
expected = pd.array(b, dtype="Int64")
tm.assert_extension_array_equal(result, expected)


@pytest.mark.parametrize(
"values",
[
Expand All @@ -129,42 +134,46 @@ def test_to_integer_array_error(values):
msg = (
r"(:?.* cannot be converted to an IntegerDtype)"
r"|(:?values must be a 1D list-like)"
r"|(Cannot pass scalar)"
)
with pytest.raises((ValueError, TypeError), match=msg):
pd.array(values, dtype="Int64")

with pytest.raises(TypeError, match=msg):
integer_array(values)
IntegerArray._from_sequence(values)


def test_to_integer_array_inferred_dtype():
def test_to_integer_array_inferred_dtype(constructor):
# if values has dtype -> respect it
result = integer_array(np.array([1, 2], dtype="int8"))
result = constructor(np.array([1, 2], dtype="int8"))
assert result.dtype == Int8Dtype()
result = integer_array(np.array([1, 2], dtype="int32"))
result = constructor(np.array([1, 2], dtype="int32"))
assert result.dtype == Int32Dtype()

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


def test_to_integer_array_dtype_keyword():
result = integer_array([1, 2], dtype="int8")
def test_to_integer_array_dtype_keyword(constructor):
result = constructor([1, 2], dtype="Int8")
assert result.dtype == Int8Dtype()

# if values has dtype -> override it
result = integer_array(np.array([1, 2], dtype="int8"), dtype="int32")
result = constructor(np.array([1, 2], dtype="int8"), dtype="Int32")
assert result.dtype == Int32Dtype()


def test_to_integer_array_float():
result = integer_array([1.0, 2.0])
expected = integer_array([1, 2])
result = IntegerArray._from_sequence([1.0, 2.0])
expected = pd.array([1, 2], dtype="Int64")
tm.assert_extension_array_equal(result, expected)

with pytest.raises(TypeError, match="cannot safely cast non-equivalent"):
integer_array([1.5, 2.0])
IntegerArray._from_sequence([1.5, 2.0])

# for float dtypes, the itemsize is not preserved
result = integer_array(np.array([1.0, 2.0], dtype="float32"))
result = IntegerArray._from_sequence(np.array([1.0, 2.0], dtype="float32"))
assert result.dtype == Int64Dtype()


Expand All @@ -176,10 +185,12 @@ def test_to_integer_array_float():
([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype()),
],
)
def test_to_integer_array_bool(bool_values, int_values, target_dtype, expected_dtype):
result = integer_array(bool_values, dtype=target_dtype)
def test_to_integer_array_bool(
constructor, bool_values, int_values, target_dtype, expected_dtype
):
result = constructor(bool_values, dtype=target_dtype)
assert result.dtype == expected_dtype
expected = integer_array(int_values, dtype=target_dtype)
expected = pd.array(int_values, dtype=target_dtype)
tm.assert_extension_array_equal(result, expected)


Expand All @@ -193,7 +204,7 @@ def test_to_integer_array_bool(bool_values, int_values, target_dtype, expected_d
)
def test_to_integer_array(values, to_dtype, result_dtype):
# convert existing arrays to IntegerArrays
result = integer_array(values, dtype=to_dtype)
result = IntegerArray._from_sequence(values, dtype=to_dtype)
assert result.dtype == result_dtype()
expected = integer_array(values, dtype=result_dtype())
expected = pd.array(values, dtype=result_dtype())
tm.assert_extension_array_equal(result, expected)
13 changes: 6 additions & 7 deletions pandas/tests/arrays/integer/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays import integer_array
from pandas.core.arrays.integer import Int8Dtype, UInt32Dtype


Expand All @@ -28,7 +27,7 @@ def test_preserve_dtypes(op):
{
"A": ["a", "b", "b"],
"B": [1, None, 3],
"C": integer_array([1, None, 3], dtype="Int64"),
"C": pd.array([1, None, 3], dtype="Int64"),
}
)

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

expected = pd.DataFrame(
{"B": np.array([1.0, 3.0]), "C": integer_array([1, 3], dtype="Int64")},
{"B": np.array([1.0, 3.0]), "C": pd.array([1, 3], dtype="Int64")},
index=pd.Index(["a", "b"], name="A"),
)
tm.assert_frame_equal(result, expected)


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

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

result = pd.Index(integer_array(other, dtype=all_data.dtype))
result = pd.Index(pd.array(other, dtype=all_data.dtype))
expected = pd.Index(other, dtype=object)

tm.assert_index_equal(result, expected)
Expand Down Expand Up @@ -229,14 +228,14 @@ def test_construct_cast_invalid(dtype):
msg = "cannot safely"
arr = [1.2, 2.3, 3.7]
with pytest.raises(TypeError, match=msg):
integer_array(arr, dtype=dtype)
pd.array(arr, dtype=dtype)

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

arr = [1.2, 2.3, 3.7, np.nan]
with pytest.raises(TypeError, match=msg):
integer_array(arr, dtype=dtype)
pd.array(arr, dtype=dtype)

with pytest.raises(TypeError, match=msg):
pd.Series(arr).astype(dtype)
Expand Down
Loading