From a548b8454bc55e0db162157009ddece68bfb46f1 Mon Sep 17 00:00:00 2001 From: heoh Date: Sun, 26 Oct 2025 23:26:30 +0000 Subject: [PATCH 1/2] Replace `@doc` decorator with inlined docstrings --- pandas/core/indexes/range.py | 114 +++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 5 deletions(-) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index a817cae51be5c..e6e2b8cfaf25f 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -27,7 +27,6 @@ from pandas.compat.numpy import function as nv from pandas.util._decorators import ( cache_readonly, - doc, set_module, ) @@ -473,8 +472,44 @@ def inferred_type(self) -> str: # -------------------------------------------------------------------- # Indexing Methods - @doc(Index.get_loc) def get_loc(self, key) -> int: + """ + Get integer location, slice or boolean mask for requested label. + + Parameters + ---------- + key : label + The key to check its location if it is present in the index. + + Returns + ------- + int if unique index, slice if monotonic index, else mask + Integer location, slice or boolean mask. + + See Also + -------- + Index.get_slice_bound : Calculate slice bound that corresponds to + given label. + Index.get_indexer : Computes indexer and mask for new index given + the current index. + Index.get_non_unique : Returns indexer and masks for new index given + the current index. + Index.get_indexer_for : Returns an indexer even when non-unique. + + Examples + -------- + >>> unique_index = pd.Index(list("abc")) + >>> unique_index.get_loc("b") + 1 + + >>> monotonic_index = pd.Index(list("abbc")) + >>> monotonic_index.get_loc("b") + slice(1, 3, None) + + >>> non_monotonic_index = pd.Index(list("abcb")) + >>> non_monotonic_index.get_loc("b") + array([False, True, False, True]) + """ if is_integer(key) or (is_float(key) and key.is_integer()): new_key = int(key) try: @@ -528,12 +563,47 @@ def _should_fallback_to_positional(self) -> bool: def tolist(self) -> list[int]: return list(self._range) - @doc(Index.__iter__) def __iter__(self) -> Iterator[int]: + """ + Return an iterator of the values. + + These are each a scalar type, which is a Python scalar + (for str, int, float) or a pandas scalar + (for Timestamp/Timedelta/Interval/Period) + + Returns + ------- + iterator + An iterator yielding scalar values from the Series. + + See Also + -------- + Series.items : Lazily iterate over (index, value) tuples. + + Examples + -------- + >>> s = pd.Series([1, 2, 3]) + >>> for x in s: + ... print(x) + 1 + 2 + 3 + """ yield from self._range - @doc(Index._shallow_copy) def _shallow_copy(self, values, name: Hashable = no_default): + """ + Create a new Index with the same class as the caller, don't copy the + data, use the same object attributes with passed in attributes taking + precedence. + + *this is an internal non-public method* + + Parameters + ---------- + values : the values to create the new Index, optional + name : Label, defaults to self.name + """ name = self._name if name is no_default else name if values.dtype.kind == "f": @@ -560,8 +630,42 @@ def _wrap_reindex_result(self, target, indexer, preserve_names: bool): target = self._shallow_copy(target._values, name=target.name) return super()._wrap_reindex_result(target, indexer, preserve_names) - @doc(Index.copy) def copy(self, name: Hashable | None = None, deep: bool = False) -> Self: + """ + Make a copy of this object. + + Name is set on the new object. + + Parameters + ---------- + name : Label, optional + Set name for new object. + deep : bool, default False + If True attempts to make a deep copy of the Index. + Else makes a shallow copy. + + Returns + ------- + Index + Index refer to new object which is a copy of this object. + + See Also + -------- + Index.delete: Make new Index with passed location(-s) deleted. + Index.drop: Make new Index with passed list of labels deleted. + + Notes + ----- + In most cases, there should be no functional difference from using + ``deep``, but if ``deep`` is passed it will attempt to deepcopy. + + Examples + -------- + >>> idx = pd.Index(["a", "b", "c"]) + >>> new_idx = idx.copy() + >>> idx is new_idx + False + """ name = self._validate_names(name=name, deep=deep)[0] new_index = self._rename(name=name) return new_index From 39b373af00217848bb6d0c56b65ec6540be0795e Mon Sep 17 00:00:00 2001 From: heoh Date: Mon, 27 Oct 2025 00:17:47 +0000 Subject: [PATCH 2/2] Update the contents of the docstrings for RangeIndex --- pandas/core/indexes/range.py | 77 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index e6e2b8cfaf25f..ac794b18075d0 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -474,41 +474,48 @@ def inferred_type(self) -> str: def get_loc(self, key) -> int: """ - Get integer location, slice or boolean mask for requested label. + Get integer location for requested label. Parameters ---------- - key : label - The key to check its location if it is present in the index. + key : int or float + Label to locate. Integer-like floats (e.g. 3.0) are accepted and + treated as the corresponding integer. Non-integer floats and other + non-integer labels are not valid and will raise KeyError or + InvalidIndexError. Returns ------- - int if unique index, slice if monotonic index, else mask - Integer location, slice or boolean mask. + int + Integer location of the label within the RangeIndex. + + Raises + ------ + KeyError + If the label is not present in the RangeIndex or the label is a + non-integer value. + InvalidIndexError + If the label is of an invalid type for the RangeIndex. See Also -------- - Index.get_slice_bound : Calculate slice bound that corresponds to + RangeIndex.get_slice_bound : Calculate slice bound that corresponds to given label. - Index.get_indexer : Computes indexer and mask for new index given + RangeIndex.get_indexer : Computes indexer and mask for new index given the current index. - Index.get_non_unique : Returns indexer and masks for new index given + RangeIndex.get_non_unique : Returns indexer and masks for new index given the current index. - Index.get_indexer_for : Returns an indexer even when non-unique. + RangeIndex.get_indexer_for : Returns an indexer even when non-unique. Examples -------- - >>> unique_index = pd.Index(list("abc")) - >>> unique_index.get_loc("b") - 1 - - >>> monotonic_index = pd.Index(list("abbc")) - >>> monotonic_index.get_loc("b") - slice(1, 3, None) + >>> idx = pd.RangeIndex(5) + >>> idx.get_loc(3) + 3 - >>> non_monotonic_index = pd.Index(list("abcb")) - >>> non_monotonic_index.get_loc("b") - array([False, True, False, True]) + >>> idx = pd.RangeIndex(2, 10, 2) # values [2, 4, 6, 8] + >>> idx.get_loc(6) + 2 """ if is_integer(key) or (is_float(key) and key.is_integer()): new_key = int(key) @@ -567,33 +574,25 @@ def __iter__(self) -> Iterator[int]: """ Return an iterator of the values. - These are each a scalar type, which is a Python scalar - (for str, int, float) or a pandas scalar - (for Timestamp/Timedelta/Interval/Period) - Returns ------- iterator - An iterator yielding scalar values from the Series. - - See Also - -------- - Series.items : Lazily iterate over (index, value) tuples. + An iterator yielding ints from the RangeIndex. Examples -------- - >>> s = pd.Series([1, 2, 3]) - >>> for x in s: + >>> idx = pd.RangeIndex(3) + >>> for x in idx: ... print(x) + 0 1 2 - 3 """ yield from self._range def _shallow_copy(self, values, name: Hashable = no_default): """ - Create a new Index with the same class as the caller, don't copy the + Create a new RangeIndex with the same class as the caller, don't copy the data, use the same object attributes with passed in attributes taking precedence. @@ -601,7 +600,7 @@ def _shallow_copy(self, values, name: Hashable = no_default): Parameters ---------- - values : the values to create the new Index, optional + values : the values to create the new RangeIndex, optional name : Label, defaults to self.name """ name = self._name if name is no_default else name @@ -641,18 +640,18 @@ def copy(self, name: Hashable | None = None, deep: bool = False) -> Self: name : Label, optional Set name for new object. deep : bool, default False - If True attempts to make a deep copy of the Index. + If True attempts to make a deep copy of the RangeIndex. Else makes a shallow copy. Returns ------- - Index - Index refer to new object which is a copy of this object. + RangeIndex + RangeIndex refer to new object which is a copy of this object. See Also -------- - Index.delete: Make new Index with passed location(-s) deleted. - Index.drop: Make new Index with passed list of labels deleted. + RangeIndex.delete: Make new RangeIndex with passed location(-s) deleted. + RangeIndex.drop: Make new RangeIndex with passed list of labels deleted. Notes ----- @@ -661,7 +660,7 @@ def copy(self, name: Hashable | None = None, deep: bool = False) -> Self: Examples -------- - >>> idx = pd.Index(["a", "b", "c"]) + >>> idx = pd.RangeIndex(3) >>> new_idx = idx.copy() >>> idx is new_idx False