Skip to content

Commit 26c5261

Browse files
committed
DOC: Refactor numeric index docs
1 parent 72786cc commit 26c5261

File tree

2 files changed

+58
-82
lines changed

2 files changed

+58
-82
lines changed

pandas/indexes/base.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -1128,17 +1128,16 @@ 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
1135-
1131+
_index_shared_docs['_convert_scalar_indexer'] = """
1132+
Convert a scalar indexer.
11361133
Parameters
11371134
----------
11381135
key : label of the slice bound
11391136
kind : {'ix', 'loc', 'getitem', 'iloc'} or None
1140-
"""
1137+
"""
11411138

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

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

11741173
return key
11751174

1176-
def _convert_slice_indexer(self, key, kind=None):
1177-
"""
1178-
convert a slice indexer. disallow floats in the start/stop/step
1175+
_index_shared_docs['_convert_slice_indexer'] = """
1176+
Convert a slice indexer.
1177+
1178+
By definition, these are labels unless 'iloc' is passed in.
1179+
Floats are not allowed as the start, step, or stop of the slice.
11791180
11801181
Parameters
11811182
----------
11821183
key : label of the slice bound
11831184
kind : {'ix', 'loc', 'getitem', 'iloc'} or None
1184-
"""
1185+
"""
1186+
1187+
@Appender(_index_shared_docs['_convert_slice_indexer'])
1188+
def _convert_slice_indexer(self, key, kind=None):
11851189
assert kind in ['ix', 'loc', 'getitem', 'iloc', None]
11861190

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

21032107
return self._shallow_copy(values)
21042108

2105-
def get_loc(self, key, method=None, tolerance=None):
2106-
"""
2107-
Get integer location for requested label
2109+
_index_shared_docs['get_loc'] = """
2110+
Get integer location for requested label.
21082111
21092112
Parameters
21102113
----------
@@ -2125,7 +2128,10 @@ def get_loc(self, key, method=None, tolerance=None):
21252128
Returns
21262129
-------
21272130
loc : int if unique index, possibly slice or mask if not
2128-
"""
2131+
"""
2132+
2133+
@Appender(_index_shared_docs['get_loc'])
2134+
def get_loc(self, key, method=None, tolerance=None):
21292135
if method is None:
21302136
if tolerance is not None:
21312137
raise ValueError('tolerance argument only valid if using pad, '
@@ -3047,8 +3053,7 @@ def _validate_indexer(self, form, key, kind):
30473053
self._invalid_indexer(form, key)
30483054
return key
30493055

3050-
def _maybe_cast_slice_bound(self, label, side, kind):
3051-
"""
3056+
_index_shared_docs['_maybe_cast_slice_bound'] = """
30523057
This function should be overloaded in subclasses that allow non-trivial
30533058
casting on label-slice bounds, e.g. datetime-like indices allowing
30543059
strings containing formatted datetimes.
@@ -3068,6 +3073,9 @@ def _maybe_cast_slice_bound(self, label, side, kind):
30683073
Value of `side` parameter should be validated in caller.
30693074
30703075
"""
3076+
3077+
@Appender(_index_shared_docs['_maybe_cast_slice_bound'])
3078+
def _maybe_cast_slice_bound(self, label, side, kind):
30713079
assert kind in ['ix', 'loc', 'getitem', None]
30723080

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

pandas/indexes/numeric.py

+34-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,36 @@ 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 integer 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+
dtype='int64',
98+
extra="""This is the default index type used
99+
by the DataFrame and Series ctors when no explicit
100+
index is provided by the user.
101+
"""
102+
)
103+
104+
105+
class Int64Index(NumericIndex):
106+
__doc__ = _num_index_shared_docs['class_descr'] % _int64_descr_args
114107

115108
_typ = 'int64index'
116109
_arrmap = _algos.arrmap_int64
@@ -141,16 +134,8 @@ def is_all_dates(self):
141134
"""
142135
return False
143136

137+
@Appender(_index_shared_docs['_convert_scalar_indexer'])
144138
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-
154139
assert kind in ['ix', 'loc', 'getitem', 'iloc', None]
155140

156141
# don't coerce ilocs to integers
@@ -177,25 +162,15 @@ def _assert_safe_casting(cls, data, subarr):
177162
Int64Index._add_logical_methods()
178163

179164

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.
165+
_float64_descr_args = dict(
166+
klass='Float64Index',
167+
dtype='float64',
168+
extra=''
169+
)
185170

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
194171

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

200175
_typ = 'float64index'
201176
_engine_type = _index.Float64Engine
@@ -228,6 +203,7 @@ def astype(self, dtype, copy=True):
228203
self.__class__)
229204
return Index(values, name=self.name, dtype=dtype)
230205

206+
@Appender(_index_shared_docs['_convert_scalar_indexer'])
231207
def _convert_scalar_indexer(self, key, kind=None):
232208
"""
233209
convert a scalar indexer
@@ -245,17 +221,8 @@ def _convert_scalar_indexer(self, key, kind=None):
245221

246222
return key
247223

224+
@Appender(_index_shared_docs['_convert_slice_indexer'])
248225
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-
259226
# if we are not a slice, then we are done
260227
if not isinstance(key, slice):
261228
return key
@@ -325,6 +292,7 @@ def __contains__(self, other):
325292
except:
326293
return False
327294

295+
@Appender(_index_shared_docs['get_loc'])
328296
def get_loc(self, key, method=None, tolerance=None):
329297
try:
330298
if np.all(np.isnan(key)):

0 commit comments

Comments
 (0)