Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def dynd_op(op, left, right):
if isinstance(left, nd.array):
left_np, left_missing = dynd_to_np_mask(left)
left_option = is_option(left.dtype)
else:
left_np, left_missing = left, False
left_option = False
if isinstance(right, nd.array):
right_np, right_missing = dynd_to_np_mask(right)
right_option = is_option(right.dtype)
else:
right_np, right_missing = right, False
right_option = False
out = op(left_np, right_np)
if left_option or right_option:
if out.dtype in dynd_missing_types:
out[left_missing | right_missing] = dynd_missing_types[out.dtype]
out = nd.asarray(out)
return nd.asarray(out).view_scalars('?' + str(out.dtype))
else:
raise ValueError("Missing type unknown")
return nd.asarray(out)
def f(self):
arr, missing = dynd_to_np_mask(self._data)
out = op(arr)
if is_option(self._data.dtype):
out[missing] = dynd_missing_types[out.dtype]
out = nd.asarray(out).view_scalars('?' + str(out.dtype))
else:
out = nd.asarray(out)
return ScalarAggregate(out, self.x_axis, self.y_axis)
return f
def dynd_op(op, left, right):
if isinstance(left, nd.array):
left_np, left_missing = dynd_to_np_mask(left)
left_option = is_option(left.dtype)
else:
left_np, left_missing = left, False
left_option = False
if isinstance(right, nd.array):
right_np, right_missing = dynd_to_np_mask(right)
right_option = is_option(right.dtype)
else:
right_np, right_missing = right, False
right_option = False
out = op(left_np, right_np)
if left_option or right_option:
if out.dtype in dynd_missing_types:
out[left_missing | right_missing] = dynd_missing_types[out.dtype]
out = nd.asarray(out)
return nd.asarray(out).view_scalars('?' + str(out.dtype))
def _where_helper(agg, cond, otherwise):
if not isboolean(cond.dshape):
raise TypeError("cond must be a boolean aggregate")
_validate_aligned(agg, cond)
cond_arr, _ = dynd_to_np_mask(cond._data)
arr, arr_mask = dynd_to_np_mask(agg._data)
arr_missing = is_option(agg._data.dtype)
while cond_arr.ndim < arr.ndim:
cond_arr = np.expand_dims(cond_arr, -1)
if isinstance(otherwise, Aggregate):
_validate_aligned(agg, otherwise)
otherwise_arr, otherwise_mask = dynd_to_np_mask(otherwise._data)
while otherwise_arr.ndim < arr.ndim:
otherwise_arr = np.expand_dims(otherwise_arr, -1)
otherwise_mask = np.expand_dims(otherwise_mask, -1)
otherwise_missing = is_option(otherwise._data.dtype)
elif isinstance(otherwise, (int, float, np.generic)):
otherwise_arr = otherwise
otherwise_mask = otherwise_missing = False
elif otherwise is None:
otherwise_arr = dynd_missing_types[arr.dtype]
def _where_helper(agg, cond, otherwise):
if not isboolean(cond.dshape):
raise TypeError("cond must be a boolean aggregate")
_validate_aligned(agg, cond)
cond_arr, _ = dynd_to_np_mask(cond._data)
arr, arr_mask = dynd_to_np_mask(agg._data)
arr_missing = is_option(agg._data.dtype)
while cond_arr.ndim < arr.ndim:
cond_arr = np.expand_dims(cond_arr, -1)
if isinstance(otherwise, Aggregate):
_validate_aligned(agg, otherwise)
otherwise_arr, otherwise_mask = dynd_to_np_mask(otherwise._data)
while otherwise_arr.ndim < arr.ndim:
otherwise_arr = np.expand_dims(otherwise_arr, -1)
otherwise_mask = np.expand_dims(otherwise_mask, -1)
otherwise_missing = is_option(otherwise._data.dtype)
elif isinstance(otherwise, (int, float, np.generic)):
otherwise_arr = otherwise
otherwise_mask = otherwise_missing = False
elif otherwise is None:
otherwise_arr = dynd_missing_types[arr.dtype]
otherwise_mask = False