Skip to content

Commit bd20fdc

Browse files
authored
DOC: EX01 (Index and RangeIndex) (#53920)
* Example for pd.Index.asof_locs * Example for pd.Index.get_slice_bound * Example for pd.RangeIndex * Example for pd.RangeIndex.start/stop/step * Example for pd.RangeIndex.from_range * cleanup for now * fix doctest * example for RangeIndex.from_range is wrong * retrigger checks * retrigger checks * retrigger checks
1 parent 1b33bf6 commit bd20fdc

File tree

3 files changed

+115
-16
lines changed

3 files changed

+115
-16
lines changed

ci/code_checks.sh

-7
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
138138
pandas.util.hash_pandas_object \
139139
pandas_object \
140140
pandas.api.interchange.from_dataframe \
141-
pandas.Index.asof_locs \
142-
pandas.Index.get_slice_bound \
143-
pandas.RangeIndex \
144-
pandas.RangeIndex.start \
145-
pandas.RangeIndex.stop \
146-
pandas.RangeIndex.step \
147-
pandas.RangeIndex.from_range \
148141
pandas.CategoricalIndex.codes \
149142
pandas.CategoricalIndex.categories \
150143
pandas.CategoricalIndex.ordered \

pandas/core/indexes/base.py

+49-8
Original file line numberDiff line numberDiff line change
@@ -5670,29 +5670,49 @@ def asof_locs(
56705670
"""
56715671
Return the locations (indices) of labels in the index.
56725672
5673-
As in the `asof` function, if the label (a particular entry in
5674-
`where`) is not in the index, the latest index label up to the
5673+
As in the :meth:`pandas.Index.asof`, if the label (a particular entry in
5674+
``where``) is not in the index, the latest index label up to the
56755675
passed label is chosen and its index returned.
56765676
5677-
If all of the labels in the index are later than a label in `where`,
5677+
If all of the labels in the index are later than a label in ``where``,
56785678
-1 is returned.
56795679
5680-
`mask` is used to ignore NA values in the index during calculation.
5680+
``mask`` is used to ignore ``NA`` values in the index during calculation.
56815681
56825682
Parameters
56835683
----------
56845684
where : Index
56855685
An Index consisting of an array of timestamps.
56865686
mask : np.ndarray[bool]
56875687
Array of booleans denoting where values in the original
5688-
data are not NA.
5688+
data are not ``NA``.
56895689
56905690
Returns
56915691
-------
56925692
np.ndarray[np.intp]
5693-
An array of locations (indices) of the labels from the Index
5694-
which correspond to the return values of the `asof` function
5695-
for every element in `where`.
5693+
An array of locations (indices) of the labels from the index
5694+
which correspond to the return values of :meth:`pandas.Index.asof`
5695+
for every element in ``where``.
5696+
5697+
See Also
5698+
--------
5699+
Index.asof : Return the label from the index, or, if not present, the
5700+
previous one.
5701+
5702+
Examples
5703+
--------
5704+
>>> idx = pd.date_range('2023-06-01', periods=3, freq='D')
5705+
>>> where = pd.DatetimeIndex(['2023-05-30 00:12:00', '2023-06-01 00:00:00',
5706+
... '2023-06-02 23:59:59'])
5707+
>>> mask = np.ones(3, dtype=bool)
5708+
>>> idx.asof_locs(where, mask)
5709+
array([-1, 0, 1])
5710+
5711+
We can use ``mask`` to ignore certain values in the index during calculation.
5712+
5713+
>>> mask[1] = False
5714+
>>> idx.asof_locs(where, mask)
5715+
array([-1, 0, 0])
56965716
"""
56975717
# error: No overload variant of "searchsorted" of "ndarray" matches argument
56985718
# types "Union[ExtensionArray, ndarray[Any, Any]]", "str"
@@ -6627,6 +6647,27 @@ def get_slice_bound(self, label, side: Literal["left", "right"]) -> int:
66276647
-------
66286648
int
66296649
Index of label.
6650+
6651+
See Also
6652+
--------
6653+
Index.get_loc : Get integer location, slice or boolean mask for requested
6654+
label.
6655+
6656+
Examples
6657+
--------
6658+
>>> idx = pd.RangeIndex(5)
6659+
>>> idx.get_slice_bound(3, 'left')
6660+
3
6661+
6662+
>>> idx.get_slice_bound(3, 'right')
6663+
4
6664+
6665+
If ``label`` is non-unique in the index, an error will be raised.
6666+
6667+
>>> idx_duplicate = pd.Index(['a', 'b', 'a', 'c', 'd'])
6668+
>>> idx_duplicate.get_slice_bound('a', 'left')
6669+
Traceback (most recent call last):
6670+
KeyError: Cannot get left slice bound for non-unique label: 'a'
66306671
"""
66316672

66326673
if side not in ("left", "right"):

pandas/core/indexes/range.py

+66-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ class RangeIndex(Index):
9595
See Also
9696
--------
9797
Index : The base pandas Index type.
98+
99+
Examples
100+
--------
101+
>>> list(pd.RangeIndex(5))
102+
[0, 1, 2, 3, 4]
103+
104+
>>> list(pd.RangeIndex(-2, 4))
105+
[-2, -1, 0, 1, 2, 3]
106+
107+
>>> list(pd.RangeIndex(0, 10, 2))
108+
[0, 2, 4, 6, 8]
109+
110+
>>> list(pd.RangeIndex(2, -10, -3))
111+
[2, -1, -4, -7]
112+
113+
>>> list(pd.RangeIndex(0))
114+
[]
115+
116+
>>> list(pd.RangeIndex(1, 0))
117+
[]
98118
"""
99119

100120
_typ = "rangeindex"
@@ -148,11 +168,19 @@ def __new__(
148168
@classmethod
149169
def from_range(cls, data: range, name=None, dtype: Dtype | None = None) -> Self:
150170
"""
151-
Create RangeIndex from a range object.
171+
Create :class:`pandas.RangeIndex` from a ``range`` object.
152172
153173
Returns
154174
-------
155175
RangeIndex
176+
177+
Examples
178+
--------
179+
>>> pd.RangeIndex.from_range(range(5))
180+
RangeIndex(start=0, stop=5, step=1)
181+
182+
>>> pd.RangeIndex.from_range(range(2, -10, -3))
183+
RangeIndex(start=2, stop=-10, step=-3)
156184
"""
157185
if not isinstance(data, range):
158186
raise TypeError(
@@ -252,6 +280,16 @@ def _format_with_header(self, header: list[str], na_rep: str) -> list[str]:
252280
def start(self) -> int:
253281
"""
254282
The value of the `start` parameter (``0`` if this was not supplied).
283+
284+
Examples
285+
--------
286+
>>> idx = pd.RangeIndex(5)
287+
>>> idx.start
288+
0
289+
290+
>>> idx = pd.RangeIndex(2, -10, -3)
291+
>>> idx.start
292+
2
255293
"""
256294
# GH 25710
257295
return self._range.start
@@ -260,13 +298,40 @@ def start(self) -> int:
260298
def stop(self) -> int:
261299
"""
262300
The value of the `stop` parameter.
301+
302+
Examples
303+
--------
304+
>>> idx = pd.RangeIndex(5)
305+
>>> idx.stop
306+
5
307+
308+
>>> idx = pd.RangeIndex(2, -10, -3)
309+
>>> idx.stop
310+
-10
263311
"""
264312
return self._range.stop
265313

266314
@property
267315
def step(self) -> int:
268316
"""
269317
The value of the `step` parameter (``1`` if this was not supplied).
318+
319+
Examples
320+
--------
321+
>>> idx = pd.RangeIndex(5)
322+
>>> idx.step
323+
1
324+
325+
>>> idx = pd.RangeIndex(2, -10, -3)
326+
>>> idx.step
327+
-3
328+
329+
Even if :class:`pandas.RangeIndex` is empty, ``step`` is still ``1`` if
330+
not supplied.
331+
332+
>>> idx = pd.RangeIndex(1, 0)
333+
>>> idx.step
334+
1
270335
"""
271336
# GH 25710
272337
return self._range.step

0 commit comments

Comments
 (0)