Skip to content

Commit 5df4971

Browse files
Aloqeelypmhatre1
authored andcommitted
DEPR: to_pytimedelta return Index[object] (pandas-dev#58383)
* DEPR: to_pytimedelta return Index[object] * ignore doctest warning --------- Co-authored-by: Abdulaziz Aloqeely <[email protected]>
1 parent 4c03eb6 commit 5df4971

File tree

6 files changed

+34
-3
lines changed

6 files changed

+34
-3
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Other Deprecations
199199
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.all`, :meth:`DataFrame.min`, :meth:`DataFrame.max`, :meth:`DataFrame.sum`, :meth:`DataFrame.prod`, :meth:`DataFrame.mean`, :meth:`DataFrame.median`, :meth:`DataFrame.sem`, :meth:`DataFrame.var`, :meth:`DataFrame.std`, :meth:`DataFrame.skew`, :meth:`DataFrame.kurt`, :meth:`Series.all`, :meth:`Series.min`, :meth:`Series.max`, :meth:`Series.sum`, :meth:`Series.prod`, :meth:`Series.mean`, :meth:`Series.median`, :meth:`Series.sem`, :meth:`Series.var`, :meth:`Series.std`, :meth:`Series.skew`, and :meth:`Series.kurt`. (:issue:`57087`)
200200
- Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`)
201201
- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`)
202+
- Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``datetime.timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties. (:issue:`57463`)
202203
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
203204
-
204205

pandas/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def pytest_collection_modifyitems(items, config) -> None:
157157
("SeriesGroupBy.fillna", "SeriesGroupBy.fillna is deprecated"),
158158
("SeriesGroupBy.idxmin", "The behavior of Series.idxmin"),
159159
("SeriesGroupBy.idxmax", "The behavior of Series.idxmax"),
160+
("to_pytimedelta", "The behavior of TimedeltaProperties.to_pytimedelta"),
160161
# Docstring divides by zero to show behavior difference
161162
("missing.mask_zero_div_zero", "divide by zero encountered"),
162163
(

pandas/core/indexes/accessors.py

+20
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
NoReturn,
1010
cast,
1111
)
12+
import warnings
1213

1314
import numpy as np
1415

1516
from pandas._libs import lib
17+
from pandas.util._exceptions import find_stack_level
1618

1719
from pandas.core.dtypes.common import (
1820
is_integer_dtype,
@@ -210,6 +212,15 @@ def _delegate_method(self, name: str, *args, **kwargs):
210212
return result
211213

212214
def to_pytimedelta(self):
215+
# GH 57463
216+
warnings.warn(
217+
f"The behavior of {type(self).__name__}.to_pytimedelta is deprecated, "
218+
"in a future version this will return a Series containing python "
219+
"datetime.timedelta objects instead of an ndarray. To retain the "
220+
"old behavior, call `np.array` on the result",
221+
FutureWarning,
222+
stacklevel=find_stack_level(),
223+
)
213224
return cast(ArrowExtensionArray, self._parent.array)._dt_to_pytimedelta()
214225

215226
def to_pydatetime(self) -> Series:
@@ -462,6 +473,15 @@ def to_pytimedelta(self) -> np.ndarray:
462473
datetime.timedelta(days=2), datetime.timedelta(days=3),
463474
datetime.timedelta(days=4)], dtype=object)
464475
"""
476+
# GH 57463
477+
warnings.warn(
478+
f"The behavior of {type(self).__name__}.to_pytimedelta is deprecated, "
479+
"in a future version this will return a Series containing python "
480+
"datetime.timedelta objects instead of an ndarray. To retain the "
481+
"old behavior, call `np.array` on the result",
482+
FutureWarning,
483+
stacklevel=find_stack_level(),
484+
)
465485
return self._get_values().to_pytimedelta()
466486

467487
@property

pandas/tests/extension/test_arrow.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2861,12 +2861,16 @@ def test_dt_to_pytimedelta():
28612861
data = [timedelta(1, 2, 3), timedelta(1, 2, 4)]
28622862
ser = pd.Series(data, dtype=ArrowDtype(pa.duration("ns")))
28632863

2864-
result = ser.dt.to_pytimedelta()
2864+
msg = "The behavior of ArrowTemporalProperties.to_pytimedelta is deprecated"
2865+
with tm.assert_produces_warning(FutureWarning, match=msg):
2866+
result = ser.dt.to_pytimedelta()
28652867
expected = np.array(data, dtype=object)
28662868
tm.assert_numpy_array_equal(result, expected)
28672869
assert all(type(res) is timedelta for res in result)
28682870

2869-
expected = ser.astype("timedelta64[ns]").dt.to_pytimedelta()
2871+
msg = "The behavior of TimedeltaProperties.to_pytimedelta is deprecated"
2872+
with tm.assert_produces_warning(FutureWarning, match=msg):
2873+
expected = ser.astype("timedelta64[ns]").dt.to_pytimedelta()
28702874
tm.assert_numpy_array_equal(result, expected)
28712875

28722876

pandas/tests/series/accessors/test_cat_accessor.py

+3
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ def test_dt_accessor_api_for_categorical(self, idx):
200200
if func == "to_period" and getattr(idx, "tz", None) is not None:
201201
# dropping TZ
202202
warn_cls.append(UserWarning)
203+
elif func == "to_pytimedelta":
204+
# GH 57463
205+
warn_cls.append(FutureWarning)
203206
if warn_cls:
204207
warn_cls = tuple(warn_cls)
205208
else:

pandas/tests/series/accessors/test_dt_accessor.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ def test_dt_namespace_accessor_timedelta(self):
192192
assert isinstance(result, DataFrame)
193193
tm.assert_index_equal(result.index, ser.index)
194194

195-
result = ser.dt.to_pytimedelta()
195+
msg = "The behavior of TimedeltaProperties.to_pytimedelta is deprecated"
196+
with tm.assert_produces_warning(FutureWarning, match=msg):
197+
result = ser.dt.to_pytimedelta()
196198
assert isinstance(result, np.ndarray)
197199
assert result.dtype == object
198200

0 commit comments

Comments
 (0)