From 650bb6dccb14abb0db35c04b60cdffd1a8af1034 Mon Sep 17 00:00:00 2001 From: Sourabh Dalodia Date: Fri, 7 Oct 2022 00:45:37 +0530 Subject: [PATCH 1/2] 48832: Added docstrings for Index classes --- pandas/core/indexes/base.py | 20 +++++++++++++ pandas/core/indexes/interval.py | 52 +++++++++++++++++++++++++++++++++ pandas/core/indexes/multi.py | 32 ++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 5ee6213ef7538..00d60a651f7ce 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1040,6 +1040,9 @@ def ravel(self, order: str_t = "C"): return values.ravel(order=order) def view(self, cls=None): + """ + Create a new view from current Index. + """ # we need to see if we are subclassing an # index type here @@ -1842,6 +1845,23 @@ def _get_default_index_names( return names def _get_names(self) -> FrozenList: + """ + Returns an array of index names. + + Returns + ------- + FrozenList + + Examples + -------- + >>> arrays = [[1, 1, 2], ['red', 'blue', 'green'], ['apple', 'orange', 'apple']] + >>> m1 = pd.MultiIndex.from_arrays(arrays, names=('number', 'color', 'fruits')) + >>> m1.names + FrozenList(['number', 'color', 'fruits']) + >>> m2 = pd.MultiIndex.from_arrays(arrays) + >>> m2.names + FrozenList([None, None, None]) + """ return FrozenList((self.name,)) def _set_names(self, values, *, level=None) -> None: diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 3eb3226328a61..a3c4d7d6e5b20 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -800,18 +800,70 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: @cache_readonly def left(self) -> Index: + """ + Returns an array of left bounds. + + Returns + ------- + :class:~`pandas.Index` + + Examples + -------- + >>> b = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (5, 10), (6, 8)]) + >>> b + IntervalIndex([(0, 1], (2, 3], (5, 10], (6, 8]], dtype='interval[int64, right]') + """ return Index(self._data.left, copy=False) @cache_readonly def right(self) -> Index: + """ + Returns an array of right bounds. + + Returns + ------- + :class:~`pandas.Index` + + Examples + -------- + >>> b = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (5, 10), (6, 8)]) + >>> b.right + Int64Index([1, 3, 10, 8], dtype='int64') + """ return Index(self._data.right, copy=False) @cache_readonly def mid(self) -> Index: + """ + Returns an array of midpoints of the intervals. + + Returns + ------- + :class:~`pandas.Index` + + Examples + -------- + >>> b = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (5, 10), (6, 8)]) + >>> b.mid + Float64Index([0.5, 2.5, 7.5, 7.0], dtype='float64') + """ return Index(self._data.mid, copy=False) @property def length(self) -> Index: + """ + Returns an array of lengths of intervals. + + Returns + ------- + :class:~`pandas.Index` + + Examples + -------- + >>> b = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (5, 10), (6, 8)]) + >>> b.length + Int64Index([1, 1, 5, 2], dtype='int64') + """ return Index(self._data.length, copy=False) # -------------------------------------------------------------------- diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 574dfdf48055d..ed4f3f0d91913 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -800,6 +800,20 @@ def size(self) -> int: @cache_readonly def levels(self) -> FrozenList: + """ + Returns array of arrays of unique values/categories from each index. + + Returns + ------- + FrozenList + + Examples + -------- + >>> arrays = [[1, 1, 2], ['red', 'blue', 'green'], ['apple', 'orange', 'apple']] + >>> m = pd.MultiIndex.from_arrays(arrays, names=('number', 'color', 'fruits')) + >>> m.levels + FrozenList([[1, 2], ['blue', 'green', 'red'], ['apple', 'orange']]) + """ # Use cache_readonly to ensure that self.get_locs doesn't repeatedly # create new IndexEngine # https://github.com/pandas-dev/pandas/issues/31648 @@ -1001,6 +1015,24 @@ def levshape(self) -> Shape: @property def codes(self): + """ + Returns an array of integers which are the positions of the actual values. + + Returns + ------- + FrozenList + + See Also + -------- + Categorical.codes + + Examples + -------- + >>> arrays = [[1, 1, 2], ['red', 'blue', 'green'], ['apple', 'orange', 'apple']] + >>> m = pd.MultiIndex.from_arrays(arrays, names=('number', 'color', 'fruits')) + >>> m.codes + FrozenList([[0, 0, 1], [2, 0, 1], [0, 1, 0]]) + """ return self._codes def _set_codes( From 4fe919740871cefde849aa71513abb02562947c3 Mon Sep 17 00:00:00 2001 From: Sourabh Dalodia Date: Fri, 7 Oct 2022 00:58:52 +0530 Subject: [PATCH 2/2] 48832: Fixed typos --- pandas/core/indexes/base.py | 3 ++- pandas/core/indexes/interval.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 00d60a651f7ce..2df062f0ea52d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1041,7 +1041,7 @@ def ravel(self, order: str_t = "C"): def view(self, cls=None): """ - Create a new view from current Index. + Creates a new view from current Index. """ # we need to see if we are subclassing an @@ -1858,6 +1858,7 @@ def _get_names(self) -> FrozenList: >>> m1 = pd.MultiIndex.from_arrays(arrays, names=('number', 'color', 'fruits')) >>> m1.names FrozenList(['number', 'color', 'fruits']) + >>> m2 = pd.MultiIndex.from_arrays(arrays) >>> m2.names FrozenList([None, None, None]) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index a3c4d7d6e5b20..079a00c2c01ee 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -810,8 +810,8 @@ def left(self) -> Index: Examples -------- >>> b = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (5, 10), (6, 8)]) - >>> b - IntervalIndex([(0, 1], (2, 3], (5, 10], (6, 8]], dtype='interval[int64, right]') + >>> b.left + Int64Index([0, 2, 5, 6], dtype='int64') """ return Index(self._data.left, copy=False)