From b3bb7f57a10778e96b860c7356d36e8b0fdc0adc Mon Sep 17 00:00:00 2001 From: verakai Date: Sat, 10 Mar 2018 11:58:39 -0300 Subject: [PATCH 1/7] update pandas.IntervalIndex docstring --- pandas/core/indexes/interval.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index ccf2e5e3c4486..1b7b7af7dccf0 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -142,20 +142,20 @@ class IntervalIndex(IntervalMixin, Index): Parameters ---------- - data : array-like (1-dimensional) + data : array-Like (1-dimensional) Array-like containing Interval objects from which to build the - IntervalIndex + IntervalIndex. closed : {'left', 'right', 'both', 'neither'}, default 'right' Whether the intervals are closed on the left-side, right-side, both or neither. name : object, optional Name to be stored in the index. copy : boolean, default False - Copy the meta-data + Copy the meta-data. dtype : dtype or None, default None - If None, dtype will be inferred + If None, dtype will be inferred. - ..versionadded:: 0.23.0 + ..versionadded:: 0.23.0. Attributes ---------- @@ -198,11 +198,11 @@ class IntervalIndex(IntervalMixin, Index): See Also -------- - Index : The base pandas Index type - Interval : A bounded slice-like interval; the elements of an IntervalIndex - interval_range : Function to create a fixed frequency IntervalIndex - cut, qcut : Convert arrays of continuous data into Categoricals/Series of - Intervals + Index : The base pandas Index type. + Interval : A bounded slice-like interval; the elements of an IntervalIndex. + qcut : Quantile-based discretization function. + cut : Return indices of half-open bins to which each value of x belongs. + interval_range : Function to create a fixed frequency IntervalIndex. """ _typ = 'intervalindex' _comparables = ['name'] From 7c8dcee08cb4eadb73f6842700de4242be2c0b64 Mon Sep 17 00:00:00 2001 From: verakai Date: Sat, 10 Mar 2018 12:46:19 -0300 Subject: [PATCH 2/7] updating IntervalIndex parameters --- pandas/core/indexes/interval.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 1b7b7af7dccf0..6ee9376c118b4 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -148,13 +148,17 @@ class IntervalIndex(IntervalMixin, Index): closed : {'left', 'right', 'both', 'neither'}, default 'right' Whether the intervals are closed on the left-side, right-side, both or neither. - name : object, optional - Name to be stored in the index. - copy : boolean, default False - Copy the meta-data. dtype : dtype or None, default None If None, dtype will be inferred. - + copy : boolean, default False + Copy the meta-data. + name : object, optional + Name to be stored in the index. + fastpath : boolean, default False + Create IntervalIndex without verifying integrity. + verify_integrity : boolean, default True + Verify that the IntervalIndex is valid. + ..versionadded:: 0.23.0. Attributes From dbac88658ba7353fb58e553c30b80bd781c2671f Mon Sep 17 00:00:00 2001 From: verakai Date: Sat, 10 Mar 2018 16:34:24 -0300 Subject: [PATCH 3/7] updating return, summary, example and some linting --- pandas/core/indexes/interval.py | 42 ++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 6ee9376c118b4..79a79ac239468 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -158,7 +158,7 @@ class IntervalIndex(IntervalMixin, Index): Create IntervalIndex without verifying integrity. verify_integrity : boolean, default True Verify that the IntervalIndex is valid. - + ..versionadded:: 0.23.0. Attributes @@ -461,7 +461,8 @@ def from_breaks(cls, breaks, closed='right', name=None, copy=False, def from_arrays(cls, left, right, closed='right', name=None, copy=False, dtype=None): """ - Construct an IntervalIndex from a a left and right array + Construct an IntervalIndex from a given element in a left + and right array. Parameters ---------- @@ -475,11 +476,15 @@ def from_arrays(cls, left, right, closed='right', name=None, copy=False, name : object, optional Name to be stored in the index. copy : boolean, default False - copy the data + Copy the data. dtype : dtype or None, default None - If None, dtype will be inferred + If None, dtype will be inferred. - ..versionadded:: 0.23.0 + .. versionadded:: 0.23.0. + + Returns + ------- + index : IntervalIndex Examples -------- @@ -488,13 +493,34 @@ def from_arrays(cls, left, right, closed='right', name=None, copy=False, closed='right', dtype='interval[int64]') + If you want to segment different groups of people based on + ages, you can apply the method as follows: + + >>> ages = pd.IntervalIndex.from_arrays([0, 2, 13], + ... [2, 13, 19], closed='left') + >>> ages + IntervalIndex([[0, 2), [2, 13), [13, 19)] + closed='left', + dtype='interval[int64]') + >>> s = pd.Series(['baby', 'kid', 'teen'], ages) + >>> s + [0, 2) baby + [2, 13) kid + [13, 19) teen + dtype: object + + Notes + ----- + Each element of `left` must be smaller or equal to the `right` element + at the same position, ie, ``left[i] <= right[i]``. + See Also -------- - interval_range : Function to create a fixed frequency IntervalIndex + interval_range : Function to create a fixed frequency IntervalIndex. IntervalIndex.from_breaks : Construct an IntervalIndex from an array of - splits + splits. IntervalIndex.from_tuples : Construct an IntervalIndex from a - list/array of tuples + list/array of tuples. """ left = maybe_convert_platform_interval(left) right = maybe_convert_platform_interval(right) From 591c741b81d351f6c28389001503fdee73f2e60c Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 13 Mar 2018 14:47:47 -0500 Subject: [PATCH 4/7] Revert class changes --- pandas/core/indexes/interval.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 79a79ac239468..1a40fb64bfe3b 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -142,16 +142,16 @@ class IntervalIndex(IntervalMixin, Index): Parameters ---------- - data : array-Like (1-dimensional) + data : array-like (1-dimensional) Array-like containing Interval objects from which to build the - IntervalIndex. + IntervalIndex closed : {'left', 'right', 'both', 'neither'}, default 'right' Whether the intervals are closed on the left-side, right-side, both or neither. - dtype : dtype or None, default None - If None, dtype will be inferred. copy : boolean, default False - Copy the meta-data. + Copy the meta-data + dtype : dtype or None, default None + If None, dtype will be inferred name : object, optional Name to be stored in the index. fastpath : boolean, default False @@ -159,7 +159,7 @@ class IntervalIndex(IntervalMixin, Index): verify_integrity : boolean, default True Verify that the IntervalIndex is valid. - ..versionadded:: 0.23.0. + ..versionadded:: 0.23.0 Attributes ---------- @@ -202,11 +202,11 @@ class IntervalIndex(IntervalMixin, Index): See Also -------- - Index : The base pandas Index type. - Interval : A bounded slice-like interval; the elements of an IntervalIndex. - qcut : Quantile-based discretization function. - cut : Return indices of half-open bins to which each value of x belongs. - interval_range : Function to create a fixed frequency IntervalIndex. + Index : The base pandas Index type + Interval : A bounded slice-like interval; the elements of an IntervalIndex + interval_range : Function to create a fixed frequency IntervalIndex + cut, qcut : Convert arrays of continuous data into Categoricals/Series of + Intervals """ _typ = 'intervalindex' _comparables = ['name'] From 982ed02855306406675652836d6d9ce2e5213a2e Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 13 Mar 2018 14:55:55 -0500 Subject: [PATCH 5/7] Updated --- pandas/core/indexes/interval.py | 47 ++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 1a40fb64bfe3b..f069767dcfbe0 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -461,8 +461,7 @@ def from_breaks(cls, breaks, closed='right', name=None, copy=False, def from_arrays(cls, left, right, closed='right', name=None, copy=False, dtype=None): """ - Construct an IntervalIndex from a given element in a left - and right array. + Construct from two arrays defining the left and right bounds. Parameters ---------- @@ -477,15 +476,38 @@ def from_arrays(cls, left, right, closed='right', name=None, copy=False, Name to be stored in the index. copy : boolean, default False Copy the data. - dtype : dtype or None, default None + dtype : dtype, optional If None, dtype will be inferred. - .. versionadded:: 0.23.0. + .. versionadded:: 0.23.0 Returns ------- index : IntervalIndex + Notes + ----- + Each element of `left` must be less than or equal to the `right` + element at the same position. If an element is missing, it must be + missing in both `left` and `right`. A TypeError is raised when + using an unsupported type for `left` or `right`. At the moment, + 'category', 'object', and 'string' subtypes are not supported. + + Raises + ------ + ValueError + When a value is missing in only one of `left` or `right`. + When a value in `left` is greater than the corresponding value + in `right`. + + See Also + -------- + interval_range : Function to create a fixed frequency IntervalIndex. + IntervalIndex.from_breaks : Construct an IntervalIndex from an array of + splits. + IntervalIndex.from_tuples : Construct an IntervalIndex from a + list/array of tuples. + Examples -------- >>> pd.IntervalIndex.from_arrays([0, 1, 2], [1, 2, 3]) @@ -500,27 +522,14 @@ def from_arrays(cls, left, right, closed='right', name=None, copy=False, ... [2, 13, 19], closed='left') >>> ages IntervalIndex([[0, 2), [2, 13), [13, 19)] - closed='left', - dtype='interval[int64]') + closed='left', + dtype='interval[int64]') >>> s = pd.Series(['baby', 'kid', 'teen'], ages) >>> s [0, 2) baby [2, 13) kid [13, 19) teen dtype: object - - Notes - ----- - Each element of `left` must be smaller or equal to the `right` element - at the same position, ie, ``left[i] <= right[i]``. - - See Also - -------- - interval_range : Function to create a fixed frequency IntervalIndex. - IntervalIndex.from_breaks : Construct an IntervalIndex from an array of - splits. - IntervalIndex.from_tuples : Construct an IntervalIndex from a - list/array of tuples. """ left = maybe_convert_platform_interval(left) right = maybe_convert_platform_interval(right) From a97170486aea6ff2ac4a22f0df64d5e628817e0e Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 13 Mar 2018 15:00:46 -0500 Subject: [PATCH 6/7] Revert changes [ci skip] --- pandas/core/indexes/interval.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index f069767dcfbe0..ad5b8d3e67d8d 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -148,16 +148,12 @@ class IntervalIndex(IntervalMixin, Index): closed : {'left', 'right', 'both', 'neither'}, default 'right' Whether the intervals are closed on the left-side, right-side, both or neither. + name : object, optional + Name to be stored in the index. copy : boolean, default False Copy the meta-data dtype : dtype or None, default None If None, dtype will be inferred - name : object, optional - Name to be stored in the index. - fastpath : boolean, default False - Create IntervalIndex without verifying integrity. - verify_integrity : boolean, default True - Verify that the IntervalIndex is valid. ..versionadded:: 0.23.0 From 277a0c5202f741e6a2083de7fd2deba81d7c9ed8 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 13 Mar 2018 15:09:05 -0500 Subject: [PATCH 7/7] NA example [ci skip] --- pandas/core/indexes/interval.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index ad5b8d3e67d8d..54800d0d76d2e 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -526,6 +526,14 @@ def from_arrays(cls, left, right, closed='right', name=None, copy=False, [2, 13) kid [13, 19) teen dtype: object + + Values may be missing, but they must be missing in both arrays. + + >>> pd.IntervalIndex.from_arrays([0, np.nan, 13], + ... [2, np.nan, 19]) + IntervalIndex([(0.0, 2.0], nan, (13.0, 19.0]] + closed='right', + dtype='interval[float64]') """ left = maybe_convert_platform_interval(left) right = maybe_convert_platform_interval(right)