Skip to content

Commit 666da19

Browse files
jbrockmendelproost
authored andcommitted
BUG/DEPR: Timestamp/Timedelta resolution (pandas-dev#29910)
* BUG/DEPR: Timestamp/Timedelta resolution * GH ref
1 parent 45b70de commit 666da19

File tree

5 files changed

+15
-62
lines changed

5 files changed

+15
-62
lines changed

doc/source/whatsnew/v1.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
457457
- Removed previously deprecated keyword "n" from :meth:`DatetimeIndex.shift`, :meth:`TimedeltaIndex.shift`, :meth:`PeriodIndex.shift`, use "periods" instead (:issue:`22458`)
458458
- Changed the default value for the `raw` argument in :func:`Series.rolling().apply() <pandas.core.window.Rolling.apply>`, :func:`DataFrame.rolling().apply() <pandas.core.window.Rolling.apply>`,
459459
- :func:`Series.expanding().apply() <pandas.core.window.Expanding.apply>`, and :func:`DataFrame.expanding().apply() <pandas.core.window.Expanding.apply>` to ``False`` (:issue:`20584`)
460+
- Changed :meth:`Timedelta.resolution` to match the behavior of the standard library ``datetime.timedelta.resolution``, for the old behavior, use :meth:`Timedelta.resolution_string` (:issue:`26839`)
460461
- Removed previously deprecated :attr:`Timestamp.weekday_name`, :attr:`DatetimeIndex.weekday_name`, and :attr:`Series.dt.weekday_name` (:issue:`18164`)
461462
- Removed previously deprecated ``errors`` argument in :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` (:issue:`22644`)
462463
-
@@ -516,6 +517,7 @@ Datetimelike
516517
- Bug in :func:`pandas._config.localization.get_locales` where the ``locales -a`` encodes the locales list as windows-1252 (:issue:`23638`, :issue:`24760`, :issue:`27368`)
517518
- Bug in :meth:`Series.var` failing to raise ``TypeError`` when called with ``timedelta64[ns]`` dtype (:issue:`28289`)
518519
- Bug in :meth:`DatetimeIndex.strftime` and :meth:`Series.dt.strftime` where ``NaT`` was converted to the string ``'NaT'`` instead of ``np.nan`` (:issue:`29578`)
520+
- Bug in :attr:`Timestamp.resolution` being a property instead of a class attribute (:issue:`29910`)
519521

520522
Timedelta
521523
^^^^^^^^^

pandas/_libs/tslibs/timedeltas.pyx

+1-50
Original file line numberDiff line numberDiff line change
@@ -1005,56 +1005,6 @@ cdef class _Timedelta(timedelta):
10051005
else:
10061006
return "D"
10071007

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

pandas/_libs/tslibs/timestamps.pyx

+1-9
Original file line numberDiff line numberDiff line change
@@ -744,15 +744,6 @@ timedelta}, default 'raise'
744744
"""
745745
return bool(ccalendar.is_leapyear(self.year))
746746

747-
@property
748-
def resolution(self):
749-
"""
750-
Return resolution describing the smallest difference between two
751-
times that can be represented by Timestamp object_state.
752-
"""
753-
# GH#21336, GH#21365
754-
return Timedelta(nanoseconds=1)
755-
756747
def tz_localize(self, tz, ambiguous='raise', nonexistent='raise'):
757748
"""
758749
Convert naive Timestamp to local time zone, or remove
@@ -1036,3 +1027,4 @@ cdef int64_t _NS_LOWER_BOUND = -9223372036854775000
10361027
# Resolution is in nanoseconds
10371028
Timestamp.min = Timestamp(_NS_LOWER_BOUND)
10381029
Timestamp.max = Timestamp(_NS_UPPER_BOUND)
1030+
Timestamp.resolution = Timedelta(nanoseconds=1) # GH#21336, GH#21365

pandas/tests/scalar/timedelta/test_timedelta.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,13 @@ def test_resolution_string(self):
804804
def test_resolution_deprecated(self):
805805
# GH#21344
806806
td = Timedelta(days=4, hours=3)
807-
with tm.assert_produces_warning(FutureWarning) as w:
808-
td.resolution
809-
assert "Use Timedelta.resolution_string instead" in str(w[0].message)
807+
result = td.resolution
808+
assert result == Timedelta(nanoseconds=1)
809+
810+
# Check that the attribute is available on the class, mirroring
811+
# the stdlib timedelta behavior
812+
result = Timedelta.resolution
813+
assert result == Timedelta(nanoseconds=1)
810814

811815

812816
@pytest.mark.parametrize(

pandas/tests/scalar/timestamp/test_timestamp.py

+4
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ def test_resolution(self):
192192
dt = Timestamp("2100-01-01 00:00:00")
193193
assert dt.resolution == Timedelta(nanoseconds=1)
194194

195+
# Check that the attribute is available on the class, mirroring
196+
# the stdlib datetime behavior
197+
assert Timestamp.resolution == Timedelta(nanoseconds=1)
198+
195199

196200
class TestTimestampConstructors:
197201
def test_constructor(self):

0 commit comments

Comments
 (0)