def _arithmetic_binop_impl(self, other, op, truediv=False, weld_elem_type=None): """ Performs the operation on two `Series` elementwise. """ left_ty = self.output_type.elem_type scalar_ty = GrizzlySeries._scalar_ty(other, left_ty) if scalar_ty is not None: # Inline scalars directly instead of adding them # as dependencies. right_ty = scalar_ty rightval = str(other) else: # Value is not a scalar -- for now, we require collection types to be # GrizzlySeries. if not isinstance(other, GrizzlySeries): raise GrizzlyError("RHS of binary operator must be a GrizzlySeries") right_ty = other.output_type.elem_type rightval = other.weld_value_ cast_type = wenp.binop_output_type(left_ty, right_ty, truediv) output_type = cast_type if weld_elem_type is None else weld_elem_type lazy = binary_map(op, left_type=str(left_ty), right_type=str(right_ty), leftval=self.weld_value_, rightval=rightval, scalararg=scalar_ty is not None, cast_type=cast_type)(WeldVec(output_type), GrizzlySeries._decoder) return GrizzlySeries(lazy, dtype=wenp.weld_type_to_dtype(output_type))
def __str__(self): if self.is_value: # This is guaranteed to be 0-copy. return str(self.to_pandas()) return "GrizzlySeries({}, dtype: {}, deps: [{}])".format( self.weld_value_.expression, str(wenp.weld_type_to_dtype(self.output_type.elem_type)), ", ".join([str(child.id) for child in self.children]))
def dtype(self): """ Returns the NumPy dtype of this Series. """ elem_type = self.elem_type if elem_type == WeldVec(I8()): return np.dtype('S') return wenp.weld_type_to_dtype(elem_type)
def _apply(self, func, *args, return_weld_elem_type=None): """ Apply the given weldfunc to `self.series` and return a new GrizzlySeries. If the return type of the result is not a string GrizzlySeries, pass 'return_weld_elem_type' to specify the element type of the result. """ output_type = self.series.output_type if return_weld_elem_type is None else WeldVec( return_weld_elem_type) dtype = 'S' if return_weld_elem_type is None else wenp.weld_type_to_dtype( return_weld_elem_type) lazy = func(self.series.weld_value_, *args)(output_type, self.constructor._decoder) return (self.constructor)(lazy, dtype=dtype)
def agg(self, funcs): """ Apply the provided aggregation functions. If a single function is provided, this returns a scalar. If multiple functions are provided, this returns a `GrizzlySeries`, where the Nth element in the series is the result of the Nth aggregation. Examples -------- >>> s = GrizzlySeries([1,2,3,4]) >>> s.agg('sum') 10 >>> s.agg(['sum', 'mean']).evaluate() 0 10.0 1 2.5 dtype: float64 """ if isinstance(funcs, str): funcs = [funcs] if isinstance(funcs, list): assert all([weldagg.supported(agg) for agg in funcs]) output_elem_type = weldagg.result_elem_type(self.elem_type, funcs) if len(funcs) > 1: output_type = WeldVec(output_elem_type) decoder = GrizzlySeries._decoder else: output_type = output_elem_type # Result is a primitive, so we can use the default primitive decoder. decoder = None result_weld_value = weldagg.agg(self.weld_value, self.elem_type, funcs)(output_type, decoder) if decoder is not None: return GrizzlySeries( result_weld_value, dtype=wenp.weld_type_to_dtype(output_elem_type), name=self.name) else: # TODO(shoumik.palkar): Do we want to evaluate here? For now, we will, but eventually it may # be advantageous to have a lazy scalar value that can be used elsewhere. return result_weld_value.evaluate()[0]
def _arithmetic_binop_impl(self, other, op, truediv=False, weld_elem_type=None): """ Performs the operation on two `Series` elementwise. Parameters ---------- other: scalar or GrizzlySeries the RHS operand op: str the operator to apply. truediv: bool, optional is this a truediv? weld_elem_type: WeldType or None the element type produced by this operation. None means it is inferred based on Numpy's type conversion rules. Returns ------- GrizzlySeries """ left_ty = self.elem_type scalar_ty = GrizzlySeries.scalar_ty(other, left_ty) if scalar_ty is not None: # Inline scalars directly instead of adding them # as dependencies. right_ty = scalar_ty rightval = str(other) else: # Value is not a scalar -- for now, we require collection types to be # GrizzlySeries. if not isinstance(other, GrizzlySeries): raise GrizzlyError( "RHS of binary operator must be a GrizzlySeries") right_ty = other.elem_type rightval = other.weld_value is_string = isinstance(left_ty, WeldVec) if op != "==" and op != "!=" and is_string: raise TypeError( "Unsupported operand type(s) for '{}': {} and {}".format( op, left_ty, right_ty)) # Don't cast if we're dealing with strings. if is_string: cast_type = "" else: cast_type = wenp.binop_output_type(left_ty, right_ty, truediv) output_type = cast_type if weld_elem_type is None else weld_elem_type lazy = binary_map(op, left_type=str(left_ty), right_type=str(right_ty), leftval=self.weld_value, rightval=rightval, scalararg=scalar_ty is not None, cast_type=cast_type)(WeldVec(output_type), GrizzlySeries._decoder) return GrizzlySeries(lazy, dtype=wenp.weld_type_to_dtype(output_type), name=self.name)