Skip to content

Commit 7e23a37

Browse files
author
Khor Chean Wei
authored
ENH: consistency of input args for boundaries - Interval (#46522)
1 parent 9292530 commit 7e23a37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1046
-681
lines changed

asv_bench/benchmarks/reshape.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ def setup(self, bins):
268268
self.datetime_series = pd.Series(
269269
np.random.randint(N, size=N), dtype="datetime64[ns]"
270270
)
271-
self.interval_bins = pd.IntervalIndex.from_breaks(np.linspace(0, N, bins))
271+
self.interval_bins = pd.IntervalIndex.from_breaks(
272+
np.linspace(0, N, bins), "right"
273+
)
272274

273275
def time_cut_int(self, bins):
274276
pd.cut(self.int_series, bins)

doc/redirects.csv

+2-2
Original file line numberDiff line numberDiff line change
@@ -741,11 +741,11 @@ generated/pandas.Index.values,../reference/api/pandas.Index.values
741741
generated/pandas.Index.view,../reference/api/pandas.Index.view
742742
generated/pandas.Index.where,../reference/api/pandas.Index.where
743743
generated/pandas.infer_freq,../reference/api/pandas.infer_freq
744-
generated/pandas.Interval.closed,../reference/api/pandas.Interval.closed
744+
generated/pandas.Interval.inclusive,../reference/api/pandas.Interval.inclusive
745745
generated/pandas.Interval.closed_left,../reference/api/pandas.Interval.closed_left
746746
generated/pandas.Interval.closed_right,../reference/api/pandas.Interval.closed_right
747747
generated/pandas.Interval,../reference/api/pandas.Interval
748-
generated/pandas.IntervalIndex.closed,../reference/api/pandas.IntervalIndex.closed
748+
generated/pandas.IntervalIndex.inclusive,../reference/api/pandas.IntervalIndex.inclusive
749749
generated/pandas.IntervalIndex.contains,../reference/api/pandas.IntervalIndex.contains
750750
generated/pandas.IntervalIndex.from_arrays,../reference/api/pandas.IntervalIndex.from_arrays
751751
generated/pandas.IntervalIndex.from_breaks,../reference/api/pandas.IntervalIndex.from_breaks

doc/source/reference/arrays.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ Properties
303303
.. autosummary::
304304
:toctree: api/
305305

306-
Interval.closed
306+
Interval.inclusive
307307
Interval.closed_left
308308
Interval.closed_right
309309
Interval.is_empty
@@ -340,7 +340,7 @@ A collection of intervals may be stored in an :class:`arrays.IntervalArray`.
340340
341341
arrays.IntervalArray.left
342342
arrays.IntervalArray.right
343-
arrays.IntervalArray.closed
343+
arrays.IntervalArray.inclusive
344344
arrays.IntervalArray.mid
345345
arrays.IntervalArray.length
346346
arrays.IntervalArray.is_empty

doc/source/reference/indexing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ IntervalIndex components
242242
IntervalIndex.left
243243
IntervalIndex.right
244244
IntervalIndex.mid
245-
IntervalIndex.closed
245+
IntervalIndex.inclusive
246246
IntervalIndex.length
247247
IntervalIndex.values
248248
IntervalIndex.is_empty

doc/source/user_guide/advanced.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ Trying to select an ``Interval`` that is not exactly contained in the ``Interval
10201020
10211021
In [7]: df.loc[pd.Interval(0.5, 2.5)]
10221022
---------------------------------------------------------------------------
1023-
KeyError: Interval(0.5, 2.5, closed='right')
1023+
KeyError: Interval(0.5, 2.5, inclusive='right')
10241024
10251025
Selecting all ``Intervals`` that overlap a given ``Interval`` can be performed using the
10261026
:meth:`~IntervalIndex.overlaps` method to create a boolean indexer.

doc/source/whatsnew/v0.20.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ Selecting via a specific interval:
448448

449449
.. ipython:: python
450450
451-
df.loc[pd.Interval(1.5, 3.0)]
451+
df.loc[pd.Interval(1.5, 3.0, "right")]
452452
453453
Selecting via a scalar value that is contained *in* the intervals.
454454

doc/source/whatsnew/v0.25.0.rst

+7-7
Original file line numberDiff line numberDiff line change
@@ -584,18 +584,18 @@ this would previously return ``True`` for any ``Interval`` overlapping an ``Inte
584584

585585
.. code-block:: python
586586
587-
In [4]: pd.Interval(1, 2, closed='neither') in ii
587+
In [4]: pd.Interval(1, 2, inclusive='neither') in ii
588588
Out[4]: True
589589
590-
In [5]: pd.Interval(-10, 10, closed='both') in ii
590+
In [5]: pd.Interval(-10, 10, inclusive='both') in ii
591591
Out[5]: True
592592
593593
*New behavior*:
594594

595595
.. ipython:: python
596596
597-
pd.Interval(1, 2, closed='neither') in ii
598-
pd.Interval(-10, 10, closed='both') in ii
597+
pd.Interval(1, 2, inclusive='neither') in ii
598+
pd.Interval(-10, 10, inclusive='both') in ii
599599
600600
The :meth:`~IntervalIndex.get_loc` method now only returns locations for exact matches to ``Interval`` queries, as opposed to the previous behavior of
601601
returning locations for overlapping matches. A ``KeyError`` will be raised if an exact match is not found.
@@ -619,7 +619,7 @@ returning locations for overlapping matches. A ``KeyError`` will be raised if a
619619
620620
In [7]: ii.get_loc(pd.Interval(2, 6))
621621
---------------------------------------------------------------------------
622-
KeyError: Interval(2, 6, closed='right')
622+
KeyError: Interval(2, 6, inclusive='right')
623623
624624
Likewise, :meth:`~IntervalIndex.get_indexer` and :meth:`~IntervalIndex.get_indexer_non_unique` will also only return locations for exact matches
625625
to ``Interval`` queries, with ``-1`` denoting that an exact match was not found.
@@ -680,11 +680,11 @@ Similarly, a ``KeyError`` will be raised for non-exact matches instead of return
680680
681681
In [6]: s[pd.Interval(2, 3)]
682682
---------------------------------------------------------------------------
683-
KeyError: Interval(2, 3, closed='right')
683+
KeyError: Interval(2, 3, inclusive='right')
684684
685685
In [7]: s.loc[pd.Interval(2, 3)]
686686
---------------------------------------------------------------------------
687-
KeyError: Interval(2, 3, closed='right')
687+
KeyError: Interval(2, 3, inclusive='right')
688688
689689
The :meth:`~IntervalIndex.overlaps` method can be used to create a boolean indexer that replicates the
690690
previous behavior of returning overlapping matches.

doc/source/whatsnew/v1.5.0.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,12 @@ Other Deprecations
669669
- Deprecated the methods :meth:`DataFrame.mad`, :meth:`Series.mad`, and the corresponding groupby methods (:issue:`11787`)
670670
- Deprecated positional arguments to :meth:`Index.join` except for ``other``, use keyword-only arguments instead of positional arguments (:issue:`46518`)
671671
- Deprecated indexing on a timezone-naive :class:`DatetimeIndex` using a string representing a timezone-aware datetime (:issue:`46903`, :issue:`36148`)
672-
-
672+
- Deprecated the ``closed`` argument in :class:`Interval` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
673+
- Deprecated the ``closed`` argument in :class:`IntervalIndex` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
674+
- Deprecated the ``closed`` argument in :class:`IntervalDtype` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
675+
- Deprecated the ``closed`` argument in :class:`IntervalArray` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
676+
- Deprecated the ``closed`` argument in :class:`intervaltree` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
677+
- Deprecated the ``closed`` argument in :class:`ArrowInterval` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`)
673678

674679
.. ---------------------------------------------------------------------------
675680
.. _whatsnew_150.performance:

pandas/_libs/interval.pyi

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from typing import (
1111
import numpy as np
1212
import numpy.typing as npt
1313

14+
from pandas._libs import lib
1415
from pandas._typing import (
1516
IntervalClosedType,
1617
Timedelta,
@@ -54,19 +55,24 @@ class IntervalMixin:
5455
def is_empty(self) -> bool: ...
5556
def _check_closed_matches(self, other: IntervalMixin, name: str = ...) -> None: ...
5657

58+
def _warning_interval(
59+
inclusive, closed
60+
) -> tuple[IntervalClosedType, lib.NoDefault]: ...
61+
5762
class Interval(IntervalMixin, Generic[_OrderableT]):
5863
@property
5964
def left(self: Interval[_OrderableT]) -> _OrderableT: ...
6065
@property
6166
def right(self: Interval[_OrderableT]) -> _OrderableT: ...
6267
@property
63-
def closed(self) -> IntervalClosedType: ...
68+
def inclusive(self) -> IntervalClosedType: ...
6469
mid: _MidDescriptor
6570
length: _LengthDescriptor
6671
def __init__(
6772
self,
6873
left: _OrderableT,
6974
right: _OrderableT,
75+
inclusive: IntervalClosedType = ...,
7076
closed: IntervalClosedType = ...,
7177
) -> None: ...
7278
def __hash__(self) -> int: ...
@@ -157,7 +163,7 @@ class IntervalTree(IntervalMixin):
157163
self,
158164
left: np.ndarray,
159165
right: np.ndarray,
160-
closed: IntervalClosedType = ...,
166+
inclusive: IntervalClosedType = ...,
161167
leaf_size: int = ...,
162168
) -> None: ...
163169
@property

0 commit comments

Comments
 (0)