Skip to content

ENH: Timestamp.round: officially accept Timedelta arguments #63687

@randolf-scholz

Description

@randolf-scholz

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Timestamp.round should accept arbitrary Timedelta, so that we don't need to stringify types.

This actually already works at runtime, because internally Timestamp.round parses the freq argument with pandas.tseries.frequencies.to_offset but it is not documented:

import pandas as pd

freq = "1h30min"
offset = pd.Timedelta(freq)

dt = pd.Timestamp.now()
dt.round(freq)  # ✅️ (documented)
dt.round(offset)  # ✅️ (undocumented)
dt.round(str(offset)) # ❌️ fails

If accepted, one should also consider patching pandas.tseries.frequencies.to_offset so that it gives identical results when passing a Timedelta versus casting that timedelta to string. In fact, it seems like internally it should probably just cast the string to timedelta first before processing it further.

Feature Description

  1. Update documentation / type-hints of Timestamp.round to allow Timedelta argument
  2. (Optional): update pandas.tseries.frequencies.to_offset so that to_offset(string_argument) and to_offset(str(Timedelta(string_argument))) always give identical results.

Alternative Solutions


Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions