Python numpy.putmask() Examples
The following are 30
code examples of numpy.putmask().
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
numpy
, or try the search function
.
Example #1
Source File: nanops.py From vnpy_crypto with MIT License | 6 votes |
def make_nancomp(op): def f(x, y): xmask = isna(x) ymask = isna(y) mask = xmask | ymask with np.errstate(all='ignore'): result = op(x, y) if mask.any(): if is_bool_dtype(result): result = result.astype('O') np.putmask(result, mask, np.nan) return result return f
Example #2
Source File: strings.py From Computable with MIT License | 6 votes |
def _map(f, arr, na_mask=False, na_value=np.nan): if isinstance(arr, Series): arr = arr.values if not isinstance(arr, np.ndarray): arr = np.asarray(arr, dtype=object) if na_mask: mask = isnull(arr) try: result = lib.map_infer_mask(arr, f, mask.view(np.uint8)) except (TypeError, AttributeError): def g(x): try: return f(x) except (TypeError, AttributeError): return na_value return _map(g, arr) if na_value is not np.nan: np.putmask(result, mask, na_value) if result.dtype == object: result = lib.maybe_convert_objects(result) return result else: return lib.map_infer(arr, f)
Example #3
Source File: test_multilevel.py From vnpy_crypto with MIT License | 6 votes |
def test_frame_getitem_setitem_boolean(self): df = self.frame.T.copy() values = df.values result = df[df > 0] expected = df.where(df > 0) tm.assert_frame_equal(result, expected) df[df > 0] = 5 values[values > 0] = 5 tm.assert_almost_equal(df.values, values) df[df == 5] = 0 values[values == 5] = 0 tm.assert_almost_equal(df.values, values) # a df that needs alignment first df[df[:-1] < 0] = 2 np.putmask(values[:-1], values[:-1] < 0, 2) tm.assert_almost_equal(df.values, values) with tm.assert_raises_regex(TypeError, 'boolean values only'): df[df * 0] = 2
Example #4
Source File: DistanceUtils.py From RaptorX-Contact with GNU General Public License v3.0 | 6 votes |
def DiscretizeDistMatrix(distm, bins=None, invalidDistanceSeparated=False): assert (bins is not None) result = np.digitize(distm, bins) - 1 if invalidDistanceSeparated: ## -1 and the maximum distance bin are separated np.putmask(result, result == -1, len(bins) ) labels = range( len(bins) + 1 ) else: ## -1 and the maximum distance bin are merged into a single bin np.putmask(result, result == -1, len(bins) -1 ) labels = range( len(bins) ) return result.astype(np.int32), np.int32(labels), bins ## calculate the natural logarithm of a matrix
Example #5
Source File: base.py From vnpy_crypto with MIT License | 6 votes |
def _join_non_unique(self, other, how='left', return_indexers=False): from pandas.core.reshape.merge import _get_join_indexers left_idx, right_idx = _get_join_indexers([self._ndarray_values], [other._ndarray_values], how=how, sort=True) left_idx = _ensure_platform_int(left_idx) right_idx = _ensure_platform_int(right_idx) join_index = np.asarray(self._ndarray_values.take(left_idx)) mask = left_idx == -1 np.putmask(join_index, mask, other._ndarray_values.take(right_idx)) join_index = self._wrap_joined_index(join_index, other) if return_indexers: return join_index, left_idx, right_idx else: return join_index
Example #6
Source File: nanops.py From recruit with Apache License 2.0 | 6 votes |
def make_nancomp(op): def f(x, y): xmask = isna(x) ymask = isna(y) mask = xmask | ymask with np.errstate(all='ignore'): result = op(x, y) if mask.any(): if is_bool_dtype(result): result = result.astype('O') np.putmask(result, mask, np.nan) return result return f
Example #7
Source File: nanops.py From recruit with Apache License 2.0 | 6 votes |
def _get_counts_nanvar(mask, axis, ddof, dtype=float): dtype = _get_dtype(dtype) count = _get_counts(mask, axis, dtype=dtype) d = count - dtype.type(ddof) # always return NaN, never inf if is_scalar(count): if count <= ddof: count = np.nan d = np.nan else: mask2 = count <= ddof if mask2.any(): np.putmask(d, mask2, np.nan) np.putmask(count, mask2, np.nan) return count, d
Example #8
Source File: sorting.py From recruit with Apache License 2.0 | 6 votes |
def _reorder_by_uniques(uniques, labels): # sorter is index where elements ought to go sorter = uniques.argsort() # reverse_indexer is where elements came from reverse_indexer = np.empty(len(sorter), dtype=np.int64) reverse_indexer.put(sorter, np.arange(len(sorter))) mask = labels < 0 # move labels to right locations (ie, unsort ascending labels) labels = algorithms.take_nd(reverse_indexer, labels, allow_fill=False) np.putmask(labels, mask, -1) # sort observed ids uniques = algorithms.take_nd(uniques, sorter, allow_fill=False) return uniques, labels
Example #9
Source File: sorting.py From recruit with Apache License 2.0 | 6 votes |
def decons_group_index(comp_labels, shape): # reconstruct labels if is_int64_overflow_possible(shape): # at some point group indices are factorized, # and may not be deconstructed here! wrong path! raise ValueError('cannot deconstruct factorized group indices!') label_list = [] factor = 1 y = 0 x = comp_labels for i in reversed(range(len(shape))): labels = (x - y) % (factor * shape[i]) // factor np.putmask(labels, comp_labels < 0, -1) label_list.append(labels) y = labels * factor factor *= shape[i] return label_list[::-1]
Example #10
Source File: groupby.py From Computable with MIT License | 6 votes |
def get_group_index(label_list, shape): """ For the particular label_list, gets the offsets into the hypothetical list representing the totally ordered cartesian product of all possible label combinations. """ if len(label_list) == 1: return label_list[0] n = len(label_list[0]) group_index = np.zeros(n, dtype=np.int64) mask = np.zeros(n, dtype=bool) for i in range(len(shape)): stride = np.prod([x for x in shape[i + 1:]], dtype=np.int64) group_index += com._ensure_int64(label_list[i]) * stride mask |= label_list[i] < 0 np.putmask(group_index, mask, -1) return group_index
Example #11
Source File: base.py From recruit with Apache License 2.0 | 6 votes |
def putmask(self, mask, value): """ Return a new Index of the values set with the mask. See Also -------- numpy.ndarray.putmask """ values = self.values.copy() try: np.putmask(values, mask, self._convert_for_op(value)) return self._shallow_copy(values) except (ValueError, TypeError) as err: if is_object_dtype(self): raise err # coerces to object return self.astype(object).putmask(mask, value)
Example #12
Source File: base.py From recruit with Apache License 2.0 | 6 votes |
def _join_non_unique(self, other, how='left', return_indexers=False): from pandas.core.reshape.merge import _get_join_indexers left_idx, right_idx = _get_join_indexers([self._ndarray_values], [other._ndarray_values], how=how, sort=True) left_idx = ensure_platform_int(left_idx) right_idx = ensure_platform_int(right_idx) join_index = np.asarray(self._ndarray_values.take(left_idx)) mask = left_idx == -1 np.putmask(join_index, mask, other._ndarray_values.take(right_idx)) join_index = self._wrap_joined_index(join_index, other) if return_indexers: return join_index, left_idx, right_idx else: return join_index
Example #13
Source File: sorting.py From vnpy_crypto with MIT License | 6 votes |
def decons_group_index(comp_labels, shape): # reconstruct labels if is_int64_overflow_possible(shape): # at some point group indices are factorized, # and may not be deconstructed here! wrong path! raise ValueError('cannot deconstruct factorized group indices!') label_list = [] factor = 1 y = 0 x = comp_labels for i in reversed(range(len(shape))): labels = (x - y) % (factor * shape[i]) // factor np.putmask(labels, comp_labels < 0, -1) label_list.append(labels) y = labels * factor factor *= shape[i] return label_list[::-1]
Example #14
Source File: nanops.py From vnpy_crypto with MIT License | 6 votes |
def _get_counts_nanvar(mask, axis, ddof, dtype=float): dtype = _get_dtype(dtype) count = _get_counts(mask, axis, dtype=dtype) d = count - dtype.type(ddof) # always return NaN, never inf if is_scalar(count): if count <= ddof: count = np.nan d = np.nan else: mask2 = count <= ddof if mask2.any(): np.putmask(d, mask2, np.nan) np.putmask(count, mask2, np.nan) return count, d
Example #15
Source File: merge.py From Computable with MIT License | 6 votes |
def _sort_labels(uniques, left, right): if not isinstance(uniques, np.ndarray): # tuplesafe uniques = Index(uniques).values sorter = uniques.argsort() reverse_indexer = np.empty(len(sorter), dtype=np.int64) reverse_indexer.put(sorter, np.arange(len(sorter))) new_left = reverse_indexer.take(com._ensure_platform_int(left)) np.putmask(new_left, left == -1, -1) new_right = reverse_indexer.take(com._ensure_platform_int(right)) np.putmask(new_right, right == -1, -1) return new_left, new_right
Example #16
Source File: groupby.py From Computable with MIT License | 6 votes |
def _reorder_by_uniques(uniques, labels): # sorter is index where elements ought to go sorter = uniques.argsort() # reverse_indexer is where elements came from reverse_indexer = np.empty(len(sorter), dtype=np.int64) reverse_indexer.put(sorter, np.arange(len(sorter))) mask = labels < 0 # move labels to right locations (ie, unsort ascending labels) labels = com.take_nd(reverse_indexer, labels, allow_fill=False) np.putmask(labels, mask, -1) # sort observed ids uniques = com.take_nd(uniques, sorter, allow_fill=False) return uniques, labels
Example #17
Source File: test_setitem.py From recruit with Apache License 2.0 | 6 votes |
def test_frame_getitem_setitem_boolean( self, multiindex_dataframe_random_data): frame = multiindex_dataframe_random_data df = frame.T.copy() values = df.values result = df[df > 0] expected = df.where(df > 0) tm.assert_frame_equal(result, expected) df[df > 0] = 5 values[values > 0] = 5 tm.assert_almost_equal(df.values, values) df[df == 5] = 0 values[values == 5] = 0 tm.assert_almost_equal(df.values, values) # a df that needs alignment first df[df[:-1] < 0] = 2 np.putmask(values[:-1], values[:-1] < 0, 2) tm.assert_almost_equal(df.values, values) with pytest.raises(TypeError, match='boolean values only'): df[df * 0] = 2
Example #18
Source File: nanops.py From Computable with MIT License | 5 votes |
def _get_values(values, skipna, fill_value=None, fill_value_typ=None, isfinite=False, copy=True): """ utility to get the values view, mask, dtype if necessary copy and mask using the specified fill_value copy = True will force the copy """ values = _values_from_object(values) if isfinite: mask = _isfinite(values) else: mask = isnull(values) dtype = values.dtype dtype_ok = _na_ok_dtype(dtype) # get our fill value (in case we need to provide an alternative # dtype for it) fill_value = _get_fill_value(dtype, fill_value=fill_value, fill_value_typ=fill_value_typ) if skipna: if copy: values = values.copy() if dtype_ok: np.putmask(values, mask, fill_value) # promote if needed else: values, changed = com._maybe_upcast_putmask(values, mask, fill_value) elif copy: values = values.copy() values = _view_if_needed(values) return values, mask, dtype
Example #19
Source File: test_multiarray.py From Computable with MIT License | 5 votes |
def test_masked_array(self): ## x = np.array([1,2,3]) ## z = np.ma.array(x,mask=[True,False,False]) ## np.putmask(z,[True,True,True],3) pass
Example #20
Source File: merge.py From Computable with MIT License | 5 votes |
def _factorize_keys(lk, rk, sort=True): if com._is_int_or_datetime_dtype(lk) and com._is_int_or_datetime_dtype(rk): klass = _hash.Int64Factorizer lk = com._ensure_int64(lk) rk = com._ensure_int64(rk) else: klass = _hash.Factorizer lk = com._ensure_object(lk) rk = com._ensure_object(rk) rizer = klass(max(len(lk), len(rk))) llab = rizer.factorize(lk) rlab = rizer.factorize(rk) count = rizer.get_count() if sort: uniques = rizer.uniques.to_array() llab, rlab = _sort_labels(uniques, llab, rlab) # NA group lmask = llab == -1 lany = lmask.any() rmask = rlab == -1 rany = rmask.any() if lany or rany: if lany: np.putmask(llab, lmask, count) if rany: np.putmask(rlab, rmask, count) count += 1 return llab, rlab, count
Example #21
Source File: nanops.py From Computable with MIT License | 5 votes |
def nanskew(values, axis=None, skipna=True): if not isinstance(values.dtype.type, np.floating): values = values.astype('f8') mask = isnull(values) count = _get_counts(mask, axis) if skipna: values = values.copy() np.putmask(values, mask, 0) A = values.sum(axis) / count B = (values ** 2).sum(axis) / count - A ** 2 C = (values ** 3).sum(axis) / count - A ** 3 - 3 * A * B # floating point error B = _zero_out_fperr(B) C = _zero_out_fperr(C) result = ((np.sqrt((count ** 2 - count)) * C) / ((count - 2) * np.sqrt(B) ** 3)) if isinstance(result, np.ndarray): result = np.where(B == 0, 0, result) result[count < 3] = np.nan return result else: result = 0 if B == 0 else result if count < 3: return np.nan return result
Example #22
Source File: frame.py From Computable with MIT License | 5 votes |
def _reindex_index(self, index, method, copy, level, fill_value=np.nan, limit=None, takeable=False): if level is not None: raise TypeError('Reindex by level not supported for sparse') if self.index.equals(index): if copy: return self.copy() else: return self if len(self.index) == 0: return SparseDataFrame(index=index, columns=self.columns) indexer = self.index.get_indexer(index, method, limit=limit) indexer = com._ensure_platform_int(indexer) mask = indexer == -1 need_mask = mask.any() new_series = {} for col, series in self.iteritems(): if mask.all(): continue values = series.values new = values.take(indexer) if need_mask: np.putmask(new, mask, fill_value) new_series[col] = new return SparseDataFrame(new_series, index=index, columns=self.columns, default_fill_value=self._default_fill_value)
Example #23
Source File: generic.py From Computable with MIT License | 5 votes |
def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None, **kwds): """ Percent change over given number of periods Parameters ---------- periods : int, default 1 Periods to shift for forming percent change fill_method : str, default 'pad' How to handle NAs before computing percent changes limit : int, default None The number of consecutive NAs to fill before stopping freq : DateOffset, timedelta, or offset alias string, optional Increment to use from time series API (e.g. 'M' or BDay()) Returns ------- chg : same type as caller """ # TODO: Not sure if above is correct - need someone to confirm. if fill_method is None: data = self else: data = self.fillna(method=fill_method, limit=limit) rs = data / data.shift(periods=periods, freq=freq, **kwds) - 1 if freq is None: mask = com.isnull(_values_from_object(self)) np.putmask(rs.values, mask, np.nan) return rs
Example #24
Source File: test_multiarray.py From Computable with MIT License | 5 votes |
def test_mask_size(self): assert_raises(ValueError, np.putmask, np.array([1, 2, 3]), [True], 5)
Example #25
Source File: DistanceUtils.py From RaptorX-Contact with GNU General Public License v3.0 | 5 votes |
def LogDistMatrix(distm): tmpMatrix = copy(distm) np.putmask(tmpMatrix, distm < 1/np.e, 1/np.e) return np.log(tmpMatrix) ## this function calculates the joint label probability distribution at different ranges: extra-, long-, medium-, short- and near-range ## data is a list of matrices with shape (L, L, 2) where L is the sequence length. ## [:, :, 0] is for target protein and [:, :, 1] is for template information ## numLabels is the number of different labels in the matrices ## this function returns 3 values: joint probability, marginal probability for target protein, marginal probability for template
Example #26
Source File: ops.py From Computable with MIT License | 5 votes |
def _comp_method_PANEL(op, name, str_rep=None, masker=False): def na_op(x, y): try: result = expressions.evaluate(op, str_rep, x, y, raise_on_error=True) except TypeError: xrav = x.ravel() result = np.empty(x.size, dtype=bool) if isinstance(y, np.ndarray): yrav = y.ravel() mask = notnull(xrav) & notnull(yrav) result[mask] = op(np.array(list(xrav[mask])), np.array(list(yrav[mask]))) else: mask = notnull(xrav) result[mask] = op(np.array(list(xrav[mask])), y) if op == operator.ne: # pragma: no cover np.putmask(result, -mask, True) else: np.putmask(result, -mask, False) result = result.reshape(x.shape) return result @Appender('Wrapper for comparison method %s' % name) def f(self, other): if isinstance(other, self._constructor): return self._compare_constructor(other, na_op) elif isinstance(other, (self._constructor_sliced, pd.DataFrame, pd.Series)): raise Exception("input needs alignment for this object [%s]" % self._constructor) else: return self._combine_const(other, na_op) f.__name__ = name return f
Example #27
Source File: groupby.py From Computable with MIT License | 5 votes |
def decons_group_index(comp_labels, shape): # reconstruct labels label_list = [] factor = 1 y = 0 x = comp_labels for i in reversed(range(len(shape))): labels = (x - y) % (factor * shape[i]) // factor np.putmask(labels, comp_labels < 0, -1) label_list.append(labels) y = labels * factor factor *= shape[i] return label_list[::-1]
Example #28
Source File: internals.py From vnpy_crypto with MIT License | 5 votes |
def replace(self, to_replace, value, inplace=False, filter=None, regex=False, convert=True, mgr=None): """ replace the to_replace value with value, possible to create new blocks here this is just a call to putmask. regex is not used here. It is used in ObjectBlocks. It is here for API compatibility. """ inplace = validate_bool_kwarg(inplace, 'inplace') original_to_replace = to_replace # try to replace, if we raise an error, convert to ObjectBlock and # retry try: values, _, to_replace, _ = self._try_coerce_args(self.values, to_replace) mask = missing.mask_missing(values, to_replace) if filter is not None: filtered_out = ~self.mgr_locs.isin(filter) mask[filtered_out.nonzero()[0]] = False blocks = self.putmask(mask, value, inplace=inplace) if convert: blocks = [b.convert(by_item=True, numeric=False, copy=not inplace) for b in blocks] return blocks except (TypeError, ValueError): # try again with a compatible block block = self.astype(object) return block.replace( to_replace=original_to_replace, value=value, inplace=inplace, filter=filter, regex=regex, convert=convert)
Example #29
Source File: internals.py From vnpy_crypto with MIT License | 5 votes |
def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False, mgr=None): """ putmask the data to the block; we must be a single block and not generate other blocks return the resulting block Parameters ---------- mask : the condition to respect new : a ndarray/object align : boolean, perform alignment on other/cond, default is True inplace : perform inplace modification, default is False Returns ------- a new block, the result of the putmask """ inplace = validate_bool_kwarg(inplace, 'inplace') # use block's copy logic. # .values may be an Index which does shallow copy by default new_values = self.values if inplace else self.copy().values new_values, _, new, _ = self._try_coerce_args(new_values, new) if isinstance(new, np.ndarray) and len(new) == len(mask): new = new[mask] mask = _safe_reshape(mask, new_values.shape) new_values[mask] = new new_values = self._try_coerce_result(new_values) return [self.make_block(values=new_values)]
Example #30
Source File: internals.py From vnpy_crypto with MIT License | 5 votes |
def putmask(self, **kwargs): return self.apply('putmask', **kwargs)