Skip to content

DEPR: deprecate Timedelta.resolution #26839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ Other Deprecations
Use the public attributes :attr:`~RangeIndex.start`, :attr:`~RangeIndex.stop` and :attr:`~RangeIndex.step` instead (:issue:`26581`).
- The :meth:`Series.ftype`, :meth:`Series.ftypes` and :meth:`DataFrame.ftypes` methods are deprecated and will be removed in a future version.
Instead, use :meth:`Series.dtype` and :meth:`DataFrame.dtypes` (:issue:`26705`).

- :meth:`Timedelta.resolution` is deprecated and replaced with :meth:`Timedelta.reso_str`. In a future version, :meth:`Timedelta.resolution` will be changed to behave like the standard library :attr:`timedelta.resolution` (:issue:`21344`)

.. _whatsnew_0250.prior_deprecations:

Expand Down
53 changes: 51 additions & 2 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ cdef class _Timedelta(timedelta):
return np.int64(self.value).view('m8[ns]')

@property
def resolution(self):
def reso_str(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where did this name come from? maybe min_resolution_string? or just resolution_string is more informative

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More or less chosen out of thin air. I think we might use it in some of the internal functions. resolution_string sounds better, will update

"""
Return a string representing the lowest timedelta resolution.

Expand Down Expand Up @@ -991,7 +991,6 @@ cdef class _Timedelta(timedelta):
>>> td.resolution
'U'
"""

self._ensure_components()
if self._ns:
return "N"
Expand All @@ -1008,6 +1007,56 @@ 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.reso_str instead.",
FutureWarning)
return self.reso_str

@property
def nanoseconds(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,11 @@ def _maybe_cast_slice_bound(self, label, side, kind):

if isinstance(label, str):
parsed = Timedelta(label)
lbound = parsed.round(parsed.resolution)
lbound = parsed.round(parsed.reso_str)
if side == 'left':
return lbound
else:
return (lbound + to_offset(parsed.resolution) -
return (lbound + to_offset(parsed.reso_str) -
Timedelta(1, 'ns'))
elif ((is_integer(label) or is_float(label)) and
not is_timedelta64_dtype(label)):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def test_nat_iso_format(get_nat):

@pytest.mark.parametrize("klass,expected", [
(Timestamp, ["freqstr", "normalize", "to_julian_date", "to_period", "tz"]),
(Timedelta, ["components", "delta", "is_populated", "to_pytimedelta",
"to_timedelta64", "view"])
(Timedelta, ["components", "delta", "is_populated", "reso_str",
"to_pytimedelta", "to_timedelta64", "view"])
])
def test_missing_public_nat_methods(klass, expected):
# see gh-17327
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,22 @@ def test_components(self):
assert not result.iloc[0].isna().all()
assert result.iloc[1].isna().all()

def test_reso_str(self):
assert Timedelta(days=1).reso_str == 'D'
assert Timedelta(days=1, hours=6).reso_str == 'H'
assert Timedelta(days=1, minutes=6).reso_str == 'T'
assert Timedelta(days=1, seconds=6).reso_str == 'S'
assert Timedelta(days=1, milliseconds=6).reso_str == 'L'
assert Timedelta(days=1, microseconds=6).reso_str == 'U'
assert Timedelta(days=1, nanoseconds=6).reso_str == 'N'

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.reso_str instead" in str(w[0].message)


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