Skip to content

Commit 7131268

Browse files
authored
BUG: __from_arrow__ inconsistent strictness (#44891)
1 parent 010989d commit 7131268

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,8 @@ Conversion
672672
- Bug in :meth:`DataFrame.convert_dtypes` not returning the correct type when a subclass does not overload :meth:`_constructor_sliced` (:issue:`43201`)
673673
- Bug in :meth:`DataFrame.astype` not propagating ``attrs`` from the original :class:`DataFrame` (:issue:`44414`)
674674
- Bug in :meth:`DataFrame.convert_dtypes` result losing ``columns.names`` (:issue:`41435`)
675+
- Bug in constructing a ``IntegerArray`` from pyarrow data failing to validate dtypes (:issue:`44891`)
676+
-
675677

676678
Strings
677679
^^^^^^^

pandas/core/arrays/numeric.py

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
is_integer,
2222
is_integer_dtype,
2323
is_list_like,
24+
pandas_dtype,
2425
)
2526

2627
from pandas.core.arrays.masked import (
@@ -49,6 +50,16 @@ def __from_arrow__(
4950

5051
pyarrow_type = pyarrow.from_numpy_dtype(self.type)
5152
if not array.type.equals(pyarrow_type):
53+
# test_from_arrow_type_error raise for string, but allow
54+
# through itemsize conversion GH#31896
55+
rt_dtype = pandas_dtype(array.type.to_pandas_dtype())
56+
if rt_dtype.kind not in ["i", "u", "f"]:
57+
# Could allow "c" or potentially disallow float<->int conversion,
58+
# but at the moment we specifically test that uint<->int works
59+
raise TypeError(
60+
f"Expected array of {self} type, got {array.type} instead"
61+
)
62+
5263
array = array.cast(pyarrow_type)
5364

5465
if isinstance(array, pyarrow.Array):

pandas/tests/arrays/masked/test_arrow_compat.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,9 @@ def test_pyarrow_array_to_numpy_and_mask(np_dtype_to_arrays):
176176
tm.assert_numpy_array_equal(mask, mask_expected_empty)
177177

178178

179-
def test_from_arrow_type_error(request, data):
179+
def test_from_arrow_type_error(data):
180180
# ensure that __from_arrow__ returns a TypeError when getting a wrong
181181
# array type
182-
if data.dtype != "boolean":
183-
# TODO numeric dtypes cast any incoming array to the correct dtype
184-
# instead of erroring
185-
request.node.add_marker(
186-
pytest.mark.xfail(raises=None, reason="numeric dtypes don't error but cast")
187-
)
188182

189183
arr = pa.array(data).cast("string")
190184
with pytest.raises(TypeError, match=None):

0 commit comments

Comments
 (0)