Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 906e004

Browse filesBrowse files
Backport PR #51869 on branch 2.0.x (ENH: Add to_pydatetime for ArrowExtensionArray) (#51870)
Backport PR #51869: ENH: Add to_pydatetime for ArrowExtensionArray Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
1 parent 9c9e9a5 commit 906e004
Copy full SHA for 906e004

File tree

Expand file treeCollapse file tree

3 files changed

+19
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+19
-0
lines changed

‎pandas/core/arrays/arrow/array.py

Copy file name to clipboardExpand all lines: pandas/core/arrays/arrow/array.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,9 @@ def _dt_round(
19581958
):
19591959
return self._round_temporally("round", freq, ambiguous, nonexistent)
19601960

1961+
def _dt_to_pydatetime(self):
1962+
return np.array(self._data.to_pylist(), dtype=object)
1963+
19611964
def _dt_tz_localize(
19621965
self,
19631966
tz,

‎pandas/core/indexes/accessors.py

Copy file name to clipboardExpand all lines: pandas/core/indexes/accessors.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ def _delegate_method(self, name: str, *args, **kwargs):
209209

210210
return result
211211

212+
def to_pydatetime(self):
213+
return cast(ArrowExtensionArray, self._parent.array)._dt_to_pydatetime()
214+
212215
def isocalendar(self):
213216
from pandas import DataFrame
214217

‎pandas/tests/extension/test_arrow.py

Copy file name to clipboardExpand all lines: pandas/tests/extension/test_arrow.py
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,19 @@ def test_dt_ceil_year_floor(freq, method):
22612261
tm.assert_series_equal(result, expected)
22622262

22632263

2264+
def test_dt_to_pydatetime():
2265+
# GH 51859
2266+
data = [datetime(2022, 1, 1), datetime(2023, 1, 1)]
2267+
ser = pd.Series(data, dtype=ArrowDtype(pa.timestamp("ns")))
2268+
2269+
result = ser.dt.to_pydatetime()
2270+
expected = np.array(data, dtype=object)
2271+
tm.assert_numpy_array_equal(result, expected)
2272+
2273+
expected = ser.astype("datetime64[ns]").dt.to_pydatetime()
2274+
tm.assert_numpy_array_equal(result, expected)
2275+
2276+
22642277
def test_dt_tz_localize_unsupported_tz_options():
22652278
ser = pd.Series(
22662279
[datetime(year=2023, month=1, day=2, hour=3), None],

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.