forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnumeric.py
384 lines (306 loc) · 11.3 KB
/
numeric.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from typing import Any
import warnings
import numpy as np
from pandas._libs import index as libindex, lib
from pandas._typing import Dtype, DtypeObj, Label
from pandas.util._decorators import doc
from pandas.core.dtypes.common import (
is_bool,
is_bool_dtype,
is_dtype_equal,
is_float,
is_float_dtype,
is_integer_dtype,
is_number,
is_numeric_dtype,
is_scalar,
is_signed_integer_dtype,
is_unsigned_integer_dtype,
)
from pandas.core.dtypes.generic import ABCSeries
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna
import pandas.core.common as com
from pandas.core.indexes.base import Index, maybe_extract_name
_num_index_shared_docs = {}
class NumericIndex(Index):
"""
Provide numeric type operations.
This is an abstract class.
"""
_default_dtype: np.dtype
_is_numeric_dtype = True
_can_hold_strings = False
def __new__(cls, data=None, dtype=None, copy=False, name=None):
cls._validate_dtype(dtype)
name = maybe_extract_name(name, data, cls)
# Coerce to ndarray if not already ndarray or Index
if not isinstance(data, (np.ndarray, Index)):
if is_scalar(data):
raise cls._scalar_data_error(data)
# other iterable of some kind
if not isinstance(data, (ABCSeries, list, tuple)):
data = list(data)
data = np.asarray(data, dtype=dtype)
if issubclass(data.dtype.type, str):
cls._string_data_error(data)
if copy or not is_dtype_equal(data.dtype, cls._default_dtype):
subarr = np.array(data, dtype=cls._default_dtype, copy=copy)
cls._assert_safe_casting(data, subarr)
else:
subarr = data
if subarr.ndim > 1:
# GH#13601, GH#20285, GH#27125
raise ValueError("Index data must be 1-dimensional")
subarr = np.asarray(subarr)
return cls._simple_new(subarr, name=name)
@classmethod
def _validate_dtype(cls, dtype: Dtype) -> None:
if dtype is None:
return
validation_metadata = {
"int64index": (is_signed_integer_dtype, "signed integer"),
"uint64index": (is_unsigned_integer_dtype, "unsigned integer"),
"float64index": (is_float_dtype, "float"),
"rangeindex": (is_signed_integer_dtype, "signed integer"),
}
validation_func, expected = validation_metadata[cls._typ]
if not validation_func(dtype):
raise ValueError(
f"Incorrect `dtype` passed: expected {expected}, received {dtype}"
)
# ----------------------------------------------------------------
# Indexing Methods
@doc(Index._maybe_cast_slice_bound)
def _maybe_cast_slice_bound(self, label, side: str, kind):
assert kind in ["loc", "getitem", None]
# we will try to coerce to integers
return self._maybe_cast_indexer(label)
# ----------------------------------------------------------------
@doc(Index._shallow_copy)
def _shallow_copy(self, values=None, name: Label = lib.no_default):
if values is not None and not self._can_hold_na and values.dtype.kind == "f":
name = self.name if name is lib.no_default else name
# Ensure we are not returning an Int64Index with float data:
return Float64Index._simple_new(values, name=name)
return super()._shallow_copy(values=values, name=name)
@doc(Index._validate_fill_value)
def _validate_fill_value(self, value):
if is_bool(value) or is_bool_dtype(value):
# force conversion to object
# so we don't lose the bools
raise TypeError
elif is_scalar(value) and isna(value):
if is_valid_nat_for_dtype(value, self.dtype):
value = self._na_value
if self.dtype.kind != "f":
# raise so that caller can cast
raise TypeError
else:
# NaT, np.datetime64("NaT"), np.timedelta64("NaT")
raise TypeError
elif is_scalar(value):
if not is_number(value):
# e.g. datetime64, timedelta64, datetime, ...
raise TypeError
elif lib.is_complex(value):
# at least until we have a ComplexIndx
raise TypeError
elif is_float(value) and self.dtype.kind != "f":
if not value.is_integer():
raise TypeError
value = int(value)
return value
def _convert_tolerance(self, tolerance, target):
tolerance = np.asarray(tolerance)
if target.size != tolerance.size and tolerance.size > 1:
raise ValueError("list-like tolerance size must match target index size")
if not np.issubdtype(tolerance.dtype, np.number):
if tolerance.ndim > 0:
raise ValueError(
f"tolerance argument for {type(self).__name__} must contain "
"numeric elements if it is list type"
)
else:
raise ValueError(
f"tolerance argument for {type(self).__name__} must be numeric "
f"if it is a scalar: {repr(tolerance)}"
)
return tolerance
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
# If we ever have BoolIndex or ComplexIndex, this may need to be tightened
return is_numeric_dtype(dtype)
@classmethod
def _assert_safe_casting(cls, data, subarr):
"""
Subclasses need to override this only if the process of casting data
from some accepted dtype to the internal dtype(s) bears the risk of
truncation (e.g. float to int).
"""
pass
@property
def _is_all_dates(self) -> bool:
"""
Checks that all the labels are datetime objects.
"""
return False
_num_index_shared_docs[
"class_descr"
] = """
Immutable sequence used for indexing and alignment. The basic object
storing axis labels for all pandas objects. %(klass)s is a special case
of `Index` with purely %(ltype)s labels. %(extra)s.
Parameters
----------
data : array-like (1-dimensional)
dtype : NumPy dtype (default: %(dtype)s)
copy : bool
Make a copy of input ndarray.
name : object
Name to be stored in the index.
Attributes
----------
None
Methods
-------
None
See Also
--------
Index : The base pandas Index type.
Notes
-----
An Index instance can **only** contain hashable objects.
"""
_int64_descr_args = {
"klass": "Int64Index",
"ltype": "integer",
"dtype": "int64",
"extra": "",
}
class IntegerIndex(NumericIndex):
"""
This is an abstract class for Int64Index, UInt64Index.
"""
_default_dtype: np.dtype
_can_hold_na = False
@classmethod
def _assert_safe_casting(cls, data, subarr):
"""
Ensure incoming data can be represented with matching signed-ness.
"""
if data.dtype.kind != cls._default_dtype.kind:
if not np.array_equal(data, subarr):
raise TypeError("Unsafe NumPy casting, you must explicitly cast")
def __contains__(self, key) -> bool:
"""
Check if key is a float and has a decimal. If it has, return False.
"""
hash(key)
try:
if is_float(key) and int(key) != key:
return False
return key in self._engine
except (OverflowError, TypeError, ValueError):
return False
@property
def inferred_type(self) -> str:
"""
Always 'integer' for ``Int64Index`` and ``UInt64Index``
"""
return "integer"
@property
def asi8(self) -> np.ndarray:
# do not cache or you'll create a memory leak
warnings.warn(
"Index.asi8 is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=2,
)
return self._values.view(self._default_dtype)
class Int64Index(IntegerIndex):
__doc__ = _num_index_shared_docs["class_descr"] % _int64_descr_args
_typ = "int64index"
_engine_type = libindex.Int64Engine
_default_dtype = np.dtype(np.int64)
_uint64_descr_args = {
"klass": "UInt64Index",
"ltype": "unsigned integer",
"dtype": "uint64",
"extra": "",
}
class UInt64Index(IntegerIndex):
__doc__ = _num_index_shared_docs["class_descr"] % _uint64_descr_args
_typ = "uint64index"
_engine_type = libindex.UInt64Engine
_default_dtype = np.dtype(np.uint64)
# ----------------------------------------------------------------
# Indexing Methods
@doc(Index._convert_arr_indexer)
def _convert_arr_indexer(self, keyarr):
# Cast the indexer to uint64 if possible so that the values returned
# from indexing are also uint64.
dtype = None
if is_integer_dtype(keyarr) or (
lib.infer_dtype(keyarr, skipna=False) == "integer"
):
dtype = np.uint64
return com.asarray_tuplesafe(keyarr, dtype=dtype)
_float64_descr_args = {
"klass": "Float64Index",
"dtype": "float64",
"ltype": "float",
"extra": "",
}
class Float64Index(NumericIndex):
__doc__ = _num_index_shared_docs["class_descr"] % _float64_descr_args
_typ = "float64index"
_engine_type = libindex.Float64Engine
_default_dtype = np.dtype(np.float64)
@property
def inferred_type(self) -> str:
"""
Always 'floating' for ``Float64Index``
"""
return "floating"
# ----------------------------------------------------------------
# Indexing Methods
@doc(Index._should_fallback_to_positional)
def _should_fallback_to_positional(self) -> bool:
return False
@doc(Index._convert_slice_indexer)
def _convert_slice_indexer(self, key: slice, kind: str):
assert kind in ["loc", "getitem"]
# We always treat __getitem__ slicing as label-based
# translate to locations
return self.slice_indexer(key.start, key.stop, key.step, kind=kind)
@doc(Index.get_loc)
def get_loc(self, key, method=None, tolerance=None):
if is_bool(key):
# Catch this to avoid accidentally casting to 1.0
raise KeyError(key)
if is_float(key) and np.isnan(key):
nan_idxs = self._nan_idxs
if not len(nan_idxs):
raise KeyError(key)
elif len(nan_idxs) == 1:
return nan_idxs[0]
return nan_idxs
return super().get_loc(key, method=method, tolerance=tolerance)
# ----------------------------------------------------------------
def _format_native_types(
self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
):
from pandas.io.formats.format import FloatArrayFormatter
formatter = FloatArrayFormatter(
self._values,
na_rep=na_rep,
float_format=float_format,
decimal=decimal,
quoting=quoting,
fixed_width=False,
)
return formatter.get_result_as_array()
def __contains__(self, other: Any) -> bool:
hash(other)
if super().__contains__(other):
return True
return is_float(other) and np.isnan(other) and self.hasnans