Skip to content

Commit 4a4635d

Browse files
gfyoungjreback
authored andcommitted
DOC: Refactor numeric index docs
Refactor `NumericIndex` docs to avoid duplicate documentation. xref #14937. Author: gfyoung <[email protected]> Closes #14985 from gfyoung/numeric-doc-refactor and squashes the following commits: ba80766 [gfyoung] DOC: Refactor numeric index docs
1 parent 1311add commit 4a4635d

File tree

2 files changed

+60
-81
lines changed

2 files changed

+60
-81
lines changed

pandas/indexes/base.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -1128,17 +1128,17 @@ def is_mixed(self):
11281128
def holds_integer(self):
11291129
return self.inferred_type in ['integer', 'mixed-integer']
11301130

1131-
# validate / convert indexers
1132-
def _convert_scalar_indexer(self, key, kind=None):
1133-
"""
1134-
convert a scalar indexer
1131+
_index_shared_docs['_convert_scalar_indexer'] = """
1132+
Convert a scalar indexer.
11351133
11361134
Parameters
11371135
----------
11381136
key : label of the slice bound
11391137
kind : {'ix', 'loc', 'getitem', 'iloc'} or None
1140-
"""
1138+
"""
11411139

1140+
@Appender(_index_shared_docs['_convert_scalar_indexer'])
1141+
def _convert_scalar_indexer(self, key, kind=None):
11421142
assert kind in ['ix', 'loc', 'getitem', 'iloc', None]
11431143

11441144
if kind == 'iloc':
@@ -1173,15 +1173,20 @@ def _convert_scalar_indexer(self, key, kind=None):
11731173

11741174
return key
11751175

1176-
def _convert_slice_indexer(self, key, kind=None):
1177-
"""
1178-
convert a slice indexer. disallow floats in the start/stop/step
1176+
_index_shared_docs['_convert_slice_indexer'] = """
1177+
Convert a slice indexer.
1178+
1179+
By definition, these are labels unless 'iloc' is passed in.
1180+
Floats are not allowed as the start, step, or stop of the slice.
11791181
11801182
Parameters
11811183
----------
11821184
key : label of the slice bound
11831185
kind : {'ix', 'loc', 'getitem', 'iloc'} or None
1184-
"""
1186+
"""
1187+
1188+
@Appender(_index_shared_docs['_convert_slice_indexer'])
1189+
def _convert_slice_indexer(self, key, kind=None):
11851190
assert kind in ['ix', 'loc', 'getitem', 'iloc', None]
11861191

11871192
# if we are not a slice, then we are done
@@ -2102,9 +2107,8 @@ def _get_unique_index(self, dropna=False):
21022107

21032108
return self._shallow_copy(values)
21042109

2105-
def get_loc(self, key, method=None, tolerance=None):
2106-
"""
2107-
Get integer location for requested label
2110+
_index_shared_docs['get_loc'] = """
2111+
Get integer location for requested label.
21082112
21092113
Parameters
21102114
----------
@@ -2125,7 +2129,10 @@ def get_loc(self, key, method=None, tolerance=None):
21252129
Returns
21262130
-------
21272131
loc : int if unique index, possibly slice or mask if not
2128-
"""
2132+
"""
2133+
2134+
@Appender(_index_shared_docs['get_loc'])
2135+
def get_loc(self, key, method=None, tolerance=None):
21292136
if method is None:
21302137
if tolerance is not None:
21312138
raise ValueError('tolerance argument only valid if using pad, '
@@ -3047,8 +3054,7 @@ def _validate_indexer(self, form, key, kind):
30473054
self._invalid_indexer(form, key)
30483055
return key
30493056

3050-
def _maybe_cast_slice_bound(self, label, side, kind):
3051-
"""
3057+
_index_shared_docs['_maybe_cast_slice_bound'] = """
30523058
This function should be overloaded in subclasses that allow non-trivial
30533059
casting on label-slice bounds, e.g. datetime-like indices allowing
30543060
strings containing formatted datetimes.
@@ -3068,6 +3074,9 @@ def _maybe_cast_slice_bound(self, label, side, kind):
30683074
Value of `side` parameter should be validated in caller.
30693075
30703076
"""
3077+
3078+
@Appender(_index_shared_docs['_maybe_cast_slice_bound'])
3079+
def _maybe_cast_slice_bound(self, label, side, kind):
30713080
assert kind in ['ix', 'loc', 'getitem', None]
30723081

30733082
# We are a plain index here (sub-class override this method if they

pandas/indexes/numeric.py

+36-66
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import pandas.indexes.base as ibase
1717

1818

19+
_num_index_shared_docs = dict()
20+
21+
1922
class NumericIndex(Index):
2023
"""
2124
Provide numeric type operations
@@ -47,27 +50,8 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
4750
name = data.name
4851
return cls._simple_new(subarr, name=name)
4952

53+
@Appender(_index_shared_docs['_maybe_cast_slice_bound'])
5054
def _maybe_cast_slice_bound(self, label, side, kind):
51-
"""
52-
This function should be overloaded in subclasses that allow non-trivial
53-
casting on label-slice bounds, e.g. datetime-like indices allowing
54-
strings containing formatted datetimes.
55-
56-
Parameters
57-
----------
58-
label : object
59-
side : {'left', 'right'}
60-
kind : {'ix', 'loc', 'getitem'}
61-
62-
Returns
63-
-------
64-
label : object
65-
66-
Notes
67-
-----
68-
Value of `side` parameter should be validated in caller.
69-
70-
"""
7155
assert kind in ['ix', 'loc', 'getitem', None]
7256

7357
# we will try to coerce to integers
@@ -90,27 +74,37 @@ def _assert_safe_casting(cls, data, subarr):
9074
pass
9175

9276

93-
class Int64Index(NumericIndex):
94-
"""
77+
_num_index_shared_docs['class_descr'] = """
9578
Immutable ndarray implementing an ordered, sliceable set. The basic object
96-
storing axis labels for all pandas objects. Int64Index is a special case
97-
of `Index` with purely integer labels. This is the default index type used
98-
by the DataFrame and Series ctors when no explicit index is provided by the
99-
user.
79+
storing axis labels for all pandas objects. %(klass)s is a special case
80+
of `Index` with purely %(ltype)s labels. %(extra)s
10081
10182
Parameters
10283
----------
10384
data : array-like (1-dimensional)
104-
dtype : NumPy dtype (default: int64)
85+
dtype : NumPy dtype (default: %(dtype)s)
10586
copy : bool
10687
Make a copy of input ndarray
10788
name : object
10889
Name to be stored in the index
109-
11090
Notes
11191
-----
112-
An Index instance can **only** contain hashable objects
113-
"""
92+
An Index instance can **only** contain hashable objects.
93+
"""
94+
95+
_int64_descr_args = dict(
96+
klass='Int64Index',
97+
ltype='integer',
98+
dtype='int64',
99+
extra="""This is the default index type used
100+
by the DataFrame and Series ctors when no explicit
101+
index is provided by the user.
102+
"""
103+
)
104+
105+
106+
class Int64Index(NumericIndex):
107+
__doc__ = _num_index_shared_docs['class_descr'] % _int64_descr_args
114108

115109
_typ = 'int64index'
116110
_arrmap = _algos.arrmap_int64
@@ -141,16 +135,8 @@ def is_all_dates(self):
141135
"""
142136
return False
143137

138+
@Appender(_index_shared_docs['_convert_scalar_indexer'])
144139
def _convert_scalar_indexer(self, key, kind=None):
145-
"""
146-
convert a scalar indexer
147-
148-
Parameters
149-
----------
150-
key : label of the slice bound
151-
kind : {'ix', 'loc', 'getitem'} or None
152-
"""
153-
154140
assert kind in ['ix', 'loc', 'getitem', 'iloc', None]
155141

156142
# don't coerce ilocs to integers
@@ -177,25 +163,16 @@ def _assert_safe_casting(cls, data, subarr):
177163
Int64Index._add_logical_methods()
178164

179165

180-
class Float64Index(NumericIndex):
181-
"""
182-
Immutable ndarray implementing an ordered, sliceable set. The basic object
183-
storing axis labels for all pandas objects. Float64Index is a special case
184-
of `Index` with purely floating point labels.
166+
_float64_descr_args = dict(
167+
klass='Float64Index',
168+
dtype='float64',
169+
ltype='float',
170+
extra=''
171+
)
185172

186-
Parameters
187-
----------
188-
data : array-like (1-dimensional)
189-
dtype : NumPy dtype (default: object)
190-
copy : bool
191-
Make a copy of input ndarray
192-
name : object
193-
Name to be stored in the index
194173

195-
Notes
196-
-----
197-
An Float64Index instance can **only** contain hashable objects
198-
"""
174+
class Float64Index(NumericIndex):
175+
__doc__ = _num_index_shared_docs['class_descr'] % _float64_descr_args
199176

200177
_typ = 'float64index'
201178
_engine_type = _index.Float64Engine
@@ -228,6 +205,7 @@ def astype(self, dtype, copy=True):
228205
self.__class__)
229206
return Index(values, name=self.name, dtype=dtype)
230207

208+
@Appender(_index_shared_docs['_convert_scalar_indexer'])
231209
def _convert_scalar_indexer(self, key, kind=None):
232210
"""
233211
convert a scalar indexer
@@ -245,17 +223,8 @@ def _convert_scalar_indexer(self, key, kind=None):
245223

246224
return key
247225

226+
@Appender(_index_shared_docs['_convert_slice_indexer'])
248227
def _convert_slice_indexer(self, key, kind=None):
249-
"""
250-
convert a slice indexer, by definition these are labels
251-
unless we are iloc
252-
253-
Parameters
254-
----------
255-
key : label of the slice bound
256-
kind : optional, type of the indexing operation (loc/ix/iloc/None)
257-
"""
258-
259228
# if we are not a slice, then we are done
260229
if not isinstance(key, slice):
261230
return key
@@ -325,6 +294,7 @@ def __contains__(self, other):
325294
except:
326295
return False
327296

297+
@Appender(_index_shared_docs['get_loc'])
328298
def get_loc(self, key, method=None, tolerance=None):
329299
try:
330300
if np.all(np.isnan(key)):

0 commit comments

Comments
 (0)