Skip to content

REF: do masking in checked_add_with_arr #47396

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
Jun 21, 2022
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
11 changes: 8 additions & 3 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,10 +1017,10 @@ def rank(

def checked_add_with_arr(
arr: npt.NDArray[np.int64],
b,
b: int | npt.NDArray[np.int64],
arr_mask: npt.NDArray[np.bool_] | None = None,
b_mask: npt.NDArray[np.bool_] | None = None,
) -> np.ndarray:
) -> npt.NDArray[np.int64]:
"""
Perform array addition that checks for underflow and overflow.

Expand Down Expand Up @@ -1098,7 +1098,12 @@ def checked_add_with_arr(

if to_raise:
raise OverflowError("Overflow in int64 addition")
return arr + b

result = arr + b
if arr_mask is not None or b2_mask is not None:
np.putmask(result, ~not_nan, iNaT)

return result


# --------------- #
Expand Down
34 changes: 15 additions & 19 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,8 +1104,12 @@ def _add_datetimelike_scalar(self, other):
return DatetimeArray(result)

i8 = self.asi8
result = checked_add_with_arr(i8, other.value, arr_mask=self._isnan)
result = self._maybe_mask_results(result)
# Incompatible types in assignment (expression has type "ndarray[Any,
# dtype[signedinteger[_64Bit]]]", variable has type
# "ndarray[Any, dtype[datetime64]]")
result = checked_add_with_arr( # type: ignore[assignment]
i8, other.value, arr_mask=self._isnan
)
dtype = DatetimeTZDtype(tz=other.tz) if other.tz else DT64NS_DTYPE
return DatetimeArray(result, dtype=dtype, freq=self.freq)

Expand Down Expand Up @@ -1146,7 +1150,6 @@ def _sub_datetimelike_scalar(self, other: datetime | np.datetime64):

i8 = self.asi8
result = checked_add_with_arr(i8, -other.value, arr_mask=self._isnan)
result = self._maybe_mask_results(result)
return result.view("timedelta64[ns]")

@final
Expand All @@ -1168,31 +1171,30 @@ def _sub_datetime_arraylike(self, other):

self_i8 = self.asi8
other_i8 = other.asi8
arr_mask = self._isnan | other._isnan
new_values = checked_add_with_arr(self_i8, -other_i8, arr_mask=arr_mask)
if self._hasna or other._hasna:
np.putmask(new_values, arr_mask, iNaT)
new_values = checked_add_with_arr(
self_i8, -other_i8, arr_mask=self._isnan, b_mask=other._isnan
)
return new_values.view("timedelta64[ns]")

@final
def _sub_period(self, other: Period):
def _sub_period(self, other: Period) -> npt.NDArray[np.object_]:
if not is_period_dtype(self.dtype):
raise TypeError(f"cannot subtract Period from a {type(self).__name__}")

# If the operation is well-defined, we return an object-dtype ndarray
# of DateOffsets. Null entries are filled with pd.NaT
self._check_compatible_with(other)
asi8 = self.asi8
new_data = asi8 - other.ordinal
new_data = np.array([self.freq.base * x for x in new_data])
new_i8_data = asi8 - other.ordinal # TODO: checked_add_with_arr
new_data = np.array([self.freq.base * x for x in new_i8_data])

if self._hasna:
new_data[self._isnan] = NaT

return new_data

@final
def _add_period(self, other: Period):
def _add_period(self, other: Period) -> PeriodArray:
if not is_timedelta64_dtype(self.dtype):
raise TypeError(f"cannot add Period to a {type(self).__name__}")

Expand Down Expand Up @@ -1225,8 +1227,6 @@ def _add_timedeltalike_scalar(self, other):
inc = delta_to_nanoseconds(other, reso=self._reso) # type: ignore[attr-defined]

new_values = checked_add_with_arr(self.asi8, inc, arr_mask=self._isnan)
new_values = new_values.view("i8")
new_values = self._maybe_mask_results(new_values)
new_values = new_values.view(self._ndarray.dtype)

new_freq = None
Expand Down Expand Up @@ -1262,10 +1262,6 @@ def _add_timedelta_arraylike(
new_values = checked_add_with_arr(
self_i8, other_i8, arr_mask=self._isnan, b_mask=other._isnan
)
if self._hasna or other._hasna:
mask = self._isnan | other._isnan
np.putmask(new_values, mask, iNaT)

return type(self)(new_values, dtype=self.dtype)

@final
Expand Down Expand Up @@ -1309,11 +1305,11 @@ def _sub_period_array(self, other: PeriodArray) -> npt.NDArray[np.object_]:
self = cast("PeriodArray", self)
self._require_matching_freq(other)

new_values = checked_add_with_arr(
new_i8_values = checked_add_with_arr(
self.asi8, -other.asi8, arr_mask=self._isnan, b_mask=other._isnan
)

new_values = np.array([self.freq.base * x for x in new_values])
new_values = np.array([self.freq.base * x for x in new_i8_values])
if self._hasna or other._hasna:
mask = self._isnan | other._isnan
new_values[mask] = NaT
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,6 @@ def _addsub_int_array_or_scalar(
if op is operator.sub:
other = -other
res_values = algos.checked_add_with_arr(self.asi8, other, arr_mask=self._isnan)
res_values = res_values.view("i8")
np.putmask(res_values, self._isnan, iNaT)
return type(self)(res_values, freq=self.freq)

def _add_offset(self, other: BaseOffset):
Expand Down