Skip to content
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

CLN: de-duplicate _local_timestamps #36609

Merged
merged 1 commit into from
Sep 24, 2020
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
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)
Comment on lines +732 to 734
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch of duplicate pattern throughout the module.


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