From 6659cff48fa15bf1624a0b37de192b76b500beb3 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 31 Oct 2018 09:12:33 -0700 Subject: [PATCH 1/3] avoid use of shallow_copy in EA mixins --- pandas/core/arrays/datetimelike.py | 4 +++- pandas/core/arrays/datetimes.py | 4 ++-- pandas/core/indexes/datetimes.py | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index dc7cf51ca109d..9972dfa34e6c5 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -422,7 +422,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/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) From f7c0382d78f28dc6c908d067dabdf1d06a5e959c Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 31 Oct 2018 09:36:12 -0700 Subject: [PATCH 2/3] Remove DatetimelikeArrayMixin._shallow_copy --- pandas/core/arrays/datetimelike.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 9972dfa34e6c5..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): """ From e0f02db0b09775c69d972ae8e25c2b630cef54f5 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 31 Oct 2018 09:38:10 -0700 Subject: [PATCH 3/3] simplify usages of shallow_copy in datetimelike indexes --- pandas/core/indexes/datetimelike.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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): """