Skip to content

Automatic PR for 4c0bd319-0495-4286-8ef5-142a0d141c73 #87

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

Open
wants to merge 1 commit into
base: 4c0bd319-0495-4286-8ef5-142a0d141c73-base
Choose a base branch
from
Open
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
28 changes: 16 additions & 12 deletions pandas/core/arrays/_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ def generate_regular_range(
iend = end._value if end is not None else None
freq.nanos # raises if non-fixed frequency
td = Timedelta(freq)
b: int | np.int64 | np.uint64
e: int | np.int64 | np.uint64
try:
td = td.as_unit( # pyright: ignore[reportGeneralTypeIssues]
unit, round_ok=False
Expand Down Expand Up @@ -98,7 +96,7 @@ def generate_regular_range(

def _generate_range_overflow_safe(
endpoint: int, periods: int, stride: int, side: str = "start"
) -> np.int64 | np.uint64:
) -> int:
"""
Calculate the second endpoint for passing to np.arange, checking
to avoid an integer overflow. Catch OverflowError and re-raise
Expand All @@ -117,7 +115,7 @@ def _generate_range_overflow_safe(

Returns
-------
other_end : np.int64 | np.uint64
other_end : int

Raises
------
Expand Down Expand Up @@ -159,13 +157,13 @@ def _generate_range_overflow_safe(
remaining = periods - mid_periods
assert 0 < remaining < periods, (remaining, periods, endpoint, stride)

midpoint = int(_generate_range_overflow_safe(endpoint, mid_periods, stride, side))
midpoint = _generate_range_overflow_safe(endpoint, mid_periods, stride, side)
return _generate_range_overflow_safe(midpoint, remaining, stride, side)


def _generate_range_overflow_safe_signed(
endpoint: int, periods: int, stride: int, side: str
) -> np.int64 | np.uint64:
) -> int:
"""
A special case for _generate_range_overflow_safe where `periods * stride`
can be calculated without overflowing int64 bounds.
Expand All @@ -183,7 +181,9 @@ def _generate_range_overflow_safe_signed(
# Putting this into a DatetimeArray/TimedeltaArray
# would incorrectly be interpreted as NaT
raise OverflowError
return result
# error: Incompatible return value type (got "signedinteger[_64Bit]",
# expected "int")
return result # type: ignore[return-value]
except (FloatingPointError, OverflowError):
# with endpoint negative and addend positive we risk
# FloatingPointError; with reversed signed we risk OverflowError
Expand All @@ -198,12 +198,16 @@ def _generate_range_overflow_safe_signed(
# exceed implementation bounds, but when passing the result to
# np.arange will get a result slightly within the bounds

uresult = np.uint64(endpoint) + np.uint64(addend)
# error: Incompatible types in assignment (expression has type
# "unsignedinteger[_64Bit]", variable has type "signedinteger[_64Bit]")
result = np.uint64(endpoint) + np.uint64(addend) # type: ignore[assignment]
i64max = np.uint64(i8max)
assert uresult > i64max
if uresult <= i64max + np.uint64(stride):
return uresult
assert result > i64max
if result <= i64max + np.uint64(stride):
# error: Incompatible return value type (got "unsignedinteger", expected
# "int")
return result # type: ignore[return-value]

raise OutOfBoundsDatetime(
f"Cannot generate range with {side}={endpoint} and periods={periods}"
)
)