|
14 | 14 | import pandas.index as _index
|
15 | 15 | from pandas.lib import Timestamp, Timedelta, is_datetime_array
|
16 | 16 | from pandas.core.base import PandasObject, FrozenList, FrozenNDArray, IndexOpsMixin, _shared_docs
|
17 |
| -from pandas.util.decorators import Appender, cache_readonly, deprecate |
| 17 | +from pandas.util.decorators import (Appender, Substitution, cache_readonly, |
| 18 | + deprecate) |
18 | 19 | from pandas.core.common import isnull, array_equivalent
|
19 | 20 | import pandas.core.common as com
|
20 | 21 | from pandas.core.common import (_values_from_object, is_float, is_integer,
|
@@ -2088,12 +2089,13 @@ def _evaluate_with_datetime_like(self, other, op, opstr):
|
2088 | 2089 | def _add_numeric_methods_disabled(cls):
|
2089 | 2090 | """ add in numeric methods to disable """
|
2090 | 2091 |
|
2091 |
| - def _make_invalid_op(opstr): |
| 2092 | + def _make_invalid_op(name): |
2092 | 2093 |
|
2093 |
| - def _invalid_op(self, other=None): |
2094 |
| - raise TypeError("cannot perform {opstr} with this index type: {typ}".format(opstr=opstr, |
2095 |
| - typ=type(self))) |
2096 |
| - return _invalid_op |
| 2094 | + def invalid_op(self, other=None): |
| 2095 | + raise TypeError("cannot perform {name} with this index type: {typ}".format(name=name, |
| 2096 | + typ=type(self))) |
| 2097 | + invalid_op.__name__ = name |
| 2098 | + return invalid_op |
2097 | 2099 |
|
2098 | 2100 | cls.__mul__ = cls.__rmul__ = _make_invalid_op('__mul__')
|
2099 | 2101 | cls.__floordiv__ = cls.__rfloordiv__ = _make_invalid_op('__floordiv__')
|
@@ -2178,8 +2180,62 @@ def _evaluate_numeric_unary(self):
|
2178 | 2180 | cls.__abs__ = _make_evaluate_unary(lambda x: np.abs(x),'__abs__')
|
2179 | 2181 | cls.__inv__ = _make_evaluate_unary(lambda x: -x,'__inv__')
|
2180 | 2182 |
|
| 2183 | + @classmethod |
| 2184 | + def _add_logical_methods(cls): |
| 2185 | + """ add in logical methods """ |
| 2186 | + |
| 2187 | + _doc = """ |
| 2188 | +
|
| 2189 | + %(desc)s |
| 2190 | +
|
| 2191 | + Parameters |
| 2192 | + ---------- |
| 2193 | + All arguments to numpy.%(outname)s are accepted. |
| 2194 | +
|
| 2195 | + Returns |
| 2196 | + ------- |
| 2197 | + %(outname)s : bool or array_like (if axis is specified) |
| 2198 | + A single element array_like may be converted to bool.""" |
| 2199 | + |
| 2200 | + def _make_logical_function(name, desc, f): |
| 2201 | + |
| 2202 | + @Substitution(outname=name, desc=desc) |
| 2203 | + @Appender(_doc) |
| 2204 | + def logical_func(self, *args, **kwargs): |
| 2205 | + result = f(self.values) |
| 2206 | + if isinstance(result, (np.ndarray, com.ABCSeries, Index)) \ |
| 2207 | + and result.ndim == 0: |
| 2208 | + # return NumPy type |
| 2209 | + return result.dtype.type(result.item()) |
| 2210 | + else: # pragma: no cover |
| 2211 | + return result |
| 2212 | + logical_func.__name__ = name |
| 2213 | + return logical_func |
| 2214 | + |
| 2215 | + cls.all = _make_logical_function( |
| 2216 | + 'all', 'Return whether all elements are True', np.all) |
| 2217 | + cls.any = _make_logical_function( |
| 2218 | + 'any', 'Return whether any element is True', np.any) |
| 2219 | + |
| 2220 | + @classmethod |
| 2221 | + def _add_logical_methods_disabled(cls): |
| 2222 | + """ add in logical methods to disable """ |
| 2223 | + |
| 2224 | + def _make_invalid_op(name): |
| 2225 | + |
| 2226 | + def invalid_op(self, other=None): |
| 2227 | + raise TypeError("cannot perform {name} with this index type: {typ}".format(name=name, |
| 2228 | + typ=type(self))) |
| 2229 | + invalid_op.__name__ = name |
| 2230 | + return invalid_op |
| 2231 | + |
| 2232 | + cls.all = _make_invalid_op('all') |
| 2233 | + cls.any = _make_invalid_op('any') |
| 2234 | + |
2181 | 2235 |
|
2182 | 2236 | Index._add_numeric_methods_disabled()
|
| 2237 | +Index._add_logical_methods() |
| 2238 | + |
2183 | 2239 |
|
2184 | 2240 | class NumericIndex(Index):
|
2185 | 2241 | """
|
@@ -2291,7 +2347,11 @@ def equals(self, other):
|
2291 | 2347 | def _wrap_joined_index(self, joined, other):
|
2292 | 2348 | name = self.name if self.name == other.name else None
|
2293 | 2349 | return Int64Index(joined, name=name)
|
| 2350 | + |
| 2351 | + |
2294 | 2352 | Int64Index._add_numeric_methods()
|
| 2353 | +Int64Index._add_logical_methods() |
| 2354 | + |
2295 | 2355 |
|
2296 | 2356 | class Float64Index(NumericIndex):
|
2297 | 2357 |
|
@@ -2483,7 +2543,10 @@ def isin(self, values, level=None):
|
2483 | 2543 | self._validate_index_level(level)
|
2484 | 2544 | return lib.ismember_nans(self._array_values(), value_set,
|
2485 | 2545 | isnull(list(value_set)).any())
|
| 2546 | + |
| 2547 | + |
2486 | 2548 | Float64Index._add_numeric_methods()
|
| 2549 | +Float64Index._add_logical_methods_disabled() |
2487 | 2550 |
|
2488 | 2551 |
|
2489 | 2552 | class MultiIndex(Index):
|
@@ -4436,7 +4499,11 @@ def isin(self, values, level=None):
|
4436 | 4499 | return np.zeros(len(labs), dtype=np.bool_)
|
4437 | 4500 | else:
|
4438 | 4501 | return np.lib.arraysetops.in1d(labs, sought_labels)
|
| 4502 | + |
| 4503 | + |
4439 | 4504 | MultiIndex._add_numeric_methods_disabled()
|
| 4505 | +MultiIndex._add_logical_methods_disabled() |
| 4506 | + |
4440 | 4507 |
|
4441 | 4508 | # For utility purposes
|
4442 | 4509 |
|
|
0 commit comments