Skip to content

Commit 04a87e5

Browse files
Backport PR #52547 on branch 2.0.x (BUG: __from_arrow__ not respecting explicit dtype) (#52584)
Backport PR #52547: BUG: __from_arrow__ not respecting explicit dtype Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 865c0d6 commit 04a87e5

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
@@ -26,6 +26,7 @@ Bug fixes
2626
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
2727
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
2828
- 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`)
29+
- Bug in :meth:`ArrowDtype.__from_arrow__` not respecting if dtype is explicitly given (:issue:`52533`)
2930

3031
.. ---------------------------------------------------------------------------
3132
.. _whatsnew_201.other:

pandas/core/arrays/arrow/dtype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,5 @@ def __from_arrow__(self, array: pa.Array | pa.ChunkedArray):
306306
Construct IntegerArray/FloatingArray from pyarrow Array/ChunkedArray.
307307
"""
308308
array_class = self.construct_array_type()
309-
return array_class(array)
309+
arr = array.cast(self.pyarrow_dtype, safe=True)
310+
return array_class(arr)

pandas/tests/extension/test_arrow.py

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

17401740

1741+
@pytest.mark.skipif(pa_version_under8p0, reason="returns object with 7.0")
1742+
def test_from_arrow_respecting_given_dtype():
1743+
date_array = pa.array(
1744+
[pd.Timestamp("2019-12-31"), pd.Timestamp("2019-12-31")], type=pa.date32()
1745+
)
1746+
result = date_array.to_pandas(
1747+
types_mapper={pa.date32(): ArrowDtype(pa.date64())}.get
1748+
)
1749+
expected = pd.Series(
1750+
[pd.Timestamp("2019-12-31"), pd.Timestamp("2019-12-31")],
1751+
dtype=ArrowDtype(pa.date64()),
1752+
)
1753+
tm.assert_series_equal(result, expected)
1754+
1755+
1756+
@pytest.mark.skipif(pa_version_under8p0, reason="doesn't raise with 7")
1757+
def test_from_arrow_respecting_given_dtype_unsafe():
1758+
array = pa.array([1.5, 2.5], type=pa.float64())
1759+
with pytest.raises(pa.ArrowInvalid, match="Float value 1.5 was truncated"):
1760+
array.to_pandas(types_mapper={pa.float64(): ArrowDtype(pa.int64())}.get)
1761+
1762+
17411763
def test_round():
17421764
dtype = "float64[pyarrow]"
17431765

0 commit comments

Comments
 (0)