-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: Cleanups, typing, memoryviews in tslibs #23368
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
37239cc
tslibs cleanup, typing, memoryviews
jbrockmendel c7bd679
cleanup and typing
jbrockmendel e2a2df9
small optimizations
jbrockmendel ae04c77
optimizations
jbrockmendel c89ddff
Whitespace fixup
jbrockmendel 498cd64
Merge branch 'master' of https://github.com/pandas-dev/pandas into li…
jbrockmendel 77039a3
Merge branch 'master' of https://github.com/pandas-dev/pandas into li…
jbrockmendel f501cd7
use np.asarray instead of .base
jbrockmendel f57f994
Merge branch 'master' of https://github.com/pandas-dev/pandas into li…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,7 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1: | |
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def ensure_datetime64ns(ndarray arr, copy=True): | ||
def ensure_datetime64ns(arr: ndarray, copy: bool=True): | ||
""" | ||
Ensure a np.datetime64 array has dtype specifically 'datetime64[ns]' | ||
|
||
|
@@ -122,7 +122,7 @@ def ensure_datetime64ns(ndarray arr, copy=True): | |
return result | ||
|
||
|
||
def ensure_timedelta64ns(ndarray arr, copy=True): | ||
def ensure_timedelta64ns(arr: ndarray, copy: bint=True): | ||
""" | ||
Ensure a np.timedelta64 array has dtype specifically 'timedelta64[ns]' | ||
|
||
|
@@ -134,14 +134,14 @@ def ensure_timedelta64ns(ndarray arr, copy=True): | |
Returns | ||
------- | ||
result : ndarray with dtype timedelta64[ns] | ||
|
||
""" | ||
return arr.astype(TD_DTYPE, copy=copy) | ||
# TODO: check for overflows when going from a lower-resolution to nanos | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def datetime_to_datetime64(object[:] values): | ||
def datetime_to_datetime64(values: object[:]): | ||
""" | ||
Convert ndarray of datetime-like objects to int64 array representing | ||
nanosecond timestamps. | ||
|
@@ -614,6 +614,8 @@ cpdef inline datetime localize_pydatetime(datetime dt, object tz): | |
# ---------------------------------------------------------------------- | ||
# Timezone Conversion | ||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
cdef inline int64_t[:] _tz_convert_dst(int64_t[:] values, tzinfo tz, | ||
bint to_utc=True): | ||
""" | ||
|
@@ -852,14 +854,14 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, | |
localized : ndarray[int64_t] | ||
""" | ||
cdef: | ||
ndarray[int64_t] trans | ||
ndarray[int64_t] trans, result_a, result_b | ||
int64_t[:] deltas, idx_shifted | ||
ndarray ambiguous_array | ||
Py_ssize_t i, idx, pos, ntrans, n = len(vals) | ||
Py_ssize_t delta_idx_offset, delta_idx | ||
int64_t *tdata | ||
int64_t[:] result, dst_hours, idx_shifted_left, idx_shifted_right | ||
int64_t v, left, right, val, v_left, v_right, new_local, remaining_mins | ||
ndarray[int64_t] result, result_a, result_b, dst_hours | ||
Py_ssize_t i, idx, pos, pos_left, pos_right, ntrans, n = len(vals) | ||
Py_ssize_t delta_idx, delta_idx_offset | ||
int64_t *tdata | ||
ndarray ambiguous_array | ||
npy_datetimestruct dts | ||
bint infer_dst = False, is_dst = False, fill = False | ||
bint shift = False, fill_nonexist = False | ||
|
@@ -874,7 +876,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, | |
for i in range(n): | ||
v = vals[i] | ||
result[i] = _tz_convert_tzlocal_utc(v, tz, to_utc=True) | ||
return result | ||
return np.asarray(result) | ||
|
||
if is_string_object(ambiguous): | ||
if ambiguous == 'infer': | ||
|
@@ -936,7 +938,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, | |
|
||
if infer_dst: | ||
dst_hours = np.empty(n, dtype=np.int64) | ||
dst_hours.fill(NPY_NAT) | ||
dst_hours[:] = NPY_NAT | ||
|
||
# Get the ambiguous hours (given the above, these are the hours | ||
# where result_a != result_b and neither of them are NAT) | ||
|
@@ -977,7 +979,13 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, | |
# Pull the only index and adjust | ||
a_idx = grp[:switch_idx] | ||
b_idx = grp[switch_idx:] | ||
dst_hours[grp] = np.hstack((result_a[a_idx], result_b[b_idx])) | ||
|
||
# __setitem__ on dst_hours.base because indexing with | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would really rather not use .base |
||
# an ndarray (grp) directly on a memoryview is not supported | ||
# TODO: is `grp` necessarily contiguous? i.e. could we | ||
# equivalently write dst_hours[grp[0]:grp[-1]] = ... ? | ||
dst_hours.base[grp] = np.hstack((result_a[a_idx], | ||
result_b[b_idx])) | ||
|
||
for i in range(n): | ||
val = vals[i] | ||
|
@@ -1026,7 +1034,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, | |
stamp = _render_tstamp(val) | ||
raise pytz.NonExistentTimeError(stamp) | ||
|
||
return result | ||
return np.asarray(result) | ||
|
||
|
||
cdef inline bisect_right_i8(int64_t *data, int64_t val, Py_ssize_t n): | ||
|
@@ -1128,7 +1136,7 @@ def normalize_i8_timestamps(int64_t[:] stamps, object tz=None): | |
dt64_to_dtstruct(stamps[i], &dts) | ||
result[i] = _normalized_stamp(&dts) | ||
|
||
return result.base # .base to access underlying np.ndarray | ||
return np.asarray(result) | ||
|
||
|
||
@cython.wraparound(False) | ||
|
@@ -1220,7 +1228,7 @@ cdef inline int64_t _normalized_stamp(npy_datetimestruct *dts) nogil: | |
|
||
@cython.wraparound(False) | ||
@cython.boundscheck(False) | ||
def is_date_array_normalized(int64_t[:] stamps, tz=None): | ||
def is_date_array_normalized(int64_t[:] stamps, object tz=None): | ||
""" | ||
Check if all of the given (nanosecond) timestamps are normalized to | ||
midnight, i.e. hour == minute == second == 0. If the optional timezone | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this not do anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT the "inline" doesn't get carried across modules in this context. Since the function is never called from within ccalendar, its not accomplishing anything.