Skip to content

Commit

Permalink
CLN: de-duplicate _local_timestamps (#36609)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Sep 24, 2020
1 parent e4086ec commit 6648439
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
31 changes: 8 additions & 23 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ def tz_to_dtype(tz):

def _field_accessor(name, field, docstring=None):
def f(self):
values = self.asi8
if self.tz is not None and not timezones.is_utc(self.tz):
values = self._local_timestamps()
values = self._local_timestamps()

if field in self._bool_ops:
if field.endswith(("start", "end")):
Expand Down Expand Up @@ -731,6 +729,8 @@ def _local_timestamps(self):
This is used to calculate time-of-day information as if the timestamps
were timezone-naive.
"""
if self.tz is None or timezones.is_utc(self.tz):
return self.asi8
return tzconversion.tz_convert_from_utc(self.asi8, self.tz)

def tz_convert(self, tz):
Expand Down Expand Up @@ -1167,10 +1167,7 @@ def month_name(self, locale=None):
>>> idx.month_name()
Index(['January', 'February', 'March'], dtype='object')
"""
if self.tz is not None and not timezones.is_utc(self.tz):
values = self._local_timestamps()
else:
values = self.asi8
values = self._local_timestamps()

result = fields.get_date_name_field(values, "month_name", locale=locale)
result = self._maybe_mask_results(result, fill_value=None)
Expand Down Expand Up @@ -1200,10 +1197,7 @@ def day_name(self, locale=None):
>>> idx.day_name()
Index(['Monday', 'Tuesday', 'Wednesday'], dtype='object')
"""
if self.tz is not None and not timezones.is_utc(self.tz):
values = self._local_timestamps()
else:
values = self.asi8
values = self._local_timestamps()

result = fields.get_date_name_field(values, "day_name", locale=locale)
result = self._maybe_mask_results(result, fill_value=None)
Expand All @@ -1217,10 +1211,7 @@ def time(self):
# If the Timestamps have a timezone that is not UTC,
# convert them into their i8 representation while
# keeping their timezone and not using UTC
if self.tz is not None and not timezones.is_utc(self.tz):
timestamps = self._local_timestamps()
else:
timestamps = self.asi8
timestamps = self._local_timestamps()

return ints_to_pydatetime(timestamps, box="time")

Expand All @@ -1241,10 +1232,7 @@ def date(self):
# If the Timestamps have a timezone that is not UTC,
# convert them into their i8 representation while
# keeping their timezone and not using UTC
if self.tz is not None and not timezones.is_utc(self.tz):
timestamps = self._local_timestamps()
else:
timestamps = self.asi8
timestamps = self._local_timestamps()

return ints_to_pydatetime(timestamps, box="date")

Expand Down Expand Up @@ -1283,10 +1271,7 @@ def isocalendar(self):
"""
from pandas import DataFrame

if self.tz is not None and not timezones.is_utc(self.tz):
values = self._local_timestamps()
else:
values = self.asi8
values = self._local_timestamps()
sarray = fields.build_isocalendar_sarray(values)
iso_calendar_df = DataFrame(
sarray, columns=["year", "week", "day"], dtype="UInt32"
Expand Down
12 changes: 2 additions & 10 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
import numpy as np

from pandas._libs import NaT, Period, Timestamp, index as libindex, lib
from pandas._libs.tslibs import (
Resolution,
ints_to_pydatetime,
parsing,
timezones,
to_offset,
)
from pandas._libs.tslibs import Resolution, ints_to_pydatetime, parsing, to_offset
from pandas._libs.tslibs.offsets import prefix_mapping
from pandas._typing import DtypeObj, Label
from pandas.errors import InvalidIndexError
Expand Down Expand Up @@ -395,9 +389,7 @@ def _get_time_micros(self):
-------
ndarray[int64_t]
"""
values = self.asi8
if self.tz is not None and not timezones.is_utc(self.tz):
values = self._data._local_timestamps()
values = self._data._local_timestamps()

nanos = values % (24 * 3600 * 1_000_000_000)
micros = nanos // 1000
Expand Down

0 comments on commit 6648439

Please sign in to comment.