Skip to content

Commit baa77c3

Browse files
jbrockmendeljreback
authored andcommitted
DEPR: deprecate Timedelta.resolution (#26839)
1 parent a890caf commit baa77c3

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ Other Deprecations
502502
Use the public attributes :attr:`~RangeIndex.start`, :attr:`~RangeIndex.stop` and :attr:`~RangeIndex.step` instead (:issue:`26581`).
503503
- The :meth:`Series.ftype`, :meth:`Series.ftypes` and :meth:`DataFrame.ftypes` methods are deprecated and will be removed in a future version.
504504
Instead, use :meth:`Series.dtype` and :meth:`DataFrame.dtypes` (:issue:`26705`).
505-
505+
- :meth:`Timedelta.resolution` is deprecated and replaced with :meth:`Timedelta.resolution_string`. In a future version, :meth:`Timedelta.resolution` will be changed to behave like the standard library :attr:`timedelta.resolution` (:issue:`21344`)
506506

507507
.. _whatsnew_0250.prior_deprecations:
508508

pandas/_libs/tslibs/timedeltas.pyx

+51-2
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ cdef class _Timedelta(timedelta):
950950
return np.int64(self.value).view('m8[ns]')
951951

952952
@property
953-
def resolution(self):
953+
def resolution_string(self):
954954
"""
955955
Return a string representing the lowest timedelta resolution.
956956
@@ -991,7 +991,6 @@ cdef class _Timedelta(timedelta):
991991
>>> td.resolution
992992
'U'
993993
"""
994-
995994
self._ensure_components()
996995
if self._ns:
997996
return "N"
@@ -1008,6 +1007,56 @@ cdef class _Timedelta(timedelta):
10081007
else:
10091008
return "D"
10101009

1010+
@property
1011+
def resolution(self):
1012+
"""
1013+
Return a string representing the lowest timedelta resolution.
1014+
1015+
Each timedelta has a defined resolution that represents the lowest OR
1016+
most granular level of precision. Each level of resolution is
1017+
represented by a short string as defined below:
1018+
1019+
Resolution: Return value
1020+
1021+
* Days: 'D'
1022+
* Hours: 'H'
1023+
* Minutes: 'T'
1024+
* Seconds: 'S'
1025+
* Milliseconds: 'L'
1026+
* Microseconds: 'U'
1027+
* Nanoseconds: 'N'
1028+
1029+
Returns
1030+
-------
1031+
str
1032+
Timedelta resolution.
1033+
1034+
Examples
1035+
--------
1036+
>>> td = pd.Timedelta('1 days 2 min 3 us 42 ns')
1037+
>>> td.resolution
1038+
'N'
1039+
1040+
>>> td = pd.Timedelta('1 days 2 min 3 us')
1041+
>>> td.resolution
1042+
'U'
1043+
1044+
>>> td = pd.Timedelta('2 min 3 s')
1045+
>>> td.resolution
1046+
'S'
1047+
1048+
>>> td = pd.Timedelta(36, unit='us')
1049+
>>> td.resolution
1050+
'U'
1051+
"""
1052+
# See GH#21344
1053+
warnings.warn("Timedelta.resolution is deprecated, in a future "
1054+
"version will behave like the standard library "
1055+
"datetime.timedelta.resolution attribute. "
1056+
"Use Timedelta.resolution_string instead.",
1057+
FutureWarning)
1058+
return self.resolution_string
1059+
10111060
@property
10121061
def nanoseconds(self):
10131062
"""

pandas/core/indexes/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,11 @@ def _maybe_cast_slice_bound(self, label, side, kind):
551551

552552
if isinstance(label, str):
553553
parsed = Timedelta(label)
554-
lbound = parsed.round(parsed.resolution)
554+
lbound = parsed.round(parsed.resolution_string)
555555
if side == 'left':
556556
return lbound
557557
else:
558-
return (lbound + to_offset(parsed.resolution) -
558+
return (lbound + to_offset(parsed.resolution_string) -
559559
Timedelta(1, 'ns'))
560560
elif ((is_integer(label) or is_float(label)) and
561561
not is_timedelta64_dtype(label)):

pandas/tests/scalar/test_nat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ def test_nat_iso_format(get_nat):
142142

143143
@pytest.mark.parametrize("klass,expected", [
144144
(Timestamp, ["freqstr", "normalize", "to_julian_date", "to_period", "tz"]),
145-
(Timedelta, ["components", "delta", "is_populated", "to_pytimedelta",
146-
"to_timedelta64", "view"])
145+
(Timedelta, ["components", "delta", "is_populated", "resolution_string",
146+
"to_pytimedelta", "to_timedelta64", "view"])
147147
])
148148
def test_missing_public_nat_methods(klass, expected):
149149
# see gh-17327

pandas/tests/scalar/timedelta/test_timedelta.py

+16
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,22 @@ def test_components(self):
742742
assert not result.iloc[0].isna().all()
743743
assert result.iloc[1].isna().all()
744744

745+
def test_resolution_string(self):
746+
assert Timedelta(days=1).resolution_string == 'D'
747+
assert Timedelta(days=1, hours=6).resolution_string == 'H'
748+
assert Timedelta(days=1, minutes=6).resolution_string == 'T'
749+
assert Timedelta(days=1, seconds=6).resolution_string == 'S'
750+
assert Timedelta(days=1, milliseconds=6).resolution_string == 'L'
751+
assert Timedelta(days=1, microseconds=6).resolution_string == 'U'
752+
assert Timedelta(days=1, nanoseconds=6).resolution_string == 'N'
753+
754+
def test_resolution_deprecated(self):
755+
# GH#21344
756+
td = Timedelta(days=4, hours=3)
757+
with tm.assert_produces_warning(FutureWarning) as w:
758+
td.resolution
759+
assert "Use Timedelta.resolution_string instead" in str(w[0].message)
760+
745761

746762
@pytest.mark.parametrize('value, expected', [
747763
(Timedelta('10S'), True),

0 commit comments

Comments
 (0)