Skip to content

REF: avoid ravel in DatetimeArray.to_period #46816

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 1 commit into from
Apr 22, 2022
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
46 changes: 27 additions & 19 deletions pandas/_libs/tslibs/vectorized.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -355,36 +355,44 @@ def is_date_array_normalized(const int64_t[:] stamps, tzinfo tz=None) -> bool:

@cython.wraparound(False)
@cython.boundscheck(False)
def dt64arr_to_periodarr(const int64_t[:] stamps, int freq, tzinfo tz):
def dt64arr_to_periodarr(ndarray stamps, int freq, tzinfo tz):
# stamps is int64_t, arbitrary ndim
cdef:
Localizer info = Localizer(tz)
int64_t utc_val, local_val
Py_ssize_t pos, i, n = stamps.shape[0]
Py_ssize_t pos, i, n = stamps.size
int64_t utc_val, local_val, res_val
int64_t* tdata = NULL

npy_datetimestruct dts
int64_t[::1] result = np.empty(n, dtype=np.int64)
ndarray result = cnp.PyArray_EMPTY(stamps.ndim, stamps.shape, cnp.NPY_INT64, 0)
cnp.broadcast mi = cnp.PyArray_MultiIterNew2(result, stamps)

if info.use_dst:
tdata = <int64_t*>cnp.PyArray_DATA(info.trans)

for i in range(n):
utc_val = stamps[i]
if utc_val == NPY_NAT:
result[i] = NPY_NAT
continue
# Analogous to: utc_val = stamps[i]
utc_val = (<int64_t*>cnp.PyArray_MultiIter_DATA(mi, 1))[0]

if info.use_utc:
local_val = utc_val
elif info.use_tzlocal:
local_val = utc_val + localize_tzinfo_api(utc_val, tz)
elif info.use_fixed:
local_val = utc_val + info.delta
if utc_val == NPY_NAT:
res_val = NPY_NAT
else:
pos = bisect_right_i8(tdata, utc_val, info.ntrans) - 1
local_val = utc_val + info.deltas[pos]
if info.use_utc:
local_val = utc_val
elif info.use_tzlocal:
local_val = utc_val + localize_tzinfo_api(utc_val, tz)
elif info.use_fixed:
local_val = utc_val + info.delta
else:
pos = bisect_right_i8(tdata, utc_val, info.ntrans) - 1
local_val = utc_val + info.deltas[pos]

dt64_to_dtstruct(local_val, &dts)
result[i] = get_period_ordinal(&dts, freq)
dt64_to_dtstruct(local_val, &dts)
res_val = get_period_ordinal(&dts, freq)

# Analogous to: result[i] = res_val
(<int64_t*>cnp.PyArray_MultiIter_DATA(mi, 0))[0] = res_val

return result.base # .base to get underlying ndarray
cnp.PyArray_MultiIter_NEXT(mi)

return result
1 change: 0 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,6 @@ def normalize(self) -> DatetimeArray:
new_values = normalize_i8_timestamps(self.asi8, self.tz)
return type(self)(new_values)._with_freq("infer").tz_localize(self.tz)

@dtl.ravel_compat
def to_period(self, freq=None) -> PeriodArray:
"""
Cast to PeriodArray/Index at a particular frequency.
Expand Down