diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index dc7cf51ca109d..dfc0a0f109836 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -89,18 +89,6 @@ def _get_attributes_dict(self): """return an attributes dict for my class""" return {k: getattr(self, k, None) for k in self._attributes} - def _shallow_copy(self, values=None, **kwargs): - if values is None: - # Note: slightly different from Index implementation which defaults - # to self.values - values = self._ndarray_values - - attributes = self._get_attributes_dict() - attributes.update(kwargs) - if not len(values) and 'dtype' not in kwargs: - attributes['dtype'] = self.dtype - return self._simple_new(values, **attributes) - class DatetimeLikeArrayMixin(ExtensionOpsMixin, AttributesMixin): """ @@ -422,7 +410,9 @@ def _add_nat(self): # and datetime dtypes result = np.zeros(len(self), dtype=np.int64) result.fill(iNaT) - return self._shallow_copy(result, freq=None) + if is_timedelta64_dtype(self): + return type(self)(result, freq=None) + return type(self)(result, tz=self.tz, freq=None) def _sub_nat(self): """Subtract pd.NaT from self""" diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index b656690b30e34..630c2843b747e 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -586,7 +586,7 @@ def tz_convert(self, tz): 'tz_localize to localize') # No conversion since timestamps are all UTC to begin with - return self._shallow_copy(tz=tz) + return self._simple_new(self.asi8, tz=tz, freq=self.freq) def tz_localize(self, tz, ambiguous='raise', nonexistent='raise', errors=None): @@ -708,7 +708,7 @@ def tz_localize(self, tz, ambiguous='raise', nonexistent='raise', self.asi8, tz, ambiguous=ambiguous, nonexistent=nonexistent, ) new_dates = new_dates.view(_NS_DTYPE) - return self._shallow_copy(new_dates, tz=tz) + return self._simple_new(new_dates, tz=tz, freq=self.freq) # ---------------------------------------------------------------- # Conversion Methods - Vectorized analogues of Timestamp methods diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 14325f42ff0d8..f2e31d1ab62c6 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -189,8 +189,7 @@ def _round(self, freq, mode, ambiguous): result = self._maybe_mask_results(result, fill_value=NaT) attribs = self._get_attributes_dict() - if 'freq' in attribs: - attribs['freq'] = None + attribs['freq'] = None if 'tz' in attribs: attribs['tz'] = None return self._ensure_localized( @@ -640,8 +639,7 @@ def where(self, cond, other=None): result = np.where(cond, values, other).astype('i8') result = self._ensure_localized(result, from_utc=True) - return self._shallow_copy(result, - **self._get_attributes_dict()) + return self._shallow_copy(result) def _summary(self, name=None): """ diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 39f247a7c4cfe..f129c5f9c81bd 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1134,6 +1134,8 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None): is_year_end = wrap_field_accessor(DatetimeArrayMixin.is_year_end) is_leap_year = wrap_field_accessor(DatetimeArrayMixin.is_leap_year) + tz_localize = wrap_array_method(DatetimeArrayMixin.tz_localize, True) + tz_convert = wrap_array_method(DatetimeArrayMixin.tz_convert, True) to_perioddelta = wrap_array_method(DatetimeArrayMixin.to_perioddelta, False) to_period = wrap_array_method(DatetimeArrayMixin.to_period, True)