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

ENH: Arithmetic with Timestamp-based intervals #35908

Open
gerritholl opened this issue Aug 26, 2020 · 6 comments
Open

ENH: Arithmetic with Timestamp-based intervals #35908

gerritholl opened this issue Aug 26, 2020 · 6 comments
Assignees
Labels
Datetime Datetime data dtype Enhancement Interval Interval data type Numeric Operations Arithmetic, Comparison, and Logical operations

Comments

@gerritholl
Copy link

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.

@gerritholl gerritholl added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 26, 2020
@mroeschke
Copy link
Member

IIRC arithmetic between integers and Timestamp was deprecated + removed some time ago cc @jbrockmendel

In [3]: In [94]: pd.Interval(iv.left - pd.Timestamp("1900-01-01"), iv.right - pd.Timestamp("1900-01-01"))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-1d12c6d4a390> in <module>
----> 1 pd.Interval(iv.left - pd.Timestamp("1900-01-01"), iv.right - pd.Timestamp("1900-01-01"))

TypeError: unsupported operand type(s) for -: 'int' and 'Timestamp'

In [4]: pd.__version__
Out[4]: '1.2.0.dev0+174.g9c5c0f2cf.dirty'

Therefore, I don't think it will be likely to support arithmetic between an Interval with integers and a Timestamp

@gerritholl
Copy link
Author

My intervals contain only Timestamp, no integers. I want to subtract a Timestamp from another Timestamp. I hope that isn't deprecated.

@mroeschke
Copy link
Member

Ah sorry, I had copied the incorrect example locally.

Yes, that request sounds reasonable. Given your integer example works, TImestamp arithmetic should work as well.

@mroeschke mroeschke added Interval Interval data type Numeric Operations Arithmetic, Comparison, and Logical operations Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 26, 2020
@souris-dev
Copy link
Contributor

Sounds good, it would be great if I could work on this. Do we need further discussion about this feature?

@mroeschke
Copy link
Member

Go for it @souris-dev

@souris-dev
Copy link
Contributor

take

@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Sep 9, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Enhancement Interval Interval data type Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants