Python pandas.core.common.is_bool_indexer() Examples
The following are 30
code examples of pandas.core.common.is_bool_indexer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas.core.common
, or try the search function
.
Example #1
Source File: interval.py From vnpy_crypto with MIT License | 6 votes |
def get_value(self, series, key): if com.is_bool_indexer(key): loc = key elif is_list_like(key): loc = self.get_indexer(key) elif isinstance(key, slice): if not (key.step is None or key.step == 1): raise ValueError("cannot support not-default step in a slice") try: loc = self.get_loc(key) except TypeError: # we didn't find exact intervals or are non-unique msg = "unable to slice with this key: {key}".format(key=key) raise ValueError(msg) else: loc = self.get_loc(key) return series.iloc[loc]
Example #2
Source File: indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def _multi_take_opportunity(self, tup): """ Check whether there is the possibility to use ``_multi_take``. Currently the limit is that all axes being indexed must be indexed with list-likes. Parameters ---------- tup : tuple Tuple of indexers, one per axis Returns ------- boolean: Whether the current indexing can be passed through _multi_take """ if not all(is_list_like_indexer(x) for x in tup): return False # just too complicated if any(com.is_bool_indexer(x) for x in tup): return False return True
Example #3
Source File: interval.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def get_value(self, series, key): if com.is_bool_indexer(key): loc = key elif is_list_like(key): loc = self.get_indexer(key) elif isinstance(key, slice): if not (key.step is None or key.step == 1): raise ValueError("cannot support not-default " "step in a slice") try: loc = self.get_loc(key) except TypeError: # we didn't find exact intervals # or are non-unique raise ValueError("unable to slice with " "this key: {}".format(key)) else: loc = self.get_loc(key) return series.iloc[loc]
Example #4
Source File: interval.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def get_value(self, series, key): if com.is_bool_indexer(key): loc = key elif is_list_like(key): loc = self.get_indexer(key) elif isinstance(key, slice): if not (key.step is None or key.step == 1): raise ValueError("cannot support not-default " "step in a slice") try: loc = self.get_loc(key) except TypeError: # we didn't find exact intervals # or are non-unique raise ValueError("unable to slice with " "this key: {}".format(key)) else: loc = self.get_loc(key) return series.iloc[loc]
Example #5
Source File: indexing.py From recruit with Apache License 2.0 | 6 votes |
def _multi_take_opportunity(self, tup): """ Check whether there is the possibility to use ``_multi_take``. Currently the limit is that all axes being indexed must be indexed with list-likes. Parameters ---------- tup : tuple Tuple of indexers, one per axis Returns ------- boolean: Whether the current indexing can be passed through _multi_take """ if not all(is_list_like_indexer(x) for x in tup): return False # just too complicated if any(com.is_bool_indexer(x) for x in tup): return False return True
Example #6
Source File: indexing.py From recruit with Apache License 2.0 | 6 votes |
def check_bool_indexer(ax, key): # boolean indexing, need to check that the data are aligned, otherwise # disallowed # this function assumes that is_bool_indexer(key) == True result = key if isinstance(key, ABCSeries) and not key.index.equals(ax): result = result.reindex(ax) mask = isna(result._values) if mask.any(): raise IndexingError('Unalignable boolean Series provided as ' 'indexer (index of the boolean Series and of ' 'the indexed object do not match') result = result.astype(bool)._values elif is_sparse(result): result = result.to_dense() result = np.asarray(result, dtype=bool) else: # is_bool_indexer has already checked for nulls in the case of an # object array key, so no check needed here result = np.asarray(result, dtype=bool) return result
Example #7
Source File: indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def check_bool_indexer(ax, key): # boolean indexing, need to check that the data are aligned, otherwise # disallowed # this function assumes that is_bool_indexer(key) == True result = key if isinstance(key, ABCSeries) and not key.index.equals(ax): result = result.reindex(ax) mask = isna(result._values) if mask.any(): raise IndexingError('Unalignable boolean Series provided as ' 'indexer (index of the boolean Series and of ' 'the indexed object do not match') result = result.astype(bool)._values elif is_sparse(result): result = result.to_dense() result = np.asarray(result, dtype=bool) else: # is_bool_indexer has already checked for nulls in the case of an # object array key, so no check needed here result = np.asarray(result, dtype=bool) return result
Example #8
Source File: indexing.py From vnpy_crypto with MIT License | 6 votes |
def check_bool_indexer(ax, key): # boolean indexing, need to check that the data are aligned, otherwise # disallowed # this function assumes that is_bool_indexer(key) == True result = key if isinstance(key, ABCSeries) and not key.index.equals(ax): result = result.reindex(ax) mask = isna(result._values) if mask.any(): raise IndexingError('Unalignable boolean Series provided as ' 'indexer (index of the boolean Series and of ' 'the indexed object do not match') result = result.astype(bool)._values elif is_sparse(result): result = result.to_dense() result = np.asarray(result, dtype=bool) else: # is_bool_indexer has already checked for nulls in the case of an # object array key, so no check needed here result = np.asarray(result, dtype=bool) return result
Example #9
Source File: indexing.py From vnpy_crypto with MIT License | 5 votes |
def _validate_key(self, key, axis): if isinstance(key, slice): return True elif com.is_bool_indexer(key): return True elif is_list_like_indexer(key): return True else: self._convert_scalar_indexer(key, axis) return True
Example #10
Source File: dataframe.py From eland with Apache License 2.0 | 5 votes |
def _getitem_array(self, key): if isinstance(key, Series): key = key.to_pandas() if is_bool_indexer(key): if isinstance(key, pd.Series) and not key.index.equals(self.index): warnings.warn( "Boolean Series key will be reindexed to match DataFrame index.", PendingDeprecationWarning, stacklevel=3, ) elif len(key) != len(self.index): raise ValueError( f"Item wrong length {len(key)} instead of {len(self.index)}." ) key = check_bool_indexer(self.index, key) # We convert to a RangeIndex because getitem_row_array is expecting a list # of indices, and RangeIndex will give us the exact indices of each boolean # requested. key = pd.RangeIndex(len(self.index))[key] if len(key): return DataFrame( _query_compiler=self._query_compiler.getitem_row_array(key) ) else: return DataFrame(columns=self.columns) else: if any(k not in self.columns for k in key): raise KeyError( f"{str([k for k in key if k not in self.columns]).replace(',', '')}" f" not index" ) return DataFrame( _query_compiler=self._query_compiler.getitem_column_array(key) )
Example #11
Source File: indexing.py From vnpy_crypto with MIT License | 5 votes |
def _validate_key(self, key, axis): ax = self.obj._get_axis(axis) # valid for a label where all labels are in the index # slice of labels (where start-end in labels) # slice of integers (only if in the labels) # boolean if isinstance(key, slice): return elif com.is_bool_indexer(key): return elif not is_list_like_indexer(key): def error(): if isna(key): raise TypeError("cannot use label indexing with a null " "key") raise KeyError(u"the label [{key}] is not in the [{axis}]" .format(key=key, axis=self.obj._get_axis_name(axis))) try: key = self._convert_scalar_indexer(key, axis) if not ax.contains(key): error() except TypeError as e: # python 3 type errors should be raised if _is_unorderable_exception(e): error() raise except: error()
Example #12
Source File: indexing.py From vnpy_crypto with MIT License | 5 votes |
def _getitem_axis(self, key, axis=None): if axis is None: axis = self.axis or 0 if isinstance(key, slice): return self._get_slice_axis(key, axis=axis) if isinstance(key, list): key = np.asarray(key) if com.is_bool_indexer(key): self._validate_key(key, axis) return self._getbool_axis(key, axis=axis) # a list of integers elif is_list_like_indexer(key): return self._get_list_axis(key, axis=axis) # a single integer else: key = self._convert_scalar_indexer(key, axis) if not is_integer(key): raise TypeError("Cannot index by location index with a " "non-integer key") # validate the location self._validate_integer(key, axis) return self._get_loc(key, axis=axis)
Example #13
Source File: test_indexing.py From recruit with Apache License 2.0 | 5 votes |
def test_mask_with_boolean(index): s = Series(range(3)) idx = Categorical([True, False, True]) if index: idx = CategoricalIndex(idx) assert com.is_bool_indexer(idx) result = s[idx] expected = s[idx.astype('object')] tm.assert_series_equal(result, expected)
Example #14
Source File: test_indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_mask_with_boolean(index): s = Series(range(3)) idx = Categorical([True, False, True]) if index: idx = CategoricalIndex(idx) assert com.is_bool_indexer(idx) result = s[idx] expected = s[idx.astype('object')] tm.assert_series_equal(result, expected)
Example #15
Source File: sparse.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def __getitem__(self, key): if isinstance(key, tuple): if len(key) > 1: raise IndexError("too many indices for array.") key = key[0] if is_integer(key): return self._get_val_at(key) elif isinstance(key, tuple): data_slice = self.values[key] elif isinstance(key, slice): # special case to preserve dtypes if key == slice(None): return self.copy() # TODO: this logic is surely elsewhere # TODO: this could be more efficient indices = np.arange(len(self), dtype=np.int32)[key] return self.take(indices) else: # TODO: I think we can avoid densifying when masking a # boolean SparseArray with another. Need to look at the # key's fill_value for True / False, and then do an intersection # on the indicies of the sp_values. if isinstance(key, SparseArray): if is_bool_dtype(key): key = key.to_dense() else: key = np.asarray(key) if com.is_bool_indexer(key) and len(self) == len(key): return self.take(np.arange(len(key), dtype=np.int32)[key]) elif hasattr(key, '__len__'): return self.take(key) else: raise ValueError("Cannot slice with '{}'".format(key)) return type(self)(data_slice, kind=self.kind)
Example #16
Source File: base.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def __getitem__(self, key): """ Override numpy.ndarray's __getitem__ method to work as desired. This function adds lists and Series as valid boolean indexers (ndarrays only supports ndarray with dtype=bool). If resulting ndim != 1, plain ndarray is returned instead of corresponding `Index` subclass. """ # There's no custom logic to be implemented in __getslice__, so it's # not overloaded intentionally. getitem = self._data.__getitem__ promote = self._shallow_copy if is_scalar(key): key = com.cast_scalar_indexer(key) return getitem(key) if isinstance(key, slice): # This case is separated from the conditional above to avoid # pessimization of basic indexing. return promote(getitem(key)) if com.is_bool_indexer(key): key = np.asarray(key, dtype=bool) key = com.values_from_object(key) result = getitem(key) if not is_scalar(result): return promote(result) else: return result
Example #17
Source File: multi.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def __getitem__(self, key): if is_scalar(key): key = com.cast_scalar_indexer(key) retval = [] for lev, level_codes in zip(self.levels, self.codes): if level_codes[key] == -1: retval.append(np.nan) else: retval.append(lev[level_codes[key]]) return tuple(retval) else: if com.is_bool_indexer(key): key = np.asarray(key, dtype=bool) sortorder = self.sortorder else: # cannot be sure whether the result will be sorted sortorder = None if isinstance(key, Index): key = np.asarray(key) new_codes = [level_codes[key] for level_codes in self.codes] return MultiIndex(levels=self.levels, codes=new_codes, names=self.names, sortorder=sortorder, verify_integrity=False)
Example #18
Source File: indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _getitem_axis(self, key, axis=None): if axis is None: axis = self.axis or 0 if isinstance(key, slice): return self._get_slice_axis(key, axis=axis) if isinstance(key, list): key = np.asarray(key) if com.is_bool_indexer(key): self._validate_key(key, axis) return self._getbool_axis(key, axis=axis) # a list of integers elif is_list_like_indexer(key): return self._get_list_axis(key, axis=axis) # a single integer else: if not is_integer(key): raise TypeError("Cannot index by location index with a " "non-integer key") # validate the location self._validate_integer(key, axis) return self._get_loc(key, axis=axis)
Example #19
Source File: indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _validate_key(self, key, axis): if com.is_bool_indexer(key): if hasattr(key, 'index') and isinstance(key.index, Index): if key.index.inferred_type == 'integer': raise NotImplementedError("iLocation based boolean " "indexing on an integer type " "is not available") raise ValueError("iLocation based boolean indexing cannot use " "an indexable as a mask") return if isinstance(key, slice): return elif is_integer(key): self._validate_integer(key, axis) elif isinstance(key, tuple): # a tuple should already have been caught by this point # so don't treat a tuple as a valid indexer raise IndexingError('Too many indexers') elif is_list_like_indexer(key): # check that the key does not exceed the maximum size of the index arr = np.array(key) len_axis = len(self.obj._get_axis(axis)) if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis): raise IndexError("positional indexers are out-of-bounds") else: raise ValueError("Can only index by location with " "a [{types}]".format(types=self._valid_types))
Example #20
Source File: indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _validate_key(self, key, axis): if isinstance(key, slice): return True elif com.is_bool_indexer(key): return True elif is_list_like_indexer(key): return True else: self._convert_scalar_indexer(key, axis) return True
Example #21
Source File: indexing.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _validate_key(self, key, axis): # valid for a collection of labels (we check their presence later) # slice of labels (where start-end in labels) # slice of integers (only if in the labels) # boolean if isinstance(key, slice): return if com.is_bool_indexer(key): return if not is_list_like_indexer(key): self._convert_scalar_indexer(key, axis)
Example #22
Source File: test_indexing.py From coffeegrindsize with MIT License | 5 votes |
def test_mask_with_boolean(index): s = Series(range(3)) idx = Categorical([True, False, True]) if index: idx = CategoricalIndex(idx) assert com.is_bool_indexer(idx) result = s[idx] expected = s[idx.astype('object')] tm.assert_series_equal(result, expected)
Example #23
Source File: multi.py From vnpy_crypto with MIT License | 5 votes |
def __getitem__(self, key): if is_scalar(key): retval = [] for lev, lab in zip(self.levels, self.labels): if lab[key] == -1: retval.append(np.nan) else: retval.append(lev[lab[key]]) return tuple(retval) else: if com.is_bool_indexer(key): key = np.asarray(key) sortorder = self.sortorder else: # cannot be sure whether the result will be sorted sortorder = None if isinstance(key, Index): key = np.asarray(key) new_labels = [lab[key] for lab in self.labels] return MultiIndex(levels=self.levels, labels=new_labels, names=self.names, sortorder=sortorder, verify_integrity=False)
Example #24
Source File: base.py From vnpy_crypto with MIT License | 5 votes |
def __getitem__(self, key): """ Override numpy.ndarray's __getitem__ method to work as desired. This function adds lists and Series as valid boolean indexers (ndarrays only supports ndarray with dtype=bool). If resulting ndim != 1, plain ndarray is returned instead of corresponding `Index` subclass. """ # There's no custom logic to be implemented in __getslice__, so it's # not overloaded intentionally. getitem = self._data.__getitem__ promote = self._shallow_copy if is_scalar(key): return getitem(key) if isinstance(key, slice): # This case is separated from the conditional above to avoid # pessimization of basic indexing. return promote(getitem(key)) if com.is_bool_indexer(key): key = np.asarray(key) key = com._values_from_object(key) result = getitem(key) if not is_scalar(result): return promote(result) else: return result
Example #25
Source File: sparse.py From recruit with Apache License 2.0 | 5 votes |
def __getitem__(self, key): if isinstance(key, tuple): if len(key) > 1: raise IndexError("too many indices for array.") key = key[0] if is_integer(key): return self._get_val_at(key) elif isinstance(key, tuple): data_slice = self.values[key] elif isinstance(key, slice): # special case to preserve dtypes if key == slice(None): return self.copy() # TODO: this logic is surely elsewhere # TODO: this could be more efficient indices = np.arange(len(self), dtype=np.int32)[key] return self.take(indices) else: # TODO: I think we can avoid densifying when masking a # boolean SparseArray with another. Need to look at the # key's fill_value for True / False, and then do an intersection # on the indicies of the sp_values. if isinstance(key, SparseArray): if is_bool_dtype(key): key = key.to_dense() else: key = np.asarray(key) if com.is_bool_indexer(key) and len(self) == len(key): return self.take(np.arange(len(key), dtype=np.int32)[key]) elif hasattr(key, '__len__'): return self.take(key) else: raise ValueError("Cannot slice with '{}'".format(key)) return type(self)(data_slice, kind=self.kind)
Example #26
Source File: indexing.py From recruit with Apache License 2.0 | 5 votes |
def _getitem_axis(self, key, axis=None): if axis is None: axis = self.axis or 0 if isinstance(key, slice): return self._get_slice_axis(key, axis=axis) if isinstance(key, list): key = np.asarray(key) if com.is_bool_indexer(key): self._validate_key(key, axis) return self._getbool_axis(key, axis=axis) # a list of integers elif is_list_like_indexer(key): return self._get_list_axis(key, axis=axis) # a single integer else: if not is_integer(key): raise TypeError("Cannot index by location index with a " "non-integer key") # validate the location self._validate_integer(key, axis) return self._get_loc(key, axis=axis)
Example #27
Source File: base.py From recruit with Apache License 2.0 | 5 votes |
def __getitem__(self, key): """ Override numpy.ndarray's __getitem__ method to work as desired. This function adds lists and Series as valid boolean indexers (ndarrays only supports ndarray with dtype=bool). If resulting ndim != 1, plain ndarray is returned instead of corresponding `Index` subclass. """ # There's no custom logic to be implemented in __getslice__, so it's # not overloaded intentionally. getitem = self._data.__getitem__ promote = self._shallow_copy if is_scalar(key): key = com.cast_scalar_indexer(key) return getitem(key) if isinstance(key, slice): # This case is separated from the conditional above to avoid # pessimization of basic indexing. return promote(getitem(key)) if com.is_bool_indexer(key): key = np.asarray(key, dtype=bool) key = com.values_from_object(key) result = getitem(key) if not is_scalar(result): return promote(result) else: return result
Example #28
Source File: indexing.py From recruit with Apache License 2.0 | 5 votes |
def _validate_key(self, key, axis): if com.is_bool_indexer(key): if hasattr(key, 'index') and isinstance(key.index, Index): if key.index.inferred_type == 'integer': raise NotImplementedError("iLocation based boolean " "indexing on an integer type " "is not available") raise ValueError("iLocation based boolean indexing cannot use " "an indexable as a mask") return if isinstance(key, slice): return elif is_integer(key): self._validate_integer(key, axis) elif isinstance(key, tuple): # a tuple should already have been caught by this point # so don't treat a tuple as a valid indexer raise IndexingError('Too many indexers') elif is_list_like_indexer(key): # check that the key does not exceed the maximum size of the index arr = np.array(key) len_axis = len(self.obj._get_axis(axis)) if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis): raise IndexError("positional indexers are out-of-bounds") else: raise ValueError("Can only index by location with " "a [{types}]".format(types=self._valid_types))
Example #29
Source File: multi.py From recruit with Apache License 2.0 | 5 votes |
def __getitem__(self, key): if is_scalar(key): key = com.cast_scalar_indexer(key) retval = [] for lev, level_codes in zip(self.levels, self.codes): if level_codes[key] == -1: retval.append(np.nan) else: retval.append(lev[level_codes[key]]) return tuple(retval) else: if com.is_bool_indexer(key): key = np.asarray(key, dtype=bool) sortorder = self.sortorder else: # cannot be sure whether the result will be sorted sortorder = None if isinstance(key, Index): key = np.asarray(key) new_codes = [level_codes[key] for level_codes in self.codes] return MultiIndex(levels=self.levels, codes=new_codes, names=self.names, sortorder=sortorder, verify_integrity=False)
Example #30
Source File: series.py From modin with Apache License 2.0 | 5 votes |
def _getitem(self, key): key = apply_if_callable(key, self) if isinstance(key, Series) and key.dtype == np.bool: # This ends up being significantly faster than looping through and getting # each item individually. key = key._to_pandas() if is_bool_indexer(key): return self.__constructor__( query_compiler=self._query_compiler.getitem_row_array( pandas.RangeIndex(len(self.index))[key] ) ) # TODO: More efficiently handle `tuple` case for `Series.__getitem__` if isinstance(key, tuple): return self._default_to_pandas(pandas.Series.__getitem__, key) else: if not is_list_like(key): reduce_dimension = True key = [key] else: reduce_dimension = False # The check for whether or not `key` is in `keys()` will throw a TypeError # if the object is not hashable. When that happens, we just use the `iloc`. try: if all(k in self.keys() for k in key): result = self._query_compiler.getitem_row_array( self.index.get_indexer_for(key) ) else: result = self._query_compiler.getitem_row_array(key) except TypeError: result = self._query_compiler.getitem_row_array(key) if reduce_dimension: return self._reduce_dimension(result) return self.__constructor__(query_compiler=result)