Skip to content

Commit 45361a4

Browse files
authored
CLN: generate_range (#56416)
1 parent c2f0659 commit 45361a4

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

pandas/core/arrays/datetimelike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,9 @@ def _validate_frequency(cls, index, freq: BaseOffset, **kwargs):
21032103
) from err
21042104

21052105
@classmethod
2106-
def _generate_range(cls, start, end, periods, freq, *args, **kwargs) -> Self:
2106+
def _generate_range(
2107+
cls, start, end, periods: int | None, freq, *args, **kwargs
2108+
) -> Self:
21072109
raise AbstractMethodError(cls)
21082110

21092111
# --------------------------------------------------------------

pandas/core/arrays/datetimes.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def _generate_range( # type: ignore[override]
405405
cls,
406406
start,
407407
end,
408-
periods,
408+
periods: int | None,
409409
freq,
410410
tz=None,
411411
normalize: bool = False,
@@ -441,9 +441,9 @@ def _generate_range( # type: ignore[override]
441441
else:
442442
unit = "ns"
443443

444-
if start is not None and unit is not None:
444+
if start is not None:
445445
start = start.as_unit(unit, round_ok=False)
446-
if end is not None and unit is not None:
446+
if end is not None:
447447
end = end.as_unit(unit, round_ok=False)
448448

449449
left_inclusive, right_inclusive = validate_inclusive(inclusive)
@@ -452,14 +452,8 @@ def _generate_range( # type: ignore[override]
452452

453453
if tz is not None:
454454
# Localize the start and end arguments
455-
start_tz = None if start is None else start.tz
456-
end_tz = None if end is None else end.tz
457-
start = _maybe_localize_point(
458-
start, start_tz, start, freq, tz, ambiguous, nonexistent
459-
)
460-
end = _maybe_localize_point(
461-
end, end_tz, end, freq, tz, ambiguous, nonexistent
462-
)
455+
start = _maybe_localize_point(start, freq, tz, ambiguous, nonexistent)
456+
end = _maybe_localize_point(end, freq, tz, ambiguous, nonexistent)
463457

464458
if freq is not None:
465459
# We break Day arithmetic (fixed 24 hour) here and opt for
@@ -505,6 +499,7 @@ def _generate_range( # type: ignore[override]
505499
# Nanosecond-granularity timestamps aren't always correctly
506500
# representable with doubles, so we limit the range that we
507501
# pass to np.linspace as much as possible
502+
periods = cast(int, periods)
508503
i8values = (
509504
np.linspace(0, end._value - start._value, periods, dtype="int64")
510505
+ start._value
@@ -2688,16 +2683,16 @@ def _maybe_normalize_endpoints(
26882683
return start, end
26892684

26902685

2691-
def _maybe_localize_point(ts, is_none, is_not_none, freq, tz, ambiguous, nonexistent):
2686+
def _maybe_localize_point(
2687+
ts: Timestamp | None, freq, tz, ambiguous, nonexistent
2688+
) -> Timestamp | None:
26922689
"""
26932690
Localize a start or end Timestamp to the timezone of the corresponding
26942691
start or end Timestamp
26952692
26962693
Parameters
26972694
----------
26982695
ts : start or end Timestamp to potentially localize
2699-
is_none : argument that should be None
2700-
is_not_none : argument that should not be None
27012696
freq : Tick, DateOffset, or None
27022697
tz : str, timezone object or None
27032698
ambiguous: str, localization behavior for ambiguous times
@@ -2710,7 +2705,7 @@ def _maybe_localize_point(ts, is_none, is_not_none, freq, tz, ambiguous, nonexis
27102705
# Make sure start and end are timezone localized if:
27112706
# 1) freq = a Timedelta-like frequency (Tick)
27122707
# 2) freq = None i.e. generating a linspaced range
2713-
if is_none is None and is_not_none is not None:
2708+
if ts is not None and ts.tzinfo is None:
27142709
# Note: We can't ambiguous='infer' a singular ambiguous time; however,
27152710
# we have historically defaulted ambiguous=False
27162711
ambiguous = ambiguous if ambiguous != "infer" else False

0 commit comments

Comments
 (0)