def __getitem__(self, key): if isSupportedInt(key): # Single integer index will return a pandas scalar return self._scalar_callback(self._data[key]) else: # Slice or array index should return same class return self.__class__(self._data[key])
def _binop(self, other, op): # Need to do 2 things: # 1) Determine return type, based on other's class # 2) Get other's int64 data to combine with self's data if isinstance(other, Datetime) or self._is_datetime_scalar(other): if op not in self.supported_with_datetime: raise TypeError("{} not supported between {} and Datetime".format(op, self.__class__.__name__)) otherclass = 'Datetime' if self._is_datetime_scalar(other): otherdata = _Timescalar(other)._data else: otherdata = other._data elif isinstance(other, Timedelta) or self._is_timedelta_scalar(other): if op not in self.supported_with_timedelta: raise TypeError("{} not supported between {} and Timedelta".format(op, self.__class__.__name__)) otherclass = 'Timedelta' if self._is_timedelta_scalar(other): otherdata = _Timescalar(other)._data else: otherdata = other._data elif (isinstance(other, pdarray) and other.dtype == int64) or isSupportedInt(other): if op not in self.supported_with_pdarray: raise TypeError("{} not supported between {} and integer".format(op, self.__class__.__name__)) otherclass = 'pdarray' otherdata = other else: return NotImplemented # Determines return type (Datetime, Timedelta, or pdarray) callback = self._get_callback(otherclass, op) # Actual operation evaluates on the underlying int64 data return callback(self._data._binop(otherdata, op))
def _r_binop(self, other, op): # Need to do 2 things: # 1) Determine return type, based on other's class # 2) Get other's int64 data to combine with self's data # First case is pdarray <op> self if (isinstance(other, pdarray) and other.dtype == int64): if op not in self.supported_with_r_pdarray: raise TypeError("{} not supported between int64 and {}".format(op, self.__class__.__name__)) callback = self._get_callback('pdarray', op) # Need to use other._binop because self._data._r_binop can only handle scalars return callback(other._binop(self._data, op)) # All other cases are scalars, so can use self._data._r_binop elif self._is_datetime_scalar(other): if op not in self.supported_with_r_datetime: raise TypeError("{} not supported between scalar datetime and {}".format(op, self.__class__.__name__)) otherclass = 'Datetime' otherdata = _Timescalar(other)._data elif self._is_timedelta_scalar(other): if op not in self.supported_with_r_timedelta: raise TypeError("{} not supported between scalar timedelta and {}".format(op, self.__class__.__name__)) otherclass = 'Timedelta' otherdata = _Timescalar(other)._data elif isSupportedInt(other): if op not in self.supported_with_r_pdarray: raise TypeError("{} not supported between int64 and {}".format(op, self.__class__.__name__)) otherclass = 'pdarray' otherdata = other else: # If here, type is not handled return NotImplemented callback = self._get_callback(otherclass, op) return callback(self._data._r_binop(otherdata, op))
def test_isSupportedInt(self): ''' Tests for both True and False scenarios of the isSupportedInt method. ''' self.assertTrue(dtypes.isSupportedInt(1)) self.assertTrue(dtypes.isSupportedInt(np.int64(1))) self.assertTrue(dtypes.isSupportedInt(np.int64(1.0))) self.assertFalse(dtypes.isSupportedInt(1.0)) self.assertFalse(dtypes.isSupportedInt('1')) self.assertFalse(dtypes.isSupportedInt('1.0'))
def arange(*args) -> pdarray: """ arange([start,] stop[, stride]) Create a pdarray of consecutive integers within the interval [start, stop). If only one arg is given then arg is the stop parameter. If two args are given, then the first arg is start and second is stop. If three args are given, then the first arg is start, second is stop, third is stride. Parameters ---------- start : int_scalars, optional Starting value (inclusive) stop : int_scalars Stopping value (exclusive) stride : int_scalars, optional The difference between consecutive elements, the default stride is 1, if stride is specified then start must also be specified. Returns ------- pdarray, int64 Integers from start (inclusive) to stop (exclusive) by stride Raises ------ TypeError Raised if start, stop, or stride is not an int object ZeroDivisionError Raised if stride == 0 See Also -------- linspace, zeros, ones, randint Notes ----- Negative strides result in decreasing values. Currently, only int64 pdarrays can be created with this method. For float64 arrays, use the linspace method. Examples -------- >>> ak.arange(0, 5, 1) array([0, 1, 2, 3, 4]) >>> ak.arange(5, 0, -1) array([5, 4, 3, 2, 1]) >>> ak.arange(0, 10, 2) array([0, 2, 4, 6, 8]) >>> ak.arange(-5, -10, -1) array([-5, -6, -7, -8, -9]) """ #if one arg is given then arg is stop if len(args) == 1: start = 0 stop = args[0] stride = 1 #if two args are given then first arg is start and second is stop if len(args) == 2: start = args[0] stop = args[1] stride = 1 #if three args are given then first arg is start, #second is stop, third is stride if len(args) == 3: start = args[0] stop = args[1] stride = args[2] if stride == 0: raise ZeroDivisionError("division by zero") if isSupportedInt(start) and isSupportedInt(stop) and isSupportedInt(stride): if stride < 0: stop = stop + 2 repMsg = generic_msg(cmd='arange', args="{} {} {}".format(start, stop, stride)) return create_pdarray(repMsg) else: raise TypeError("start,stop,stride must be type int or np.int64 {} {} {}".\ format(start,stop,stride))