Skip to content

Commit a70694e

Browse files
jbrockmendeljreback
authored andcommitted
implement ensure_localized in datetimelikeArrayMixin (pandas-dev#24378)
1 parent 04a0eac commit a70694e

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

pandas/core/arrays/datetimelike.py

+35
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,41 @@ def _evaluate_compare(self, other, op):
10791079
result[mask] = filler
10801080
return result
10811081

1082+
def _ensure_localized(self, arg, ambiguous='raise', nonexistent='raise',
1083+
from_utc=False):
1084+
"""
1085+
Ensure that we are re-localized.
1086+
1087+
This is for compat as we can then call this on all datetimelike
1088+
arrays generally (ignored for Period/Timedelta)
1089+
1090+
Parameters
1091+
----------
1092+
arg : Union[DatetimeLikeArray, DatetimeIndexOpsMixin, ndarray]
1093+
ambiguous : str, bool, or bool-ndarray, default 'raise'
1094+
nonexistent : str, default 'raise'
1095+
from_utc : bool, default False
1096+
If True, localize the i8 ndarray to UTC first before converting to
1097+
the appropriate tz. If False, localize directly to the tz.
1098+
1099+
Returns
1100+
-------
1101+
localized array
1102+
"""
1103+
1104+
# reconvert to local tz
1105+
tz = getattr(self, 'tz', None)
1106+
if tz is not None:
1107+
if not isinstance(arg, type(self)):
1108+
arg = self._simple_new(arg)
1109+
if from_utc:
1110+
arg = arg.tz_localize('UTC').tz_convert(self.tz)
1111+
else:
1112+
arg = arg.tz_localize(
1113+
self.tz, ambiguous=ambiguous, nonexistent=nonexistent
1114+
)
1115+
return arg
1116+
10821117

10831118
DatetimeLikeArrayMixin._add_comparison_ops()
10841119

pandas/core/indexes/datetimelike.py

+11-30
Original file line numberDiff line numberDiff line change
@@ -111,36 +111,17 @@ def _evaluate_compare(self, other, op):
111111

112112
def _ensure_localized(self, arg, ambiguous='raise', nonexistent='raise',
113113
from_utc=False):
114-
"""
115-
Ensure that we are re-localized.
116-
117-
This is for compat as we can then call this on all datetimelike
118-
indexes generally (ignored for Period/Timedelta)
119-
120-
Parameters
121-
----------
122-
arg : DatetimeIndex / i8 ndarray
123-
ambiguous : str, bool, or bool-ndarray, default 'raise'
124-
nonexistent : str, default 'raise'
125-
from_utc : bool, default False
126-
If True, localize the i8 ndarray to UTC first before converting to
127-
the appropriate tz. If False, localize directly to the tz.
128-
129-
Returns
130-
-------
131-
localized DTI
132-
"""
133-
134-
# reconvert to local tz
135-
if getattr(self, 'tz', None) is not None:
136-
if not isinstance(arg, ABCIndexClass):
137-
arg = self._simple_new(arg)
138-
if from_utc:
139-
arg = arg.tz_localize('UTC').tz_convert(self.tz)
140-
else:
141-
arg = arg.tz_localize(
142-
self.tz, ambiguous=ambiguous, nonexistent=nonexistent
143-
)
114+
# See DatetimeLikeArrayMixin._ensure_localized.__doc__
115+
116+
if getattr(self, 'tz', None):
117+
# ensure_localized is only relevant for tz-aware DTI
118+
from pandas.core.arrays import DatetimeArrayMixin as DatetimeArray
119+
dtarr = DatetimeArray(self)
120+
result = dtarr._ensure_localized(arg,
121+
ambiguous=ambiguous,
122+
nonexistent=nonexistent,
123+
from_utc=from_utc)
124+
return type(self)(result, name=self.name)
144125
return arg
145126

146127
def _box_values_as_index(self):

0 commit comments

Comments
 (0)