Skip to content

Commit 6648439

Browse files
authored
CLN: de-duplicate _local_timestamps (#36609)
1 parent e4086ec commit 6648439

File tree

2 files changed

+10
-33
lines changed

2 files changed

+10
-33
lines changed

pandas/core/arrays/datetimes.py

+8-23
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ def tz_to_dtype(tz):
7676

7777
def _field_accessor(name, field, docstring=None):
7878
def f(self):
79-
values = self.asi8
80-
if self.tz is not None and not timezones.is_utc(self.tz):
81-
values = self._local_timestamps()
79+
values = self._local_timestamps()
8280

8381
if field in self._bool_ops:
8482
if field.endswith(("start", "end")):
@@ -731,6 +729,8 @@ def _local_timestamps(self):
731729
This is used to calculate time-of-day information as if the timestamps
732730
were timezone-naive.
733731
"""
732+
if self.tz is None or timezones.is_utc(self.tz):
733+
return self.asi8
734734
return tzconversion.tz_convert_from_utc(self.asi8, self.tz)
735735

736736
def tz_convert(self, tz):
@@ -1167,10 +1167,7 @@ def month_name(self, locale=None):
11671167
>>> idx.month_name()
11681168
Index(['January', 'February', 'March'], dtype='object')
11691169
"""
1170-
if self.tz is not None and not timezones.is_utc(self.tz):
1171-
values = self._local_timestamps()
1172-
else:
1173-
values = self.asi8
1170+
values = self._local_timestamps()
11741171

11751172
result = fields.get_date_name_field(values, "month_name", locale=locale)
11761173
result = self._maybe_mask_results(result, fill_value=None)
@@ -1200,10 +1197,7 @@ def day_name(self, locale=None):
12001197
>>> idx.day_name()
12011198
Index(['Monday', 'Tuesday', 'Wednesday'], dtype='object')
12021199
"""
1203-
if self.tz is not None and not timezones.is_utc(self.tz):
1204-
values = self._local_timestamps()
1205-
else:
1206-
values = self.asi8
1200+
values = self._local_timestamps()
12071201

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

12251216
return ints_to_pydatetime(timestamps, box="time")
12261217

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

12491237
return ints_to_pydatetime(timestamps, box="date")
12501238

@@ -1283,10 +1271,7 @@ def isocalendar(self):
12831271
"""
12841272
from pandas import DataFrame
12851273

1286-
if self.tz is not None and not timezones.is_utc(self.tz):
1287-
values = self._local_timestamps()
1288-
else:
1289-
values = self.asi8
1274+
values = self._local_timestamps()
12901275
sarray = fields.build_isocalendar_sarray(values)
12911276
iso_calendar_df = DataFrame(
12921277
sarray, columns=["year", "week", "day"], dtype="UInt32"

pandas/core/indexes/datetimes.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66
import numpy as np
77

88
from pandas._libs import NaT, Period, Timestamp, index as libindex, lib
9-
from pandas._libs.tslibs import (
10-
Resolution,
11-
ints_to_pydatetime,
12-
parsing,
13-
timezones,
14-
to_offset,
15-
)
9+
from pandas._libs.tslibs import Resolution, ints_to_pydatetime, parsing, to_offset
1610
from pandas._libs.tslibs.offsets import prefix_mapping
1711
from pandas._typing import DtypeObj, Label
1812
from pandas.errors import InvalidIndexError
@@ -395,9 +389,7 @@ def _get_time_micros(self):
395389
-------
396390
ndarray[int64_t]
397391
"""
398-
values = self.asi8
399-
if self.tz is not None and not timezones.is_utc(self.tz):
400-
values = self._data._local_timestamps()
392+
values = self._data._local_timestamps()
401393

402394
nanos = values % (24 * 3600 * 1_000_000_000)
403395
micros = nanos // 1000

0 commit comments

Comments
 (0)