Skip to content

ENH: add unit, as_unit to dt accessor #51223

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 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions doc/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ Datetime properties
Series.dt.days_in_month
Series.dt.tz
Series.dt.freq
Series.dt.unit

Datetime methods
^^^^^^^^^^^^^^^^
Expand All @@ -346,6 +347,7 @@ Datetime methods
Series.dt.ceil
Series.dt.month_name
Series.dt.day_name
Series.dt.as_unit

Period properties
^^^^^^^^^^^^^^^^^
Expand All @@ -370,6 +372,7 @@ Timedelta properties
Series.dt.microseconds
Series.dt.nanoseconds
Series.dt.components
Series.dt.unit

Timedelta methods
^^^^^^^^^^^^^^^^^
Expand All @@ -380,6 +383,7 @@ Timedelta methods

Series.dt.to_pytimedelta
Series.dt.total_seconds
Series.dt.as_unit


.. _api.series.str:
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 @@ -291,6 +291,7 @@ Other enhancements
- 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:`50616`)
- Added :meth:`Series.dt.unit` and :meth:`Series.dt.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`51223`)
- Added new argument ``dtype`` to :func:`read_sql` to be consistent with :func:`read_sql_query` (:issue:`50797`)
-

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ def _scalar_type(self) -> type[Timestamp]:
"nanosecond",
]
_other_ops: list[str] = ["date", "time", "timetz"]
_datetimelike_ops: list[str] = _field_ops + _object_ops + _bool_ops + _other_ops
_datetimelike_ops: list[str] = (
_field_ops + _object_ops + _bool_ops + _other_ops + ["unit"]
)
_datetimelike_methods: list[str] = [
"to_period",
"tz_localize",
Expand All @@ -240,6 +242,7 @@ def _scalar_type(self) -> type[Timestamp]:
"ceil",
"month_name",
"day_name",
"as_unit",
]

# ndim is inherited from ExtensionArray, must exist to ensure
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ def _scalar_type(self) -> type[Timedelta]:
_bool_ops: list[str] = []
_object_ops: list[str] = ["freq"]
_field_ops: list[str] = ["days", "seconds", "microseconds", "nanoseconds"]
_datetimelike_ops: list[str] = _field_ops + _object_ops + _bool_ops
_datetimelike_ops: list[str] = _field_ops + _object_ops + _bool_ops + ["unit"]
_datetimelike_methods: list[str] = [
"to_pytimedelta",
"total_seconds",
"round",
"floor",
"ceil",
"as_unit",
]

# Note: ndim must be defined to ensure NaT.__richcmp__(TimedeltaArray)
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,14 @@ def isocalendar(self):


@delegate_names(
delegate=DatetimeArray, accessors=DatetimeArray._datetimelike_ops, typ="property"
delegate=DatetimeArray,
accessors=DatetimeArray._datetimelike_ops + ["unit"],
typ="property",
)
@delegate_names(
delegate=DatetimeArray, accessors=DatetimeArray._datetimelike_methods, typ="method"
delegate=DatetimeArray,
accessors=DatetimeArray._datetimelike_methods + ["as_unit"],
typ="method",
)
class DatetimeProperties(Properties):
"""
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/series/accessors/test_cat_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def test_dt_accessor_api_for_categorical(self, idx):
("floor", ("D",), {}),
("ceil", ("D",), {}),
("asfreq", ("D",), {}),
("as_unit", ("s"), {}),
]
if idx.dtype == "M8[ns]":
# exclude dt64tz since that is already localized and would raise
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/series/accessors/test_dt_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"day_name",
"month_name",
"isocalendar",
"as_unit",
]
ok_for_td = TimedeltaArray._datetimelike_ops
ok_for_td_methods = [
Expand All @@ -64,6 +65,7 @@
"round",
"floor",
"ceil",
"as_unit",
]


Expand Down