Skip to content

Commit c9dd346

Browse files
jbrockmendelvictor
authored and
victor
committed
implement _local_timestamps in DatetimeArrayMixin (pandas-dev#21721)
1 parent 66d1cb5 commit c9dd346

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

pandas/core/arrays/datetimes.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import warnings
33

44
import numpy as np
5+
from pytz import utc
56

67
from pandas._libs import tslib
78
from pandas._libs.tslib import Timestamp, NaT, iNaT
8-
from pandas._libs.tslibs import timezones
9+
from pandas._libs.tslibs import conversion, timezones
910

1011
from pandas.util._decorators import cache_readonly
1112

@@ -110,6 +111,25 @@ def _sub_datelike_dti(self, other):
110111
new_values[mask] = iNaT
111112
return new_values.view('timedelta64[ns]')
112113

114+
# -----------------------------------------------------------------
115+
# Timezone Conversion and Localization Methods
116+
117+
def _local_timestamps(self):
118+
"""
119+
Convert to an i8 (unix-like nanosecond timestamp) representation
120+
while keeping the local timezone and not using UTC.
121+
This is used to calculate time-of-day information as if the timestamps
122+
were timezone-naive.
123+
"""
124+
values = self.asi8
125+
indexer = values.argsort()
126+
result = conversion.tz_convert(values.take(indexer), utc, self.tz)
127+
128+
n = len(indexer)
129+
reverse = np.empty(n, dtype=np.int_)
130+
reverse.put(indexer, np.arange(n))
131+
return result.take(reverse)
132+
113133
# ----------------------------------------------------------------
114134
# Conversion Methods - Vectorized analogues of Timedelta methods
115135

pandas/core/indexes/datetimes.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,7 @@ def _local_timestamps(self):
603603
if self.is_monotonic:
604604
return conversion.tz_convert(self.asi8, utc, self.tz)
605605
else:
606-
values = self.asi8
607-
indexer = values.argsort()
608-
result = conversion.tz_convert(values.take(indexer), utc, self.tz)
609-
610-
n = len(indexer)
611-
reverse = np.empty(n, dtype=np.int_)
612-
reverse.put(indexer, np.arange(n))
613-
return result.take(reverse)
606+
return DatetimeArrayMixin._local_timestamps(self)
614607

615608
@classmethod
616609
def _simple_new(cls, values, name=None, freq=None, tz=None,

0 commit comments

Comments
 (0)