Skip to content

Commit 9a40316

Browse files
authored
REF: simplify objects_to_datetime64ns (#55950)
* REF: simplify objects_to_datetime64ns * revert annotations
1 parent 76d28c7 commit 9a40316

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

pandas/core/arrays/datetimes.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -2250,9 +2250,7 @@ def _sequence_to_dt64(
22502250
)
22512251
return result, tz, None
22522252
else:
2253-
# data comes back here as either i8 to denote UTC timestamps
2254-
# or M8[ns] to denote wall times
2255-
converted, inferred_tz = objects_to_datetime64ns(
2253+
converted, inferred_tz = objects_to_datetime64(
22562254
data,
22572255
dayfirst=dayfirst,
22582256
yearfirst=yearfirst,
@@ -2262,13 +2260,13 @@ def _sequence_to_dt64(
22622260
copy = False
22632261
if tz and inferred_tz:
22642262
# two timezones: convert to intended from base UTC repr
2265-
assert converted.dtype == "i8"
2266-
# GH#42505
2267-
# by convention, these are _already_ UTC, e.g
2263+
# GH#42505 by convention, these are _already_ UTC
2264+
assert converted.dtype == out_dtype, converted.dtype
22682265
result = converted.view(out_dtype)
22692266

22702267
elif inferred_tz:
22712268
tz = inferred_tz
2269+
assert converted.dtype == out_dtype, converted.dtype
22722270
result = converted.view(out_dtype)
22732271

22742272
else:
@@ -2360,7 +2358,7 @@ def _construct_from_dt64_naive(
23602358
return result, copy
23612359

23622360

2363-
def objects_to_datetime64ns(
2361+
def objects_to_datetime64(
23642362
data: np.ndarray,
23652363
dayfirst,
23662364
yearfirst,
@@ -2388,10 +2386,11 @@ def objects_to_datetime64ns(
23882386
Returns
23892387
-------
23902388
result : ndarray
2391-
np.int64 dtype if returned values represent UTC timestamps
2392-
np.datetime64[ns] if returned values represent wall times
2389+
np.datetime64[out_unit] if returned values represent wall times or UTC
2390+
timestamps.
23932391
object if mixed timezones
23942392
inferred_tz : tzinfo or None
2393+
If not None, then the datetime64 values in `result` denote UTC timestamps.
23952394
23962395
Raises
23972396
------
@@ -2414,11 +2413,8 @@ def objects_to_datetime64ns(
24142413
if tz_parsed is not None:
24152414
# We can take a shortcut since the datetime64 numpy array
24162415
# is in UTC
2417-
# Return i8 values to denote unix timestamps
2418-
return result.view("i8"), tz_parsed
2416+
return result, tz_parsed
24192417
elif result.dtype.kind == "M":
2420-
# returning M8[ns] denotes wall-times; since tz is None
2421-
# the distinction is a thin one
24222418
return result, tz_parsed
24232419
elif result.dtype == object:
24242420
# GH#23675 when called via `pd.to_datetime`, returning an object-dtype

pandas/core/tools/datetimes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
from pandas.core.arrays.base import ExtensionArray
7272
from pandas.core.arrays.datetimes import (
7373
maybe_convert_dtype,
74-
objects_to_datetime64ns,
74+
objects_to_datetime64,
7575
tz_to_dtype,
7676
)
7777
from pandas.core.construction import extract_array
@@ -485,7 +485,7 @@ def _convert_listlike_datetimes(
485485
if format is not None and format != "mixed":
486486
return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)
487487

488-
result, tz_parsed = objects_to_datetime64ns(
488+
result, tz_parsed = objects_to_datetime64(
489489
arg,
490490
dayfirst=dayfirst,
491491
yearfirst=yearfirst,
@@ -499,7 +499,7 @@ def _convert_listlike_datetimes(
499499
# is in UTC
500500
dtype = cast(DatetimeTZDtype, tz_to_dtype(tz_parsed))
501501
dt64_values = result.view(f"M8[{dtype.unit}]")
502-
dta = DatetimeArray(dt64_values, dtype=dtype)
502+
dta = DatetimeArray._simple_new(dt64_values, dtype=dtype)
503503
return DatetimeIndex._simple_new(dta, name=name)
504504

505505
return _box_as_indexlike(result, utc=utc, name=name)

0 commit comments

Comments
 (0)