Skip to content

Commit 0800f80

Browse files
Charlie-XIAOim-vinicius
authored and
im-vinicius
committed
DOC: EX01 (Index and RangeIndex) (pandas-dev#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 7d7ab0c commit 0800f80

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
@@ -5679,29 +5679,49 @@ def asof_locs(
56795679
"""
56805680
Return the locations (indices) of labels in the index.
56815681
5682-
As in the `asof` function, if the label (a particular entry in
5683-
`where`) is not in the index, the latest index label up to the
5682+
As in the :meth:`pandas.Index.asof`, if the label (a particular entry in
5683+
``where``) is not in the index, the latest index label up to the
56845684
passed label is chosen and its index returned.
56855685
5686-
If all of the labels in the index are later than a label in `where`,
5686+
If all of the labels in the index are later than a label in ``where``,
56875687
-1 is returned.
56885688
5689-
`mask` is used to ignore NA values in the index during calculation.
5689+
``mask`` is used to ignore ``NA`` values in the index during calculation.
56905690
56915691
Parameters
56925692
----------
56935693
where : Index
56945694
An Index consisting of an array of timestamps.
56955695
mask : np.ndarray[bool]
56965696
Array of booleans denoting where values in the original
5697-
data are not NA.
5697+
data are not ``NA``.
56985698
56995699
Returns
57005700
-------
57015701
np.ndarray[np.intp]
5702-
An array of locations (indices) of the labels from the Index
5703-
which correspond to the return values of the `asof` function
5704-
for every element in `where`.
5702+
An array of locations (indices) of the labels from the index
5703+
which correspond to the return values of :meth:`pandas.Index.asof`
5704+
for every element in ``where``.
5705+
5706+
See Also
5707+
--------
5708+
Index.asof : Return the label from the index, or, if not present, the
5709+
previous one.
5710+
5711+
Examples
5712+
--------
5713+
>>> idx = pd.date_range('2023-06-01', periods=3, freq='D')
5714+
>>> where = pd.DatetimeIndex(['2023-05-30 00:12:00', '2023-06-01 00:00:00',
5715+
... '2023-06-02 23:59:59'])
5716+
>>> mask = np.ones(3, dtype=bool)
5717+
>>> idx.asof_locs(where, mask)
5718+
array([-1, 0, 1])
5719+
5720+
We can use ``mask`` to ignore certain values in the index during calculation.
5721+
5722+
>>> mask[1] = False
5723+
>>> idx.asof_locs(where, mask)
5724+
array([-1, 0, 0])
57055725
"""
57065726
# error: No overload variant of "searchsorted" of "ndarray" matches argument
57075727
# types "Union[ExtensionArray, ndarray[Any, Any]]", "str"
@@ -6636,6 +6656,27 @@ def get_slice_bound(self, label, side: Literal["left", "right"]) -> int:
66366656
-------
66376657
int
66386658
Index of label.
6659+
6660+
See Also
6661+
--------
6662+
Index.get_loc : Get integer location, slice or boolean mask for requested
6663+
label.
6664+
6665+
Examples
6666+
--------
6667+
>>> idx = pd.RangeIndex(5)
6668+
>>> idx.get_slice_bound(3, 'left')
6669+
3
6670+
6671+
>>> idx.get_slice_bound(3, 'right')
6672+
4
6673+
6674+
If ``label`` is non-unique in the index, an error will be raised.
6675+
6676+
>>> idx_duplicate = pd.Index(['a', 'b', 'a', 'c', 'd'])
6677+
>>> idx_duplicate.get_slice_bound('a', 'left')
6678+
Traceback (most recent call last):
6679+
KeyError: Cannot get left slice bound for non-unique label: 'a'
66396680
"""
66406681

66416682
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)