Skip to content

ENH: cut return bins should be Timestamp/Timedelta objects for datelike data #19891

Closed
@mroeschke

Description

@mroeschke
In [1]: s = pd.Series(pd.date_range('20180101', periods=3))

In [2]: s
Out[2]:
0   2018-01-01
1   2018-01-02
2   2018-01-03
dtype: datetime64[ns]

In [3]: res, bins = pd.cut(s, 2, retbins=True)

In [4]: pd.cut(s, bins)  # Bins do not "roundtrip"

ValueError: bins must be of datetime64 dtype

In [5]: pd.cut(s, [pd.Timestamp(b) for b in bins]) == res
Out[5]:
0    True
1    True
2    True
dtype: bool

For convenience and argument of "roundtripping", I think cut(..., retbins=True) with datelike data (datetime or timedelta data) should return bins that is an array of Timestamp/Timedelta objects instead of an array of integers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeReshapingConcat, Merge/Join, Stack/Unstack, ExplodeTimezonesTimezone data dtype

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions