def _add_datetimelike_scalar(self, other): # adding a timedeltaindex to a datetimelike from pandas.core.arrays import DatetimeArrayMixin assert other is not NaT other = Timestamp(other) if other is NaT: # In this case we specifically interpret NaT as a datetime, not # the timedelta interpretation we would get by returning self + NaT result = self.asi8.view('m8[ms]') + NaT.to_datetime64() return DatetimeArrayMixin(result) i8 = self.asi8 result = checked_add_with_arr(i8, other.value, arr_mask=self._isnan) result = self._maybe_mask_results(result) return DatetimeArrayMixin(result, tz=other.tz, freq=self.freq)
def test_astype_object(self, tz_naive_fixture): tz = tz_naive_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) arr = DatetimeArrayMixin(dti) asobj = arr.astype('O') assert isinstance(asobj, np.ndarray) assert asobj.dtype == 'O' assert list(asobj) == list(dti)
def test_int_properties(self, datetime_index, propname): dti = datetime_index arr = DatetimeArrayMixin(dti) result = getattr(arr, propname) expected = np.array(getattr(dti, propname), dtype=result.dtype) tm.assert_numpy_array_equal(result, expected)
def _add_datelike(self, other): # adding a timedeltaindex to a datetimelike from pandas.core.arrays import DatetimeArrayMixin if isinstance(other, (DatetimeArrayMixin, np.ndarray)): # if other is an ndarray, we assume it is datetime64-dtype # defer to implementation in DatetimeIndex if not isinstance(other, DatetimeArrayMixin): other = DatetimeArrayMixin(other) return other + self else: assert other is not NaT other = Timestamp(other) i8 = self.asi8 result = checked_add_with_arr(i8, other.value, arr_mask=self._isnan) result = self._maybe_mask_results(result, fill_value=iNaT) return DatetimeArrayMixin(result)
def _add_datetime_arraylike(self, other): """Add DatetimeArray/Index or ndarray[datetime64] to TimedeltaArray""" if isinstance(other, np.ndarray): # At this point we have already checked that dtype is datetime64 from pandas.core.arrays import DatetimeArrayMixin other = DatetimeArrayMixin(other) # defer to implementation in DatetimeArray return other + self
def test_bool_properties(self, datetime_index, propname): # in this case _bool_ops is just `is_leap_year` dti = datetime_index arr = DatetimeArrayMixin(dti) assert dti.freq == arr.freq result = getattr(arr, propname) expected = np.array(getattr(dti, propname), dtype=result.dtype) tm.assert_numpy_array_equal(result, expected)
def test_from_dti(self, tz_naive_fixture): tz = tz_naive_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) arr = DatetimeArrayMixin(dti) assert list(dti) == list(arr) # Check that Index.__new__ knows what to do with DatetimeArray dti2 = pd.Index(arr) assert isinstance(dti2, pd.DatetimeIndex) assert list(dti2) == list(arr)
def test_to_period(self, datetime_index, freqstr): dti = datetime_index arr = DatetimeArrayMixin(dti) expected = dti.to_period(freq=freqstr) result = arr.to_period(freq=freqstr) assert isinstance(result, PeriodArrayMixin) # placeholder until these become actual EA subclasses and we can use # an EA-specific tm.assert_ function tm.assert_index_equal(pd.Index(result), pd.Index(expected))
def test_to_timestamp(self, how, period_index): pi = period_index arr = PeriodArrayMixin(pi) expected = DatetimeArrayMixin(pi.to_timestamp(how=how)) result = arr.to_timestamp(how=how) assert isinstance(result, DatetimeArrayMixin) # placeholder until these become actual EA subclasses and we can use # an EA-specific tm.assert_ function tm.assert_index_equal(pd.Index(result), pd.Index(expected))
def test_array(self, tz_naive_fixture): # GH#23524 tz = tz_naive_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) arr = DatetimeArrayMixin(dti) expected = dti.asi8.view('M8[ns]') result = np.array(arr) tm.assert_numpy_array_equal(result, expected) # check that we are not making copies when setting copy=False result = np.array(arr, copy=False) assert result.base is expected.base assert result.base is not None
def test_array_object_dtype(self, tz_naive_fixture): # GH#23524 tz = tz_naive_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) arr = DatetimeArrayMixin(dti) expected = np.array(list(dti)) result = np.array(arr, dtype=object) tm.assert_numpy_array_equal(result, expected) # also test the DatetimeIndex method while we're at it result = np.array(dti, dtype=object) tm.assert_numpy_array_equal(result, expected)
def __rsub__(self, other): if is_datetime64_dtype(other) and is_timedelta64_dtype(self): # ndarray[datetime64] cannot be subtracted from self, so # we need to wrap in DatetimeArray/Index and flip the operation if not isinstance(other, DatetimeLikeArrayMixin): # Avoid down-casting DatetimeIndex from pandas.core.arrays import DatetimeArrayMixin other = DatetimeArrayMixin(other) return other - self elif (is_datetime64_any_dtype(self) and hasattr(other, 'dtype') and not is_datetime64_any_dtype(other)): # GH#19959 datetime - datetime is well-defined as timedelta, # but any other type - datetime is not well-defined. raise TypeError("cannot subtract {cls} from {typ}".format( cls=type(self).__name__, typ=type(other).__name__)) return -(self - other)
def to_timestamp(self, freq=None, how='start'): """ Cast to DatetimeArray/Index Parameters ---------- freq : string or DateOffset, optional Target frequency. The default is 'D' for week or longer, 'S' otherwise how : {'s', 'e', 'start', 'end'} Returns ------- DatetimeArray/Index """ from pandas.core.arrays import DatetimeArrayMixin how = libperiod._validate_end_alias(how) end = how == 'E' if end: if freq == 'B': # roll forward to ensure we land on B date adjust = Timedelta(1, 'D') - Timedelta(1, 'ns') return self.to_timestamp(how='start') + adjust else: adjust = Timedelta(1, 'ns') return (self + 1).to_timestamp(how='start') - adjust if freq is None: base, mult = frequencies.get_freq_code(self.freq) freq = frequencies.get_to_timestamp_base(base) else: freq = Period._maybe_convert_freq(freq) base, mult = frequencies.get_freq_code(freq) new_data = self.asfreq(freq, how=how) new_data = libperiod.periodarr_to_dt64arr(new_data._ndarray_values, base) return DatetimeArrayMixin(new_data, freq='infer')