Skip to content

implement ensure_localized in datetimelikeArrayMixin #24378

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 1 commit into from
Dec 21, 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
35 changes: 35 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,41 @@ def _evaluate_compare(self, other, op):
result[mask] = filler
return result

def _ensure_localized(self, arg, ambiguous='raise', nonexistent='raise',
from_utc=False):
"""
Ensure that we are re-localized.

This is for compat as we can then call this on all datetimelike
arrays generally (ignored for Period/Timedelta)

Parameters
----------
arg : Union[DatetimeLikeArray, DatetimeIndexOpsMixin, ndarray]
ambiguous : str, bool, or bool-ndarray, default 'raise'
nonexistent : str, default 'raise'
from_utc : bool, default False
If True, localize the i8 ndarray to UTC first before converting to
the appropriate tz. If False, localize directly to the tz.

Returns
-------
localized array
"""

# reconvert to local tz
tz = getattr(self, 'tz', None)
if tz is not None:
if not isinstance(arg, type(self)):
arg = self._simple_new(arg)
if from_utc:
arg = arg.tz_localize('UTC').tz_convert(self.tz)
else:
arg = arg.tz_localize(
self.tz, ambiguous=ambiguous, nonexistent=nonexistent
)
return arg


DatetimeLikeArrayMixin._add_comparison_ops()

Expand Down
41 changes: 11 additions & 30 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,17 @@ def _evaluate_compare(self, other, op):

def _ensure_localized(self, arg, ambiguous='raise', nonexistent='raise',
from_utc=False):
"""
Ensure that we are re-localized.

This is for compat as we can then call this on all datetimelike
indexes generally (ignored for Period/Timedelta)

Parameters
----------
arg : DatetimeIndex / i8 ndarray
ambiguous : str, bool, or bool-ndarray, default 'raise'
nonexistent : str, default 'raise'
from_utc : bool, default False
If True, localize the i8 ndarray to UTC first before converting to
the appropriate tz. If False, localize directly to the tz.

Returns
-------
localized DTI
"""

# reconvert to local tz
if getattr(self, 'tz', None) is not None:
if not isinstance(arg, ABCIndexClass):
arg = self._simple_new(arg)
if from_utc:
arg = arg.tz_localize('UTC').tz_convert(self.tz)
else:
arg = arg.tz_localize(
self.tz, ambiguous=ambiguous, nonexistent=nonexistent
)
# See DatetimeLikeArrayMixin._ensure_localized.__doc__

if getattr(self, 'tz', None):
# ensure_localized is only relevant for tz-aware DTI
from pandas.core.arrays import DatetimeArrayMixin as DatetimeArray
dtarr = DatetimeArray(self)
result = dtarr._ensure_localized(arg,
ambiguous=ambiguous,
nonexistent=nonexistent,
from_utc=from_utc)
return type(self)(result, name=self.name)
return arg

def _box_values_as_index(self):
Expand Down