-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
ENH: infer Timestamp(iso8601string) resolution #49737
Changes from all commits
27c442a
d559ffd
e88f475
b7f40a5
cc9178d
0d4d03c
dd58c3f
82fa494
9bc302d
00d83e1
8956004
a5d9504
32570ea
954c71c
a78f16d
d744044
11c98d2
f490db5
eaea344
8d45eba
d7f8696
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -405,7 +405,8 @@ cdef _TSObject convert_datetime_to_tsobject( | |
|
||
|
||
cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts, | ||
int tzoffset, tzinfo tz=None): | ||
int tzoffset, tzinfo tz=None, | ||
NPY_DATETIMEUNIT reso=NPY_FR_ns): | ||
""" | ||
Convert a datetimestruct `dts`, along with initial timezone offset | ||
`tzoffset` to a _TSObject (with timezone object `tz` - optional). | ||
|
@@ -416,6 +417,7 @@ cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts, | |
tzoffset: int | ||
tz : tzinfo or None | ||
timezone for the timezone-aware output. | ||
reso : NPY_DATETIMEUNIT, default NPY_FR_ns | ||
|
||
Returns | ||
------- | ||
|
@@ -427,16 +429,19 @@ cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts, | |
datetime dt | ||
Py_ssize_t pos | ||
|
||
value = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts) | ||
value = npy_datetimestruct_to_datetime(reso, &dts) | ||
obj.dts = dts | ||
obj.tzinfo = timezone(timedelta(minutes=tzoffset)) | ||
obj.value = tz_localize_to_utc_single(value, obj.tzinfo) | ||
obj.value = tz_localize_to_utc_single( | ||
value, obj.tzinfo, ambiguous=None, nonexistent=None, creso=reso | ||
) | ||
obj.creso = reso | ||
if tz is None: | ||
check_overflows(obj, NPY_FR_ns) | ||
check_overflows(obj, reso) | ||
return obj | ||
|
||
cdef: | ||
Localizer info = Localizer(tz, NPY_FR_ns) | ||
Localizer info = Localizer(tz, reso) | ||
|
||
# Infer fold from offset-adjusted obj.value | ||
# see PEP 495 https://www.python.org/dev/peps/pep-0495/#the-fold-attribute | ||
|
@@ -454,6 +459,7 @@ cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts, | |
obj.dts.us, obj.tzinfo, fold=obj.fold) | ||
obj = convert_datetime_to_tsobject( | ||
dt, tz, nanos=obj.dts.ps // 1000) | ||
obj.ensure_reso(reso) # TODO: more performant to get reso right up front? | ||
return obj | ||
|
||
|
||
|
@@ -490,7 +496,7 @@ cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, str unit, | |
int out_local = 0, out_tzoffset = 0, string_to_dts_failed | ||
datetime dt | ||
int64_t ival | ||
NPY_DATETIMEUNIT out_bestunit | ||
NPY_DATETIMEUNIT out_bestunit, reso | ||
|
||
if len(ts) == 0 or ts in nat_strings: | ||
ts = NaT | ||
|
@@ -513,19 +519,26 @@ cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, str unit, | |
&out_tzoffset, False | ||
) | ||
if not string_to_dts_failed: | ||
reso = get_supported_reso(out_bestunit) | ||
try: | ||
check_dts_bounds(&dts, NPY_FR_ns) | ||
check_dts_bounds(&dts, reso) | ||
if out_local == 1: | ||
return _create_tsobject_tz_using_offset(dts, | ||
out_tzoffset, tz) | ||
return _create_tsobject_tz_using_offset( | ||
dts, out_tzoffset, tz, reso | ||
) | ||
else: | ||
ival = npy_datetimestruct_to_datetime(NPY_FR_ns, &dts) | ||
ival = npy_datetimestruct_to_datetime(reso, &dts) | ||
if tz is not None: | ||
# shift for _localize_tso | ||
ival = tz_localize_to_utc_single(ival, tz, | ||
ambiguous="raise") | ||
|
||
return convert_to_tsobject(ival, tz, None, False, False) | ||
ival = tz_localize_to_utc_single( | ||
ival, tz, ambiguous="raise", nonexistent=None, creso=reso | ||
) | ||
obj = _TSObject() | ||
obj.dts = dts | ||
obj.value = ival | ||
obj.creso = reso | ||
maybe_localize_tso(obj, tz, obj.creso) | ||
return obj | ||
Comment on lines
-528
to
+541
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. is this because 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. some combination of that and me not wanting to recurse. honestly i dont remember how much of each off the top of my head |
||
|
||
except OutOfBoundsDatetime: | ||
# GH#19382 for just-barely-OutOfBounds falling back to dateutil | ||
|
@@ -538,10 +551,12 @@ cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, str unit, | |
pass | ||
|
||
try: | ||
dt = parse_datetime_string(ts, dayfirst=dayfirst, | ||
yearfirst=yearfirst) | ||
except (ValueError, OverflowError): | ||
raise ValueError("could not convert string to Timestamp") | ||
# TODO: use the one that returns reso | ||
dt = parse_datetime_string( | ||
ts, dayfirst=dayfirst, yearfirst=yearfirst | ||
) | ||
except (ValueError, OverflowError) as err: | ||
raise ValueError("could not convert string to Timestamp") from err | ||
|
||
return convert_datetime_to_tsobject(dt, tz) | ||
|
||
|
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.
this is for use by
ensure_reso
?