Skip to content

REF: add creso keyword to parse_pydatetime #55579

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
Oct 18, 2023
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
3 changes: 2 additions & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ cpdef array_to_datetime(
tzinfo tz_out = None
bint found_tz = False, found_naive = False
cnp.flatiter it = cnp.PyArray_IterNew(values)
NPY_DATETIMEUNIT creso = NPY_FR_ns

# specify error conditions
assert is_raise or is_ignore or is_coerce
Expand Down Expand Up @@ -484,7 +485,7 @@ cpdef array_to_datetime(
found_tz,
utc_convert,
)
iresult[i] = parse_pydatetime(val, &dts, utc_convert)
iresult[i] = parse_pydatetime(val, &dts, utc_convert, creso=creso)

elif PyDate_Check(val):
iresult[i] = pydate_to_dt64(val, &dts)
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/conversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ cdef int64_t parse_pydatetime(
datetime val,
npy_datetimestruct *dts,
bint utc_convert,
NPY_DATETIMEUNIT creso,
) except? -1
22 changes: 12 additions & 10 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ from pandas._libs.tslibs.nattype cimport (
c_nat_strings as nat_strings,
)
from pandas._libs.tslibs.parsing cimport parse_datetime_string
from pandas._libs.tslibs.timestamps cimport _Timestamp
from pandas._libs.tslibs.timezones cimport (
get_utcoffset,
is_utc,
Expand Down Expand Up @@ -302,8 +303,8 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
pandas_datetime_to_datetimestruct(ts, NPY_FR_ns, &obj.dts)
elif PyDateTime_Check(ts):
if nanos == 0:
if isinstance(ts, ABCTimestamp):
reso = abbrev_to_npy_unit(ts.unit) # TODO: faster way to do this?
if isinstance(ts, _Timestamp):
reso = (<_Timestamp>ts)._creso
else:
# TODO: what if user explicitly passes nanos=0?
reso = NPY_FR_us
Expand Down Expand Up @@ -729,6 +730,7 @@ cdef int64_t parse_pydatetime(
datetime val,
npy_datetimestruct *dts,
bint utc_convert,
NPY_DATETIMEUNIT creso,
) except? -1:
"""
Convert pydatetime to datetime64.
Expand All @@ -741,6 +743,8 @@ cdef int64_t parse_pydatetime(
Needed to use in pydatetime_to_dt64, which writes to it.
utc_convert : bool
Whether to convert/localize to UTC.
creso : NPY_DATETIMEUNIT
Resolution to store the the result.

Raises
------
Expand All @@ -752,17 +756,15 @@ cdef int64_t parse_pydatetime(

if val.tzinfo is not None:
if utc_convert:
_ts = convert_datetime_to_tsobject(val, None)
_ts.ensure_reso(NPY_FR_ns)
_ts = convert_datetime_to_tsobject(val, None, nanos=0, reso=creso)
result = _ts.value
else:
_ts = convert_datetime_to_tsobject(val, None)
_ts.ensure_reso(NPY_FR_ns)
_ts = convert_datetime_to_tsobject(val, None, nanos=0, reso=creso)
result = _ts.value
else:
if isinstance(val, ABCTimestamp):
result = val.as_unit("ns")._value
if isinstance(val, _Timestamp):
result = (<_Timestamp>val)._as_creso(creso, round_ok=False)._value
else:
result = pydatetime_to_dt64(val, dts)
check_dts_bounds(dts)
result = pydatetime_to_dt64(val, dts, reso=creso)
check_dts_bounds(dts, creso)
return result