Skip to content

Commit

Permalink
BUG/DEPR: Timestamp/Timedelta resolution (pandas-dev#29910)
Browse files Browse the repository at this point in the history
* BUG/DEPR: Timestamp/Timedelta resolution

* GH ref
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent 45b70de commit 666da19
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 62 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
- Removed previously deprecated keyword "n" from :meth:`DatetimeIndex.shift`, :meth:`TimedeltaIndex.shift`, :meth:`PeriodIndex.shift`, use "periods" instead (:issue:`22458`)
- 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>`,
- :func:`Series.expanding().apply() <pandas.core.window.Expanding.apply>`, and :func:`DataFrame.expanding().apply() <pandas.core.window.Expanding.apply>` to ``False`` (:issue:`20584`)
- 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`)
- Removed previously deprecated :attr:`Timestamp.weekday_name`, :attr:`DatetimeIndex.weekday_name`, and :attr:`Series.dt.weekday_name` (:issue:`18164`)
- Removed previously deprecated ``errors`` argument in :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` (:issue:`22644`)
-
Expand Down Expand Up @@ -516,6 +517,7 @@ Datetimelike
- 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`)
- Bug in :meth:`Series.var` failing to raise ``TypeError`` when called with ``timedelta64[ns]`` dtype (:issue:`28289`)
- Bug in :meth:`DatetimeIndex.strftime` and :meth:`Series.dt.strftime` where ``NaT`` was converted to the string ``'NaT'`` instead of ``np.nan`` (:issue:`29578`)
- Bug in :attr:`Timestamp.resolution` being a property instead of a class attribute (:issue:`29910`)

Timedelta
^^^^^^^^^
Expand Down
51 changes: 1 addition & 50 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1005,56 +1005,6 @@ cdef class _Timedelta(timedelta):
else:
return "D"

@property
def resolution(self):
"""
Return a string representing the lowest timedelta resolution.
Each timedelta has a defined resolution that represents the lowest OR
most granular level of precision. Each level of resolution is
represented by a short string as defined below:
Resolution: Return value
* Days: 'D'
* Hours: 'H'
* Minutes: 'T'
* Seconds: 'S'
* Milliseconds: 'L'
* Microseconds: 'U'
* Nanoseconds: 'N'
Returns
-------
str
Timedelta resolution.
Examples
--------
>>> td = pd.Timedelta('1 days 2 min 3 us 42 ns')
>>> td.resolution
'N'
>>> td = pd.Timedelta('1 days 2 min 3 us')
>>> td.resolution
'U'
>>> td = pd.Timedelta('2 min 3 s')
>>> td.resolution
'S'
>>> td = pd.Timedelta(36, unit='us')
>>> td.resolution
'U'
"""
# See GH#21344
warnings.warn("Timedelta.resolution is deprecated, in a future "
"version will behave like the standard library "
"datetime.timedelta.resolution attribute. "
"Use Timedelta.resolution_string instead.",
FutureWarning)
return self.resolution_string

@property
def nanoseconds(self):
"""
Expand Down Expand Up @@ -1602,3 +1552,4 @@ cdef _broadcast_floordiv_td64(int64_t value, object other,
# resolution in ns
Timedelta.min = Timedelta(np.iinfo(np.int64).min + 1)
Timedelta.max = Timedelta(np.iinfo(np.int64).max)
Timedelta.resolution = Timedelta(nanoseconds=1)
10 changes: 1 addition & 9 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,6 @@ timedelta}, default 'raise'
"""
return bool(ccalendar.is_leapyear(self.year))

@property
def resolution(self):
"""
Return resolution describing the smallest difference between two
times that can be represented by Timestamp object_state.
"""
# GH#21336, GH#21365
return Timedelta(nanoseconds=1)

def tz_localize(self, tz, ambiguous='raise', nonexistent='raise'):
"""
Convert naive Timestamp to local time zone, or remove
Expand Down Expand Up @@ -1036,3 +1027,4 @@ cdef int64_t _NS_LOWER_BOUND = -9223372036854775000
# Resolution is in nanoseconds
Timestamp.min = Timestamp(_NS_LOWER_BOUND)
Timestamp.max = Timestamp(_NS_UPPER_BOUND)
Timestamp.resolution = Timedelta(nanoseconds=1) # GH#21336, GH#21365
10 changes: 7 additions & 3 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,13 @@ def test_resolution_string(self):
def test_resolution_deprecated(self):
# GH#21344
td = Timedelta(days=4, hours=3)
with tm.assert_produces_warning(FutureWarning) as w:
td.resolution
assert "Use Timedelta.resolution_string instead" in str(w[0].message)
result = td.resolution
assert result == Timedelta(nanoseconds=1)

# Check that the attribute is available on the class, mirroring
# the stdlib timedelta behavior
result = Timedelta.resolution
assert result == Timedelta(nanoseconds=1)


@pytest.mark.parametrize(
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def test_resolution(self):
dt = Timestamp("2100-01-01 00:00:00")
assert dt.resolution == Timedelta(nanoseconds=1)

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


class TestTimestampConstructors:
def test_constructor(self):
Expand Down

0 comments on commit 666da19

Please sign in to comment.