Skip to content

Commit c377e3c

Browse files
authored
REF: fewer simple_new calls in DTA._generate_range GH#24562 (#45293)
1 parent d3c62ad commit c377e3c

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

pandas/core/arrays/datetimes.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -442,53 +442,53 @@ def _generate_range(
442442
end = end.tz_localize(None)
443443

444444
if isinstance(freq, Tick):
445-
values = generate_regular_range(start, end, periods, freq)
445+
i8values = generate_regular_range(start, end, periods, freq)
446446
else:
447447
xdr = generate_range(start=start, end=end, periods=periods, offset=freq)
448-
values = np.array([x.value for x in xdr], dtype=np.int64)
448+
i8values = np.array([x.value for x in xdr], dtype=np.int64)
449449

450-
_tz = start.tz if start is not None else end.tz
451-
values = values.view("M8[ns]")
452-
index = cls._simple_new(values, freq=freq, dtype=tz_to_dtype(_tz))
450+
endpoint_tz = start.tz if start is not None else end.tz
453451

454-
if tz is not None and index.tz is None:
455-
arr = tzconversion.tz_localize_to_utc(
456-
index.asi8, tz, ambiguous=ambiguous, nonexistent=nonexistent
452+
if tz is not None and endpoint_tz is None:
453+
i8values = tzconversion.tz_localize_to_utc(
454+
i8values, tz, ambiguous=ambiguous, nonexistent=nonexistent
457455
)
458456

459-
index = cls(arr)
460-
461-
# index is localized datetime64 array -> have to convert
457+
# i8values is localized datetime64 array -> have to convert
462458
# start/end as well to compare
463459
if start is not None:
464-
start = start.tz_localize(tz, ambiguous, nonexistent).asm8
460+
start = start.tz_localize(tz, ambiguous, nonexistent)
465461
if end is not None:
466-
end = end.tz_localize(tz, ambiguous, nonexistent).asm8
462+
end = end.tz_localize(tz, ambiguous, nonexistent)
467463
else:
468464
# Create a linearly spaced date_range in local time
469465
# Nanosecond-granularity timestamps aren't always correctly
470466
# representable with doubles, so we limit the range that we
471467
# pass to np.linspace as much as possible
472-
arr = (
468+
i8values = (
473469
np.linspace(0, end.value - start.value, periods, dtype="int64")
474470
+ start.value
475471
)
476-
dtype = tz_to_dtype(tz)
477-
arr = arr.astype("M8[ns]", copy=False)
478-
index = cls._simple_new(arr, freq=None, dtype=dtype)
472+
if i8values.dtype != "i8":
473+
# 2022-01-09 I (brock) am not sure if it is possible for this
474+
# to overflow and cast to e.g. f8, but if it does we need to cast
475+
i8values = i8values.astype("i8")
479476

480477
if start == end:
481478
if not left_inclusive and not right_inclusive:
482-
index = index[1:-1]
479+
i8values = i8values[1:-1]
483480
else:
481+
start_i8 = Timestamp(start).value
482+
end_i8 = Timestamp(end).value
484483
if not left_inclusive or not right_inclusive:
485-
if not left_inclusive and len(index) and index[0] == start:
486-
index = index[1:]
487-
if not right_inclusive and len(index) and index[-1] == end:
488-
index = index[:-1]
484+
if not left_inclusive and len(i8values) and i8values[0] == start_i8:
485+
i8values = i8values[1:]
486+
if not right_inclusive and len(i8values) and i8values[-1] == end_i8:
487+
i8values = i8values[:-1]
489488

489+
dt64_values = i8values.view("datetime64[ns]")
490490
dtype = tz_to_dtype(tz)
491-
return cls._simple_new(index._ndarray, freq=freq, dtype=dtype)
491+
return cls._simple_new(dt64_values, freq=freq, dtype=dtype)
492492

493493
# -----------------------------------------------------------------
494494
# DatetimeLike Interface

0 commit comments

Comments
 (0)