Skip to content

Commit cbbf33c

Browse files
authored
ENH: add unit, as_unit to dt accessor (#51223)
* ENH: add unit, as_unit to dt accessor * GH ref
1 parent cf2b88b commit cbbf33c

File tree

7 files changed

+20
-4
lines changed

7 files changed

+20
-4
lines changed

doc/source/reference/series.rst

+4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ Datetime properties
326326
Series.dt.days_in_month
327327
Series.dt.tz
328328
Series.dt.freq
329+
Series.dt.unit
329330

330331
Datetime methods
331332
^^^^^^^^^^^^^^^^
@@ -346,6 +347,7 @@ Datetime methods
346347
Series.dt.ceil
347348
Series.dt.month_name
348349
Series.dt.day_name
350+
Series.dt.as_unit
349351

350352
Period properties
351353
^^^^^^^^^^^^^^^^^
@@ -370,6 +372,7 @@ Timedelta properties
370372
Series.dt.microseconds
371373
Series.dt.nanoseconds
372374
Series.dt.components
375+
Series.dt.unit
373376

374377
Timedelta methods
375378
^^^^^^^^^^^^^^^^^
@@ -380,6 +383,7 @@ Timedelta methods
380383

381384
Series.dt.to_pytimedelta
382385
Series.dt.total_seconds
386+
Series.dt.as_unit
383387

384388

385389
.. _api.series.str:

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ Other enhancements
292292
- Improved error message in :func:`to_datetime` for non-ISO8601 formats, informing users about the position of the first error (:issue:`50361`)
293293
- 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`)
294294
- 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`)
295+
- 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`)
295296
- Added new argument ``dtype`` to :func:`read_sql` to be consistent with :func:`read_sql_query` (:issue:`50797`)
296297
-
297298

pandas/core/arrays/datetimes.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ def _scalar_type(self) -> type[Timestamp]:
228228
"nanosecond",
229229
]
230230
_other_ops: list[str] = ["date", "time", "timetz"]
231-
_datetimelike_ops: list[str] = _field_ops + _object_ops + _bool_ops + _other_ops
231+
_datetimelike_ops: list[str] = (
232+
_field_ops + _object_ops + _bool_ops + _other_ops + ["unit"]
233+
)
232234
_datetimelike_methods: list[str] = [
233235
"to_period",
234236
"tz_localize",
@@ -240,6 +242,7 @@ def _scalar_type(self) -> type[Timestamp]:
240242
"ceil",
241243
"month_name",
242244
"day_name",
245+
"as_unit",
243246
]
244247

245248
# ndim is inherited from ExtensionArray, must exist to ensure

pandas/core/arrays/timedeltas.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,14 @@ def _scalar_type(self) -> type[Timedelta]:
137137
_bool_ops: list[str] = []
138138
_object_ops: list[str] = ["freq"]
139139
_field_ops: list[str] = ["days", "seconds", "microseconds", "nanoseconds"]
140-
_datetimelike_ops: list[str] = _field_ops + _object_ops + _bool_ops
140+
_datetimelike_ops: list[str] = _field_ops + _object_ops + _bool_ops + ["unit"]
141141
_datetimelike_methods: list[str] = [
142142
"to_pytimedelta",
143143
"total_seconds",
144144
"round",
145145
"floor",
146146
"ceil",
147+
"as_unit",
147148
]
148149

149150
# Note: ndim must be defined to ensure NaT.__richcmp__(TimedeltaArray)

pandas/core/indexes/accessors.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,14 @@ def isocalendar(self):
227227

228228

229229
@delegate_names(
230-
delegate=DatetimeArray, accessors=DatetimeArray._datetimelike_ops, typ="property"
230+
delegate=DatetimeArray,
231+
accessors=DatetimeArray._datetimelike_ops + ["unit"],
232+
typ="property",
231233
)
232234
@delegate_names(
233-
delegate=DatetimeArray, accessors=DatetimeArray._datetimelike_methods, typ="method"
235+
delegate=DatetimeArray,
236+
accessors=DatetimeArray._datetimelike_methods + ["as_unit"],
237+
typ="method",
234238
)
235239
class DatetimeProperties(Properties):
236240
"""

pandas/tests/series/accessors/test_cat_accessor.py

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def test_dt_accessor_api_for_categorical(self, idx):
167167
("floor", ("D",), {}),
168168
("ceil", ("D",), {}),
169169
("asfreq", ("D",), {}),
170+
("as_unit", ("s"), {}),
170171
]
171172
if idx.dtype == "M8[ns]":
172173
# exclude dt64tz since that is already localized and would raise

pandas/tests/series/accessors/test_dt_accessor.py

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"day_name",
5656
"month_name",
5757
"isocalendar",
58+
"as_unit",
5859
]
5960
ok_for_td = TimedeltaArray._datetimelike_ops
6061
ok_for_td_methods = [
@@ -64,6 +65,7 @@
6465
"round",
6566
"floor",
6667
"ceil",
68+
"as_unit",
6769
]
6870

6971

0 commit comments

Comments
 (0)