-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF factor out parse_pydatetime from array_to_datetime #49866
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
MarcoGorelli
merged 6 commits into
pandas-dev:main
from
MarcoGorelli:factor-out-handle-pydatetime
Nov 24, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93d80a8
factor out parse_pydatetime
657699e
fix segfault
51db700
:truck: rename validate_tzout to convert_timezone
0f68b41
Merge remote-tracking branch 'upstream/main' into factor-out-handle-p…
ebb8b39
change ensure_reso return value
b99ceb5
add except? -1 to ensure_reso
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
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 |
---|---|---|
|
@@ -41,6 +41,7 @@ from pandas._libs.tslibs.np_datetime cimport ( | |
npy_datetimestruct, | ||
npy_datetimestruct_to_datetime, | ||
pandas_datetime_to_datetimestruct, | ||
pydatetime_to_dt64, | ||
pydatetime_to_dtstruct, | ||
string_to_dts, | ||
) | ||
|
@@ -65,6 +66,7 @@ from pandas._libs.tslibs.nattype cimport ( | |
c_NaT as NaT, | ||
c_nat_strings as nat_strings, | ||
) | ||
from pandas._libs.tslibs.timestamps cimport _Timestamp | ||
from pandas._libs.tslibs.tzconversion cimport ( | ||
Localizer, | ||
tz_localize_to_utc_single, | ||
|
@@ -208,9 +210,10 @@ cdef class _TSObject: | |
self.fold = 0 | ||
self.creso = NPY_FR_ns # default value | ||
|
||
cdef void ensure_reso(self, NPY_DATETIMEUNIT creso): | ||
cdef int64_t ensure_reso(self, NPY_DATETIMEUNIT creso) except? -1: | ||
if self.creso != creso: | ||
self.value = convert_reso(self.value, self.creso, creso, False) | ||
return self.value | ||
|
||
|
||
cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit, | ||
|
@@ -642,3 +645,99 @@ cpdef inline datetime localize_pydatetime(datetime dt, tzinfo tz): | |
elif isinstance(dt, ABCTimestamp): | ||
return dt.tz_localize(tz) | ||
return _localize_pydatetime(dt, tz) | ||
|
||
|
||
cdef tzinfo convert_timezone( | ||
tzinfo tz_in, | ||
tzinfo tz_out, | ||
bint found_naive, | ||
bint found_tz, | ||
bint utc_convert, | ||
): | ||
""" | ||
Validate that ``tz_in`` can be converted/localized to ``tz_out``. | ||
|
||
Parameters | ||
---------- | ||
tz_in : tzinfo | ||
Timezone info of element being processed. | ||
tz_out : tzinfo | ||
Timezone info of output. | ||
found_naive : bool | ||
Whether a timezone-naive element has been found so far. | ||
found_tz : bool | ||
Whether a timezone-aware element has been found so far. | ||
utc_convert : bool | ||
Whether to convert/localize to UTC. | ||
|
||
Returns | ||
------- | ||
tz_info | ||
Timezone info of output. | ||
|
||
Raises | ||
------ | ||
ValueError | ||
If ``tz_in`` can't be converted/localized to ``tz_out``. | ||
""" | ||
if tz_in is not None: | ||
if utc_convert: | ||
pass | ||
elif found_naive: | ||
raise ValueError('Tz-aware datetime.datetime ' | ||
'cannot be converted to ' | ||
'datetime64 unless utc=True') | ||
elif tz_out is not None and not tz_compare(tz_out, tz_in): | ||
raise ValueError('Tz-aware datetime.datetime ' | ||
'cannot be converted to ' | ||
'datetime64 unless utc=True') | ||
else: | ||
tz_out = tz_in | ||
else: | ||
if found_tz and not utc_convert: | ||
raise ValueError('Cannot mix tz-aware with ' | ||
'tz-naive values') | ||
return tz_out | ||
|
||
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. one more newline 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. you're right (and that's another one for cython-lint!) done |
||
|
||
cdef int64_t parse_pydatetime( | ||
object val, | ||
npy_datetimestruct *dts, | ||
bint utc_convert, | ||
) except? -1: | ||
""" | ||
Convert pydatetime to datetime64. | ||
|
||
Parameters | ||
---------- | ||
val | ||
Element being processed. | ||
dts : *npy_datetimestruct | ||
Needed to use in pydatetime_to_dt64, which writes to it. | ||
utc_convert : bool | ||
Whether to convert/localize to UTC. | ||
|
||
Raises | ||
------ | ||
OutOfBoundsDatetime | ||
""" | ||
cdef: | ||
_TSObject _ts | ||
int64_t result | ||
|
||
if val.tzinfo is not None: | ||
if utc_convert: | ||
_ts = convert_datetime_to_tsobject(val, None) | ||
MarcoGorelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ts.ensure_reso(NPY_FR_ns) | ||
result = _ts.value | ||
else: | ||
_ts = convert_datetime_to_tsobject(val, None) | ||
_ts.ensure_reso(NPY_FR_ns) | ||
WillAyd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = _ts.value | ||
else: | ||
if isinstance(val, _Timestamp): | ||
result = val.as_unit("ns").value | ||
else: | ||
result = pydatetime_to_dt64(val, dts) | ||
check_dts_bounds(dts) | ||
return result |
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.
Uh oh!
There was an error while loading. Please reload this page.