Skip to content

Commit 4fcc6a6

Browse files
authored
REF: use standard pattern in normalize_i8_timestamps (#34507)
* REF: use standard pattern for normalize_i8_timestamps * unused import
1 parent 45d0898 commit 4fcc6a6

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

pandas/_libs/tslibs/conversion.pyx

+10-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,16 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
788788
npy_datetimestruct dts
789789
int64_t delta, local_val
790790

791-
if is_tzlocal(tz):
791+
if tz is None or is_utc(tz):
792+
with nogil:
793+
for i in range(n):
794+
if stamps[i] == NPY_NAT:
795+
result[i] = NPY_NAT
796+
continue
797+
local_val = stamps[i]
798+
dt64_to_dtstruct(local_val, &dts)
799+
result[i] = _normalized_stamp(&dts)
800+
elif is_tzlocal(tz):
792801
for i in range(n):
793802
if stamps[i] == NPY_NAT:
794803
result[i] = NPY_NAT

pandas/_libs/tslibs/timestamps.pyx

+1-6
Original file line numberDiff line numberDiff line change
@@ -1451,13 +1451,8 @@ default 'raise'
14511451
ndarray[int64_t] normalized
14521452
tzinfo own_tz = self.tzinfo # could be None
14531453

1454-
if own_tz is None or is_utc(own_tz):
1455-
DAY_NS = ccalendar.DAY_NANOS
1456-
normalized_value = self.value - (self.value % DAY_NS)
1457-
return Timestamp(normalized_value).tz_localize(own_tz)
1458-
14591454
normalized = normalize_i8_timestamps(
1460-
np.array([self.value], dtype='i8'), tz=own_tz)
1455+
np.array([self.value], dtype="i8"), tz=own_tz)
14611456
return Timestamp(normalized[0]).tz_localize(own_tz)
14621457

14631458

pandas/core/arrays/datetimes.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
from pandas._libs.tslibs import (
1010
NaT,
1111
Timestamp,
12-
ccalendar,
1312
conversion,
1413
fields,
14+
frequencies as libfrequencies,
1515
iNaT,
1616
resolution as libresolution,
1717
timezones,
1818
tzconversion,
1919
)
20-
import pandas._libs.tslibs.frequencies as libfrequencies
2120
from pandas.errors import PerformanceWarning
2221

2322
from pandas.core.dtypes.common import (
@@ -1036,14 +1035,7 @@ def normalize(self):
10361035
'2014-08-01 00:00:00+05:30'],
10371036
dtype='datetime64[ns, Asia/Calcutta]', freq=None)
10381037
"""
1039-
if self.tz is None or timezones.is_utc(self.tz):
1040-
not_null = ~self.isna()
1041-
DAY_NS = ccalendar.DAY_SECONDS * 1_000_000_000
1042-
new_values = self.asi8.copy()
1043-
adjustment = new_values[not_null] % DAY_NS
1044-
new_values[not_null] = new_values[not_null] - adjustment
1045-
else:
1046-
new_values = conversion.normalize_i8_timestamps(self.asi8, self.tz)
1038+
new_values = conversion.normalize_i8_timestamps(self.asi8, self.tz)
10471039
return type(self)(new_values)._with_freq("infer").tz_localize(self.tz)
10481040

10491041
def to_period(self, freq=None):

0 commit comments

Comments
 (0)