Skip to content
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

Doc: resolve GL08 for pandas.Timedelta.microseconds, unit & value #57616

Merged
merged 3 commits into from
Feb 28, 2024
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
3 changes: 0 additions & 3 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Series.dt.qyear\
pandas.Series.dt.unit\
pandas.Series.empty\
pandas.Timedelta.microseconds\
pandas.Timedelta.unit\
pandas.Timedelta.value\
pandas.Timestamp.day\
pandas.Timestamp.fold\
pandas.Timestamp.hour\
Expand Down
68 changes: 68 additions & 0 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,25 @@ cdef class _Timedelta(timedelta):

@property
def value(self):
"""
Return the value of Timedelta object in nanoseconds.

Return the total seconds, milliseconds and microseconds
of the timedelta as nanoseconds.

Returns
-------
int

See Also
--------
Timedelta.unit : Return the unit of Timedelta object.

Examples
--------
>>> pd.Timedelta(1, "us").value
1000
"""
try:
return convert_reso(self._value, self._creso, NPY_FR_ns, False)
except OverflowError:
Expand Down Expand Up @@ -1120,6 +1139,37 @@ cdef class _Timedelta(timedelta):
def microseconds(self) -> int: # TODO(cython3): make cdef property
# NB: using the python C-API PyDateTime_DELTA_GET_MICROSECONDS will fail
# (or be incorrect)
"""
Return the number of microseconds (n), where 0 <= n < 1 millisecond.

Timedelta.microseconds = milliseconds * 1000 + microseconds.

Returns
-------
int
Number of microseconds.

See Also
--------
Timedelta.components : Return all attributes with assigned values
(i.e. days, hours, minutes, seconds, milliseconds, microseconds,
nanoseconds).

Examples
--------
**Using string input**

>>> td = pd.Timedelta('1 days 2 min 3 us')

>>> td.microseconds
3

**Using integer input**

>>> td = pd.Timedelta(42, unit='us')
>>> td.microseconds
42
"""
self._ensure_components()
return self._ms * 1000 + self._us

Expand All @@ -1141,6 +1191,24 @@ cdef class _Timedelta(timedelta):

@property
def unit(self) -> str:
"""
Return the unit of Timedelta object.

The unit of Timedelta object is nanosecond, i.e., 'ns' by default.

Returns
-------
str

See Also
--------
Timedelta.value : Return the value of Timedelta object in nanoseconds.
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add Timedelta.as_unit here too?


Examples
--------
>>> td = pd.Timedelta(42, unit='us')
'ns'
"""
return npy_unit_to_abbrev(self._creso)

def __hash__(_Timedelta self):
Expand Down