Skip to content

REF: use standard pattern in normalize_i8_timestamps #34507

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

Merged
merged 3 commits into from
Jun 1, 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
11 changes: 10 additions & 1 deletion pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,16 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
npy_datetimestruct dts
int64_t delta, local_val

if is_tzlocal(tz):
if tz is None or is_utc(tz):
with nogil:
for i in range(n):
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
local_val = stamps[i]
dt64_to_dtstruct(local_val, &dts)
result[i] = _normalized_stamp(&dts)
elif is_tzlocal(tz):
for i in range(n):
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
Expand Down
7 changes: 1 addition & 6 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1451,13 +1451,8 @@ default 'raise'
ndarray[int64_t] normalized
tzinfo own_tz = self.tzinfo # could be None

if own_tz is None or is_utc(own_tz):
DAY_NS = ccalendar.DAY_NANOS
normalized_value = self.value - (self.value % DAY_NS)
return Timestamp(normalized_value).tz_localize(own_tz)

normalized = normalize_i8_timestamps(
np.array([self.value], dtype='i8'), tz=own_tz)
np.array([self.value], dtype="i8"), tz=own_tz)
return Timestamp(normalized[0]).tz_localize(own_tz)


Expand Down
12 changes: 2 additions & 10 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
from pandas._libs.tslibs import (
NaT,
Timestamp,
ccalendar,
conversion,
fields,
frequencies as libfrequencies,
iNaT,
resolution as libresolution,
timezones,
tzconversion,
)
import pandas._libs.tslibs.frequencies as libfrequencies
from pandas.errors import PerformanceWarning

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -1036,14 +1035,7 @@ def normalize(self):
'2014-08-01 00:00:00+05:30'],
dtype='datetime64[ns, Asia/Calcutta]', freq=None)
"""
if self.tz is None or timezones.is_utc(self.tz):
Copy link
Contributor

Choose a reason for hiding this comment

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

this fastpath looks like it was removed in favor of the loop in normalize_i8_timestamps. cc @jbrockmendel

not_null = ~self.isna()
DAY_NS = ccalendar.DAY_SECONDS * 1_000_000_000
new_values = self.asi8.copy()
adjustment = new_values[not_null] % DAY_NS
new_values[not_null] = new_values[not_null] - adjustment
else:
new_values = conversion.normalize_i8_timestamps(self.asi8, self.tz)
new_values = conversion.normalize_i8_timestamps(self.asi8, self.tz)
return type(self)(new_values)._with_freq("infer").tz_localize(self.tz)

def to_period(self, freq=None):
Expand Down