Skip to content

REF: simplify objects_to_datetime64ns #55950

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 2 commits into from
Nov 14, 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
22 changes: 9 additions & 13 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2250,9 +2250,7 @@ def _sequence_to_dt64(
)
return result, tz, None
else:
# data comes back here as either i8 to denote UTC timestamps
# or M8[ns] to denote wall times
converted, inferred_tz = objects_to_datetime64ns(
converted, inferred_tz = objects_to_datetime64(
data,
dayfirst=dayfirst,
yearfirst=yearfirst,
Expand All @@ -2262,13 +2260,13 @@ def _sequence_to_dt64(
copy = False
if tz and inferred_tz:
# two timezones: convert to intended from base UTC repr
assert converted.dtype == "i8"
# GH#42505
# by convention, these are _already_ UTC, e.g
# GH#42505 by convention, these are _already_ UTC
assert converted.dtype == out_dtype, converted.dtype
result = converted.view(out_dtype)

elif inferred_tz:
tz = inferred_tz
assert converted.dtype == out_dtype, converted.dtype
result = converted.view(out_dtype)

else:
Expand Down Expand Up @@ -2360,7 +2358,7 @@ def _construct_from_dt64_naive(
return result, copy


def objects_to_datetime64ns(
def objects_to_datetime64(
data: np.ndarray,
dayfirst,
yearfirst,
Expand Down Expand Up @@ -2388,10 +2386,11 @@ def objects_to_datetime64ns(
Returns
-------
result : ndarray
np.int64 dtype if returned values represent UTC timestamps
np.datetime64[ns] if returned values represent wall times
np.datetime64[out_unit] if returned values represent wall times or UTC
timestamps.
object if mixed timezones
inferred_tz : tzinfo or None
If not None, then the datetime64 values in `result` denote UTC timestamps.

Raises
------
Expand All @@ -2414,11 +2413,8 @@ def objects_to_datetime64ns(
if tz_parsed is not None:
# We can take a shortcut since the datetime64 numpy array
# is in UTC
# Return i8 values to denote unix timestamps
return result.view("i8"), tz_parsed
return result, tz_parsed
elif result.dtype.kind == "M":
# returning M8[ns] denotes wall-times; since tz is None
# the distinction is a thin one
return result, tz_parsed
elif result.dtype == object:
# GH#23675 when called via `pd.to_datetime`, returning an object-dtype
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
from pandas.core.arrays.base import ExtensionArray
from pandas.core.arrays.datetimes import (
maybe_convert_dtype,
objects_to_datetime64ns,
objects_to_datetime64,
tz_to_dtype,
)
from pandas.core.construction import extract_array
Expand Down Expand Up @@ -485,7 +485,7 @@ def _convert_listlike_datetimes(
if format is not None and format != "mixed":
return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)

result, tz_parsed = objects_to_datetime64ns(
result, tz_parsed = objects_to_datetime64(
arg,
dayfirst=dayfirst,
yearfirst=yearfirst,
Expand All @@ -499,7 +499,7 @@ def _convert_listlike_datetimes(
# is in UTC
dtype = cast(DatetimeTZDtype, tz_to_dtype(tz_parsed))
dt64_values = result.view(f"M8[{dtype.unit}]")
dta = DatetimeArray(dt64_values, dtype=dtype)
dta = DatetimeArray._simple_new(dt64_values, dtype=dtype)
return DatetimeIndex._simple_new(dta, name=name)

return _box_as_indexlike(result, utc=utc, name=name)
Expand Down