Skip to content

Commit

Permalink
ERR: Raise a better error message with to_pydatetime and ArrowDtype(p…
Browse files Browse the repository at this point in the history
…a.date) (pandas-dev#52952)

* ERR: Raise a better error message with to_pydatetime and ArrowDtype(pa.date)

* Update pandas/tests/extension/test_arrow.py

* Add warning

* fix error message

* Suggest to convert to pyarrow timestamp type first
  • Loading branch information
mroeschke authored and Yi Wei committed May 19, 2023
1 parent 6681710 commit 305c7f2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Bug fixes

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

.. ---------------------------------------------------------------------------
.. _whatsnew_202.contributors:
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,11 @@ def _dt_month_name(self, locale: str | None = None):
return type(self)(pc.strftime(self._pa_array, format="%B", locale=locale))

def _dt_to_pydatetime(self):
if pa.types.is_date(self.dtype.pyarrow_dtype):
raise ValueError(
f"to_pydatetime cannot be called with {self.dtype.pyarrow_dtype} type. "
"Convert to pyarrow timestamp type."
)
data = self._pa_array.to_pylist()
if self._dtype.pyarrow_dtype.unit == "ns":
data = [None if ts is None else ts.to_pydatetime(warn=False) for ts in data]
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2615,6 +2615,19 @@ def test_dt_to_pydatetime():
tm.assert_numpy_array_equal(result, expected)


@pytest.mark.parametrize("date_type", [32, 64])
def test_dt_to_pydatetime_date_error(date_type):
# GH 52812
ser = pd.Series(
[date(2022, 12, 31)],
dtype=ArrowDtype(getattr(pa, f"date{date_type}")()),
)
msg = "The behavior of ArrowTemporalProperties.to_pydatetime is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
with pytest.raises(ValueError, match="to_pydatetime cannot be called with"):
ser.dt.to_pydatetime()


def test_dt_tz_localize_unsupported_tz_options():
ser = pd.Series(
[datetime(year=2023, month=1, day=2, hour=3), None],
Expand Down

0 comments on commit 305c7f2

Please sign in to comment.