Skip to content

ENH: DTI/TDI as_unit #50616

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 6 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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: 2 additions & 0 deletions doc/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ Conversion
.. autosummary::
:toctree: api/

DatetimeIndex.as_unit
DatetimeIndex.to_period
DatetimeIndex.to_pydatetime
DatetimeIndex.to_series
Expand Down Expand Up @@ -423,6 +424,7 @@ Conversion
.. autosummary::
:toctree: api/

TimedeltaIndex.as_unit
TimedeltaIndex.to_pytimedelta
TimedeltaIndex.to_series
TimedeltaIndex.round
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Other enhancements
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
- Improved error message in :func:`to_datetime` for non-ISO8601 formats, informing users about the position of the first error (:issue:`50361`)
- Improved error message when trying to align :class:`DataFrame` objects (for example, in :func:`DataFrame.compare`) to clarify that "identically labelled" refers to both index and columns (:issue:`50083`)
- Added :meth:`DatetimeIndex.as_unit` and :meth:`TimedeltaIndex.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`??`)
Copy link
Member

@gfyoung gfyoung Jan 8, 2023

Choose a reason for hiding this comment

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

PR is LGTM besides this formality:

Suggested change
- Added :meth:`DatetimeIndex.as_unit` and :meth:`TimedeltaIndex.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`??`)
- Added :meth:`DatetimeIndex.as_unit` and :meth:`TimedeltaIndex.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`50616`)

-

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):

_join_precedence = 10

@property
def unit(self) -> str:
return self._data.unit

def as_unit(self: _TDT, unit: str) -> _TDT:
arr = self._data.as_unit(unit)
return type(self)._simple_new(arr, name=self.name)

def _with_freq(self, freq):
arr = self._data._with_freq(freq)
return type(self)._simple_new(arr, name=self._name)
Expand Down
13 changes: 3 additions & 10 deletions pandas/tests/indexes/datetimes/methods/test_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
import pandas._testing as tm


def astype_non_nano(dti_nano, unit):
# TODO(2.0): remove once DTI supports as_unit
dta = dti_nano._data.as_unit(unit)
dti = DatetimeIndex(dta, name=dti_nano.name)
return dti


@pytest.mark.parametrize("tz", [None, "Asia/Shanghai", "Europe/Berlin"])
@pytest.mark.parametrize("name", [None, "my_dti"])
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s"])
Expand All @@ -32,12 +25,12 @@ def test_dti_snap(name, tz, unit):
tz=tz,
freq="D",
)
dti = astype_non_nano(dti, unit)
dti = dti.as_unit(unit)

result = dti.snap(freq="W-MON")
expected = date_range("12/31/2001", "1/7/2002", name=name, tz=tz, freq="w-mon")
expected = expected.repeat([3, 4])
expected = astype_non_nano(expected, unit)
expected = expected.as_unit(unit)
tm.assert_index_equal(result, expected)
assert result.tz == expected.tz
assert result.freq is None
Expand All @@ -47,7 +40,7 @@ def test_dti_snap(name, tz, unit):

expected = date_range("1/1/2002", "1/7/2002", name=name, tz=tz, freq="b")
expected = expected.repeat([1, 1, 1, 2, 2])
expected = astype_non_nano(expected, unit)
expected = expected.as_unit(unit)
tm.assert_index_equal(result, expected)
assert result.tz == expected.tz
assert result.freq is None
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,9 +1587,8 @@ def test_convert_non_ns(self):
ser = Series(arr)
assert ser.dtype == arr.dtype

tdi = timedelta_range("00:00:01", periods=3, freq="s")
tda = tdi._data.as_unit("s")
expected = Series(tda)
tdi = timedelta_range("00:00:01", periods=3, freq="s").as_unit("s")
expected = Series(tdi)
assert expected.dtype == arr.dtype
tm.assert_series_equal(ser, expected)

Expand Down