Skip to content

ENH: Arithmetic with Timestamp-based intervals #35908

Open
@gerritholl

Description

@gerritholl

Is your feature request related to a problem?

It is currently possible to perform arithmetic on an Interval iff it is an Interval of numbers. For Interval iv, iv - c == Interval(iv.left-c, iv.right-c). This does not work for intervals of Timestamps:

In [90]: iv = pd.Interval(3, 4)

In [91]: pd.Interval(iv.left-2, iv.right-2)
Out[91]: Interval(1, 2, closed='right')

In [92]: iv - 2
Out[92]: Interval(1, 2, closed='right')

In [93]: iv = pd.Interval(pd.Timestamp("1900-01-01"), pd.Timestamp("1900-01-02"))

In [94]: pd.Interval(iv.left - pd.Timestamp("1900-01-01"), iv.right - pd.Timestamp("1900-01-0
    ...: 1"))
Out[94]: Interval(Timedelta('0 days 00:00:00'), Timedelta('1 days 00:00:00'), closed='right')

In [95]: iv - pd.Timestamp("1900-01-01")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-95-b61ec78c0d66> in <module>
----> 1 iv - pd.Timestamp("1900-01-01")

TypeError: unsupported operand type(s) for -: 'pandas._libs.interval.Interval' and 'Timestamp'

Describe the solution you'd like

I would like that the behaviour that iv - b == Interval(iv.left-b, iv.right-b) holds for all types that Interval can hold, including Timestamps and Timedeltas.

API breaking implications

No backward compatibility issues expected.

Describe alternatives you've considered

Users can currently resort to Interval(iv.left-b, iv.right-b). This is more typing and required repeating the expression b.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions