def test_is_datetimelike(): assert not com.is_datetimelike([1, 2, 3]) assert not com.is_datetimelike(pd.Index([1, 2, 3])) assert com.is_datetimelike(pd.DatetimeIndex([1, 2, 3])) assert com.is_datetimelike(pd.PeriodIndex([], freq="A")) assert com.is_datetimelike(np.array([], dtype=np.datetime64)) assert com.is_datetimelike(pd.Series([], dtype="timedelta64[ns]")) assert com.is_datetimelike(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern")) dtype = DatetimeTZDtype("ns", tz="US/Eastern") s = pd.Series([], dtype=dtype) assert com.is_datetimelike(s)
def test_is_datetimelike(): assert not com.is_datetimelike([1, 2, 3]) assert not com.is_datetimelike(pd.Index([1, 2, 3])) assert com.is_datetimelike(pd.DatetimeIndex([1, 2, 3])) assert com.is_datetimelike(pd.PeriodIndex([], freq="A")) assert com.is_datetimelike(np.array([], dtype=np.datetime64)) assert com.is_datetimelike(pd.Series([], dtype="timedelta64[ns]")) assert com.is_datetimelike(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern")) dtype = DatetimeTZDtype("ns", tz="US/Eastern") s = pd.Series([], dtype=dtype) assert com.is_datetimelike(s)
def __iter__(self): """ Return an iterator of the values. These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period) """ # We are explicity making element iterators. if is_datetimelike(self._values): return map(com.maybe_box_datetimelike, self._values) elif is_extension_array_dtype(self._values): return iter(self._values) else: return map(self._values.item, range(self._values.size))
def __iter__(self): """ Return an iterator of the values. These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period) """ # We are explicity making element iterators. if is_datetimelike(self._values): return map(com.maybe_box_datetimelike, self._values) elif is_extension_array_dtype(self._values): return iter(self._values) else: return map(self._values.item, range(self._values.size))
def tolist(self): """ Return a list of the values. These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period) See Also -------- numpy.ndarray.tolist """ if is_datetimelike(self): return [com._maybe_box_datetimelike(x) for x in self._values] else: return self._values.tolist()
def tolist(self): """ Return a list of the values. These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period) See Also -------- numpy.ndarray.tolist """ if is_datetimelike(self): return [_maybe_box_datetimelike(x) for x in self._values] else: return self._values.tolist()
def wrapper(self, other): meth = getattr(dtl.DatetimeLikeArrayMixin, opname) if isinstance(other, (datetime, np.datetime64, compat.string_types)): if isinstance(other, datetime): # GH#18435 strings get a pass from tzawareness compat self._assert_tzawareness_compat(other) other = _to_m8(other, tz=self.tz) result = meth(self, other) if isna(other): result.fill(nat_result) else: if isinstance(other, list): other = type(self)(other) elif not isinstance(other, (np.ndarray, ABCIndexClass, ABCSeries)): # Following Timestamp convention, __eq__ is all-False # and __ne__ is all True, others raise TypeError. if opname == '__eq__': return np.zeros(shape=self.shape, dtype=bool) elif opname == '__ne__': return np.ones(shape=self.shape, dtype=bool) raise TypeError('%s type object %s' % (type(other), str(other))) if is_datetimelike(other): self._assert_tzawareness_compat(other) result = meth(self, np.asarray(other)) result = com.values_from_object(result) # Make sure to pass an array to result[...]; indexing with # Series breaks with older version of numpy o_mask = np.array(isna(other)) if o_mask.any(): result[o_mask] = nat_result if self.hasnans: result[self._isnan] = nat_result return result
def wrapper(self, other): meth = getattr(dtl.DatetimeLikeArrayMixin, opname) if isinstance(other, (datetime, np.datetime64, compat.string_types)): if isinstance(other, datetime): # GH#18435 strings get a pass from tzawareness compat self._assert_tzawareness_compat(other) other = _to_m8(other, tz=self.tz) result = meth(self, other) if isna(other): result.fill(nat_result) else: if isinstance(other, list): other = type(self)(other) elif not isinstance(other, (np.ndarray, ABCIndexClass, ABCSeries)): # Following Timestamp convention, __eq__ is all-False # and __ne__ is all True, others raise TypeError. if opname == '__eq__': return np.zeros(shape=self.shape, dtype=bool) elif opname == '__ne__': return np.ones(shape=self.shape, dtype=bool) raise TypeError('%s type object %s' % (type(other), str(other))) if is_datetimelike(other): self._assert_tzawareness_compat(other) result = meth(self, np.asarray(other)) result = com._values_from_object(result) # Make sure to pass an array to result[...]; indexing with # Series breaks with older version of numpy o_mask = np.array(isna(other)) if o_mask.any(): result[o_mask] = nat_result if self.hasnans: result[self._isnan] = nat_result return result