Skip to content

More tslibs TODOS: small optimizations, trim namespaces #18446

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
96 changes: 49 additions & 47 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,55 @@ cdef class _Timedelta(timedelta):
td=components, seconds=seconds)
return tpl

cdef _Timedelta _round(_Timedelta self, freq, rounder):
# Note: _round is not visible in the python namespace, so methods that
# call it must be defined in _Timedelta, not Timedelta
cdef:
int64_t result, unit

from pandas.tseries.frequencies import to_offset
unit = to_offset(freq).nanos
result = unit * rounder(self.value / float(unit))
return Timedelta(result, unit='ns')

def round(self, freq):
"""
Round the Timedelta to the specified resolution

Returns
-------
a new Timedelta rounded to the given resolution of `freq`

Parameters
----------
freq : a freq string indicating the rounding resolution

Raises
------
ValueError if the freq cannot be converted
"""
return self._round(freq, np.round)

def floor(self, freq):
"""
return a new Timedelta floored to this resolution

Parameters
----------
freq : a freq string indicating the flooring resolution
"""
return self._round(freq, np.floor)

def ceil(self, freq):
"""
return a new Timedelta ceiled to this resolution

Parameters
----------
freq : a freq string indicating the ceiling resolution
"""
return self._round(freq, np.ceil)


# Python front end to C extension type _Timedelta
# This serves as the box for timedelta64
Expand Down Expand Up @@ -865,53 +914,6 @@ class Timedelta(_Timedelta):
object_state = self.value,
return (Timedelta, object_state)

def _round(self, freq, rounder):
cdef:
int64_t result, unit

from pandas.tseries.frequencies import to_offset
unit = to_offset(freq).nanos
result = unit * rounder(self.value / float(unit))
return Timedelta(result, unit='ns')

def round(self, freq):
"""
Round the Timedelta to the specified resolution

Returns
-------
a new Timedelta rounded to the given resolution of `freq`

Parameters
----------
freq : a freq string indicating the rounding resolution

Raises
------
ValueError if the freq cannot be converted
"""
return self._round(freq, np.round)

def floor(self, freq):
"""
return a new Timedelta floored to this resolution

Parameters
----------
freq : a freq string indicating the flooring resolution
"""
return self._round(freq, np.floor)

def ceil(self, freq):
"""
return a new Timedelta ceiled to this resolution

Parameters
----------
freq : a freq string indicating the ceiling resolution
"""
return self._round(freq, np.ceil)

# ----------------------------------------------------------------
# Arithmetic Methods
# TODO: Can some of these be defined in the cython class?
Expand Down
Loading