Skip to content

Commit 0e1c237

Browse files
authored
BUG: __from_arrow__ not respecting explicit dtype (#52547)
* BUG: __from_arrow__ not respecting explicit dtype * Skip for arrow 7 * Add test * Remove skip * Skip * Skip
1 parent e3337aa commit 0e1c237

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v2.0.1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Bug fixes
2727
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
2828
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
2929
- Bug in :func:`pandas.testing.assert_series_equal` where ``check_dtype=False`` would still raise for datetime or timedelta types with different resolutions (:issue:`52449`)
30+
- Bug in :meth:`ArrowDtype.__from_arrow__` not respecting if dtype is explicitly given (:issue:`52533`)
3031

3132
.. ---------------------------------------------------------------------------
3233
.. _whatsnew_201.other:

pandas/core/arrays/arrow/dtype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,5 @@ def __from_arrow__(self, array: pa.Array | pa.ChunkedArray):
319319
Construct IntegerArray/FloatingArray from pyarrow Array/ChunkedArray.
320320
"""
321321
array_class = self.construct_array_type()
322-
return array_class(array)
322+
arr = array.cast(self.pyarrow_dtype, safe=True)
323+
return array_class(arr)

pandas/tests/extension/test_arrow.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,28 @@ def test_setitem_invalid_dtype(data):
17291729
data[:] = fill_value
17301730

17311731

1732+
@pytest.mark.skipif(pa_version_under8p0, reason="returns object with 7.0")
1733+
def test_from_arrow_respecting_given_dtype():
1734+
date_array = pa.array(
1735+
[pd.Timestamp("2019-12-31"), pd.Timestamp("2019-12-31")], type=pa.date32()
1736+
)
1737+
result = date_array.to_pandas(
1738+
types_mapper={pa.date32(): ArrowDtype(pa.date64())}.get
1739+
)
1740+
expected = pd.Series(
1741+
[pd.Timestamp("2019-12-31"), pd.Timestamp("2019-12-31")],
1742+
dtype=ArrowDtype(pa.date64()),
1743+
)
1744+
tm.assert_series_equal(result, expected)
1745+
1746+
1747+
@pytest.mark.skipif(pa_version_under8p0, reason="doesn't raise with 7")
1748+
def test_from_arrow_respecting_given_dtype_unsafe():
1749+
array = pa.array([1.5, 2.5], type=pa.float64())
1750+
with pytest.raises(pa.ArrowInvalid, match="Float value 1.5 was truncated"):
1751+
array.to_pandas(types_mapper={pa.float64(): ArrowDtype(pa.int64())}.get)
1752+
1753+
17321754
def test_round():
17331755
dtype = "float64[pyarrow]"
17341756

0 commit comments

Comments
 (0)