Skip to content

Commit 7a0518d

Browse files
jbrockmendelPingviinituutti
authored andcommitted
REF: Remove DatetimelikeArrayMixin._shallow_copy (pandas-dev#23430)
1 parent 8cb6f51 commit 7a0518d

File tree

4 files changed

+9
-19
lines changed

4 files changed

+9
-19
lines changed

pandas/core/arrays/datetimelike.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,6 @@ def _get_attributes_dict(self):
9090
"""return an attributes dict for my class"""
9191
return {k: getattr(self, k, None) for k in self._attributes}
9292

93-
def _shallow_copy(self, values=None, **kwargs):
94-
if values is None:
95-
# Note: slightly different from Index implementation which defaults
96-
# to self.values
97-
values = self._ndarray_values
98-
99-
attributes = self._get_attributes_dict()
100-
attributes.update(kwargs)
101-
if not len(values) and 'dtype' not in kwargs:
102-
attributes['dtype'] = self.dtype
103-
return self._simple_new(values, **attributes)
104-
10593

10694
class DatetimeLikeArrayMixin(ExtensionOpsMixin, AttributesMixin):
10795
"""
@@ -423,7 +411,9 @@ def _add_nat(self):
423411
# and datetime dtypes
424412
result = np.zeros(len(self), dtype=np.int64)
425413
result.fill(iNaT)
426-
return self._shallow_copy(result, freq=None)
414+
if is_timedelta64_dtype(self):
415+
return type(self)(result, freq=None)
416+
return type(self)(result, tz=self.tz, freq=None)
427417

428418
def _sub_nat(self):
429419
"""Subtract pd.NaT from self"""

pandas/core/arrays/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ def tz_convert(self, tz):
594594
'tz_localize to localize')
595595

596596
# No conversion since timestamps are all UTC to begin with
597-
return self._shallow_copy(tz=tz)
597+
return self._simple_new(self.asi8, tz=tz, freq=self.freq)
598598

599599
def tz_localize(self, tz, ambiguous='raise', nonexistent='raise',
600600
errors=None):
@@ -716,7 +716,7 @@ def tz_localize(self, tz, ambiguous='raise', nonexistent='raise',
716716
self.asi8, tz, ambiguous=ambiguous, nonexistent=nonexistent,
717717
)
718718
new_dates = new_dates.view(_NS_DTYPE)
719-
return self._shallow_copy(new_dates, tz=tz)
719+
return self._simple_new(new_dates, tz=tz, freq=self.freq)
720720

721721
# ----------------------------------------------------------------
722722
# Conversion Methods - Vectorized analogues of Timestamp methods

pandas/core/indexes/datetimelike.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ def _round(self, freq, mode, ambiguous):
189189
result = self._maybe_mask_results(result, fill_value=NaT)
190190

191191
attribs = self._get_attributes_dict()
192-
if 'freq' in attribs:
193-
attribs['freq'] = None
192+
attribs['freq'] = None
194193
if 'tz' in attribs:
195194
attribs['tz'] = None
196195
return self._ensure_localized(
@@ -640,8 +639,7 @@ def where(self, cond, other=None):
640639
result = np.where(cond, values, other).astype('i8')
641640

642641
result = self._ensure_localized(result, from_utc=True)
643-
return self._shallow_copy(result,
644-
**self._get_attributes_dict())
642+
return self._shallow_copy(result)
645643

646644
def _summary(self, name=None):
647645
"""

pandas/core/indexes/datetimes.py

+2
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,8 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
11241124
is_year_end = wrap_field_accessor(DatetimeArrayMixin.is_year_end)
11251125
is_leap_year = wrap_field_accessor(DatetimeArrayMixin.is_leap_year)
11261126

1127+
tz_localize = wrap_array_method(DatetimeArrayMixin.tz_localize, True)
1128+
tz_convert = wrap_array_method(DatetimeArrayMixin.tz_convert, True)
11271129
to_perioddelta = wrap_array_method(DatetimeArrayMixin.to_perioddelta,
11281130
False)
11291131
to_period = wrap_array_method(DatetimeArrayMixin.to_period, True)

0 commit comments

Comments
 (0)