Skip to content

Commit 7183439

Browse files
verakaiTomAugspurger
authored andcommitted
DOC: update the IntervalIndex.from_array docstring (#20224)
1 parent 79200d3 commit 7183439

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

pandas/core/indexes/interval.py

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def from_breaks(cls, breaks, closed='right', name=None, copy=False,
457457
def from_arrays(cls, left, right, closed='right', name=None, copy=False,
458458
dtype=None):
459459
"""
460-
Construct an IntervalIndex from a a left and right array
460+
Construct from two arrays defining the left and right bounds.
461461
462462
Parameters
463463
----------
@@ -471,11 +471,38 @@ def from_arrays(cls, left, right, closed='right', name=None, copy=False,
471471
name : object, optional
472472
Name to be stored in the index.
473473
copy : boolean, default False
474-
copy the data
475-
dtype : dtype or None, default None
476-
If None, dtype will be inferred
474+
Copy the data.
475+
dtype : dtype, optional
476+
If None, dtype will be inferred.
477477
478-
..versionadded:: 0.23.0
478+
.. versionadded:: 0.23.0
479+
480+
Returns
481+
-------
482+
index : IntervalIndex
483+
484+
Notes
485+
-----
486+
Each element of `left` must be less than or equal to the `right`
487+
element at the same position. If an element is missing, it must be
488+
missing in both `left` and `right`. A TypeError is raised when
489+
using an unsupported type for `left` or `right`. At the moment,
490+
'category', 'object', and 'string' subtypes are not supported.
491+
492+
Raises
493+
------
494+
ValueError
495+
When a value is missing in only one of `left` or `right`.
496+
When a value in `left` is greater than the corresponding value
497+
in `right`.
498+
499+
See Also
500+
--------
501+
interval_range : Function to create a fixed frequency IntervalIndex.
502+
IntervalIndex.from_breaks : Construct an IntervalIndex from an array of
503+
splits.
504+
IntervalIndex.from_tuples : Construct an IntervalIndex from a
505+
list/array of tuples.
479506
480507
Examples
481508
--------
@@ -484,13 +511,29 @@ def from_arrays(cls, left, right, closed='right', name=None, copy=False,
484511
closed='right',
485512
dtype='interval[int64]')
486513
487-
See Also
488-
--------
489-
interval_range : Function to create a fixed frequency IntervalIndex
490-
IntervalIndex.from_breaks : Construct an IntervalIndex from an array of
491-
splits
492-
IntervalIndex.from_tuples : Construct an IntervalIndex from a
493-
list/array of tuples
514+
If you want to segment different groups of people based on
515+
ages, you can apply the method as follows:
516+
517+
>>> ages = pd.IntervalIndex.from_arrays([0, 2, 13],
518+
... [2, 13, 19], closed='left')
519+
>>> ages
520+
IntervalIndex([[0, 2), [2, 13), [13, 19)]
521+
closed='left',
522+
dtype='interval[int64]')
523+
>>> s = pd.Series(['baby', 'kid', 'teen'], ages)
524+
>>> s
525+
[0, 2) baby
526+
[2, 13) kid
527+
[13, 19) teen
528+
dtype: object
529+
530+
Values may be missing, but they must be missing in both arrays.
531+
532+
>>> pd.IntervalIndex.from_arrays([0, np.nan, 13],
533+
... [2, np.nan, 19])
534+
IntervalIndex([(0.0, 2.0], nan, (13.0, 19.0]]
535+
closed='right',
536+
dtype='interval[float64]')
494537
"""
495538
left = maybe_convert_platform_interval(left)
496539
right = maybe_convert_platform_interval(right)

0 commit comments

Comments
 (0)