Skip to content
Open
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
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
infer_dtype_from_scalar,
invalidate_string_dtypes,
maybe_downcast_to_dtype,
maybe_unbox_numpy_scalar,
)
from pandas.core.dtypes.common import (
infer_dtype_from_object,
Expand Down Expand Up @@ -12169,7 +12170,7 @@ def _get_data() -> DataFrame:
df = df.astype(dtype)
arr = concat_compat(list(df._iter_column_arrays()))
return arr._reduce(name, skipna=skipna, keepdims=False, **kwds)
return func(df.values)
return maybe_unbox_numpy_scalar(func(df.values))
elif axis == 1:
if len(df.index) == 0:
# Taking a transpose would result in no columns, losing the dtype.
Expand Down
12 changes: 7 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,10 +962,10 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
return tuple(self.__array_wrap__(x) for x in result)
elif method == "reduce":
result = lib.item_from_zerodim(result)
return result
return maybe_unbox_numpy_scalar(result)
elif is_scalar(result):
# e.g. matmul
return result
return maybe_unbox_numpy_scalar(result)

if result.dtype == np.float16:
result = result.astype(np.float32)
Expand Down Expand Up @@ -6815,7 +6815,7 @@ def _searchsorted_monotonic(self, label, side: Literal["left", "right"] = "left"
pos = self[::-1].searchsorted(
label, side="right" if side == "left" else "left"
)
return len(self) - pos
return maybe_unbox_numpy_scalar(len(self) - pos)

raise ValueError("index must be monotonic increasing or decreasing")

Expand Down Expand Up @@ -7005,6 +7005,8 @@ def slice_locs(
if start_slice == -1:
start_slice -= len(self)

start_slice = maybe_unbox_numpy_scalar(start_slice)
end_slice = maybe_unbox_numpy_scalar(end_slice)
return start_slice, end_slice

def delete(
Expand Down Expand Up @@ -7412,7 +7414,7 @@ def any(self, *args, **kwargs):
# i.e. EA, call _reduce instead of "any" to get TypeError instead
# of AttributeError
return vals._reduce("any")
return np.any(vals)
return maybe_unbox_numpy_scalar(np.any(vals))

def all(self, *args, **kwargs):
"""
Expand Down Expand Up @@ -7460,7 +7462,7 @@ def all(self, *args, **kwargs):
# i.e. EA, call _reduce instead of "all" to get TypeError instead
# of AttributeError
return vals._reduce("all")
return np.all(vals)
return maybe_unbox_numpy_scalar(np.all(vals))

@final
def _maybe_disable_logical_methods(self, opname: str_t) -> None:
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
infer_dtype_from_scalar,
maybe_box_datetimelike,
maybe_downcast_numeric,
maybe_unbox_numpy_scalar,
maybe_upcast_numeric_to_64bit,
)
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -812,7 +813,7 @@ def get_loc(self, key) -> int | slice | np.ndarray:
if matches == 0:
raise KeyError(key)
if matches == 1:
return mask.argmax()
return maybe_unbox_numpy_scalar(mask.argmax())

res = lib.maybe_booleans_to_slice(mask.view("u1"))
if isinstance(res, slice) and res.stop is None:
Expand Down
11 changes: 8 additions & 3 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
)
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.cast import coerce_indexer_dtype
from pandas.core.dtypes.cast import (
coerce_indexer_dtype,
maybe_unbox_numpy_scalar,
)
from pandas.core.dtypes.common import (
ensure_int64,
ensure_platform_int,
Expand Down Expand Up @@ -3130,7 +3133,9 @@ def get_slice_bound(
"""
if not isinstance(label, tuple):
label = (label,)
return self._partial_tup_index(label, side=side)
result = self._partial_tup_index(label, side=side)
result = maybe_unbox_numpy_scalar(result)
return result

def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
"""
Expand Down Expand Up @@ -3717,7 +3722,7 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes):
if start == end:
# The label is present in self.levels[level] but unused:
raise KeyError(key)
return slice(start, end)
return slice(maybe_unbox_numpy_scalar(start), maybe_unbox_numpy_scalar(end))

def get_locs(self, seq) -> npt.NDArray[np.intp]:
"""
Expand Down
17 changes: 11 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,7 @@ def quantile(
return self._constructor(result, index=idx, name=self.name)
else:
# scalar
return result.iloc[0]
return maybe_unbox_numpy_scalar(result.iloc[0])

def corr(
self,
Expand Down Expand Up @@ -2754,9 +2754,11 @@ def corr(
other_values = other.to_numpy(dtype=float, na_value=np.nan, copy=False)

if method in ["pearson", "spearman", "kendall"] or callable(method):
return nanops.nancorr(
result = nanops.nancorr(
this_values, other_values, method=method, min_periods=min_periods
)
result = maybe_unbox_numpy_scalar(result)
return result

raise ValueError(
"method must be either 'pearson', "
Expand Down Expand Up @@ -2808,9 +2810,11 @@ def cov(
return np.nan
this_values = this.to_numpy(dtype=float, na_value=np.nan, copy=False)
other_values = other.to_numpy(dtype=float, na_value=np.nan, copy=False)
return nanops.nancov(
result = nanops.nancov(
this_values, other_values, min_periods=min_periods, ddof=ddof
)
result = maybe_unbox_numpy_scalar(result)
return result

@doc(
klass="Series",
Expand Down Expand Up @@ -3023,11 +3027,12 @@ def dot(self, other: AnyArrayLike | DataFrame) -> Series | np.ndarray:
np.dot(lvals, rvals), index=other.columns, copy=False, dtype=common_type
).__finalize__(self, method="dot")
elif isinstance(other, Series):
return np.dot(lvals, rvals)
result = np.dot(lvals, rvals)
elif isinstance(rvals, np.ndarray):
return np.dot(lvals, rvals)
result = np.dot(lvals, rvals)
else: # pragma: no cover
raise TypeError(f"unsupported type: {type(other)}")
return maybe_unbox_numpy_scalar(result)

def __matmul__(self, other):
"""
Expand Down Expand Up @@ -5701,7 +5706,7 @@ def pop(self, item: Hashable) -> Any:
2 3
dtype: int64
"""
return super().pop(item=item)
return maybe_unbox_numpy_scalar(super().pop(item=item))

def info(
self,
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/series/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,13 @@ def test_np_matmul():


@pytest.mark.parametrize("box", [pd.Index, pd.Series])
def test_np_matmul_1D(box):
def test_np_matmul_1D(box, using_python_scalars):
result = np.matmul(box([1, 2]), box([2, 3]))
assert result == 8
assert isinstance(result, np.int64)
if using_python_scalars:
assert type(result) == int, type(result)
else:
assert type(result) == np.int64, type(result)


def test_array_ufuncs_for_many_arguments():
Expand Down
Loading