Skip to content

Commit c5de1f2

Browse files
some docs and tests
1 parent b0ea2f1 commit c5de1f2

File tree

2 files changed

+72
-18
lines changed

2 files changed

+72
-18
lines changed

pandas/core/arrays/integer.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass
1212
from pandas.core.dtypes.common import (
1313
is_integer, is_scalar, is_float,
14+
is_bool_dtype,
1415
is_float_dtype,
1516
is_integer_dtype,
1617
is_object_dtype,
@@ -85,6 +86,7 @@ def integer_array(values, dtype=None, copy=False):
8586
values : 1D list-like
8687
dtype : dtype, optional
8788
dtype to coerce
89+
copy : boolean, default False
8890
8991
Returns
9092
-------
@@ -198,33 +200,40 @@ def coerce_to_array(values, dtype, mask=None, copy=False):
198200

199201
class IntegerArray(ExtensionArray, ExtensionOpsMixin):
200202
"""
201-
We represent an IntegerArray with 2 numpy arrays
203+
Array of integer (optional missing) values.
204+
205+
We represent an IntegerArray with 2 numpy arrays:
206+
202207
- data: contains a numpy integer array of the appropriate dtype
203-
- mask: a boolean array holding a mask on the data, False is missing
208+
- mask: a boolean array holding a mask on the data, True is missing
209+
210+
To construct an IntegerArray from generic array-like input, use
211+
``integer_array`` function instead.
212+
213+
Parameters
214+
----------
215+
values : integer 1D numpy array
216+
mask : boolean 1D numpy array
217+
copy : bool, default False
218+
219+
Returns
220+
-------
221+
IntegerArray
222+
204223
"""
205224

206225
@cache_readonly
207226
def dtype(self):
208227
return _dtypes[str(self._data.dtype)]
209228

210229
def __init__(self, values, mask, copy=False):
211-
"""
212-
Parameters
213-
----------
214-
values : 1D list-like / IntegerArray
215-
mask : 1D list-like, optional
216-
dtype : subclass of _IntegerDtype, optional
217-
copy : bool, default False
218-
219-
Returns
220-
-------
221-
IntegerArray
222-
"""
223230
if not (isinstance(values, np.ndarray)
224-
and np.issubdtype(values.dtype, np.integer)):
225-
raise TypeError("values should be integer numpy array")
226-
if not (isinstance(mask, np.ndarray) and mask.dtype == np.bool_):
227-
raise TypeError("mask should be boolean numpy array")
231+
and is_integer_dtype(values.dtype)):
232+
raise TypeError("values should be integer numpy array. Use "
233+
"the 'integer_array' function instead")
234+
if not (isinstance(mask, np.ndarray) and is_bool_dtype(mask.dtype)):
235+
raise TypeError("mask should be boolean numpy array. Use "
236+
"the 'integer_array' function instead")
228237

229238
if copy:
230239
values = values.copy()

pandas/tests/extension/integer/test_integer.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,45 @@ def test_conversions(data_missing):
645645
assert type(r) == type(e)
646646

647647

648+
def test_integer_array_constructor():
649+
values = np.array([1, 2, 3, 4], dtype='int64')
650+
mask = np.array([False, False, False, True], dtype='bool')
651+
652+
result = IntegerArray(values, mask)
653+
expected = integer_array([1, 2, 3, np.nan], dtype='int64')
654+
tm.assert_extension_array_equal(result, expected)
655+
656+
with pytest.raises(TypeError):
657+
IntegerArray(values.tolist(), mask)
658+
659+
with pytest.raises(TypeError):
660+
IntegerArray(values, mask.tolist())
661+
662+
with pytest.raises(TypeError):
663+
IntegerArray(values.astype(float), mask)
664+
665+
with pytest.raises(TypeError):
666+
IntegerArray(values)
667+
668+
669+
def test_integer_array_constructor_copy():
670+
values = np.array([1, 2, 3, 4], dtype='int64')
671+
mask = np.array([False, False, False, True], dtype='bool')
672+
673+
result = IntegerArray(values, mask)
674+
assert result._data is values
675+
assert result._mask is mask
676+
677+
result = IntegerArray(values, mask, copy=True)
678+
assert result._data is not values
679+
assert result._mask is not mask
680+
681+
648682
@pytest.mark.parametrize(
649683
'values',
650684
[
651685
['foo', 'bar'],
686+
['1', '2'],
652687
'foo',
653688
1,
654689
1.0,
@@ -660,6 +695,15 @@ def test_to_integer_array_error(values):
660695
integer_array(values)
661696

662697

698+
def test_to_integer_array_float():
699+
result = integer_array([1., 2.])
700+
expected = integer_array([1, 2])
701+
tm.assert_extension_array_equal(result, expected)
702+
703+
with pytest.raises(TypeError, match="cannot safely cast non-equivalent"):
704+
integer_array([1.5, 2.])
705+
706+
663707
@pytest.mark.parametrize(
664708
'values, to_dtype, result_dtype',
665709
[
@@ -669,6 +713,7 @@ def test_to_integer_array_error(values):
669713
def test_to_integer_array(values, to_dtype, result_dtype):
670714
# convert existing arrays to IntegerArrays
671715
result = integer_array(values, dtype=to_dtype)
716+
assert result.dtype == result_dtype()
672717
expected = integer_array(values, dtype=result_dtype())
673718
tm.assert_extension_array_equal(result, expected)
674719

0 commit comments

Comments
 (0)