Skip to content

REF: Remove DatetimelikeArrayMixin._shallow_copy #23430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why self.asi8 and not self or self._data here? Simple_new doesn't take M8 values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_simple_new(int64values, tz=tz) will get you the same thing as __new__(int64values, tz=tz), but the same is not true with M8 values. I find that really helpful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are referring to the timezone difference you mentioned earlier?

I personally find that very confusing that DatetimeArray(self._data, tz=..) and DatetimeArray(self._data.view(int), tz=..) do not give the same. To the extent possible, I would not repeat this pattern for our Arrays?
(of course, in the DatetimeIndex constructor we cannot just change it, unless we want to deprecate that first).


def tz_localize(self, tz, ambiguous='raise', nonexistent='raise',
errors=None):
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
"""
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down