Skip to content

Commit fbbdac5

Browse files
authored
Backport PR #52952: ERR: Raise a better error message with to_pydatetime and ArrowDtype(pa.date) (#52989)
* Backport PR #52952: ERR: Raise a better error message with to_pydatetime and ArrowDtype(pa.date) * Update pandas/tests/extension/test_arrow.py
1 parent bfe810b commit fbbdac5

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Diff for: doc/source/whatsnew/v2.0.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Bug fixes
3131

3232
Other
3333
~~~~~
34-
-
34+
- Raised a better error message when calling :func:`Series.dt.to_pydatetime` with :class:`ArrowDtype` with ``pyarrow.date32`` or ``pyarrow.date64`` type (:issue:`52812`)
3535

3636
.. ---------------------------------------------------------------------------
3737
.. _whatsnew_202.contributors:

Diff for: pandas/core/arrays/arrow/array.py

+5
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,11 @@ def _dt_round(
21612161
return self._round_temporally("round", freq, ambiguous, nonexistent)
21622162

21632163
def _dt_to_pydatetime(self):
2164+
if pa.types.is_date(self.dtype.pyarrow_dtype):
2165+
raise ValueError(
2166+
f"to_pydatetime cannot be called with {self.dtype.pyarrow_dtype} type. "
2167+
"Convert to pyarrow timestamp type."
2168+
)
21642169
data = self._data.to_pylist()
21652170
if self._dtype.pyarrow_dtype.unit == "ns":
21662171
data = [None if ts is None else ts.to_pydatetime(warn=False) for ts in data]

Diff for: pandas/tests/extension/test_arrow.py

+11
Original file line numberDiff line numberDiff line change
@@ -2512,6 +2512,17 @@ def test_dt_to_pydatetime():
25122512
tm.assert_numpy_array_equal(result, expected)
25132513

25142514

2515+
@pytest.mark.parametrize("date_type", [32, 64])
2516+
def test_dt_to_pydatetime_date_error(date_type):
2517+
# GH 52812
2518+
ser = pd.Series(
2519+
[date(2022, 12, 31)],
2520+
dtype=ArrowDtype(getattr(pa, f"date{date_type}")()),
2521+
)
2522+
with pytest.raises(ValueError, match="to_pydatetime cannot be called with"):
2523+
ser.dt.to_pydatetime()
2524+
2525+
25152526
def test_dt_tz_localize_unsupported_tz_options():
25162527
ser = pd.Series(
25172528
[datetime(year=2023, month=1, day=2, hour=3), None],

0 commit comments

Comments
 (0)