Skip to content

CLN: Assorted #50433

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 13 commits into from
Dec 29, 2022
3 changes: 1 addition & 2 deletions pandas/_libs/intervaltree.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ cdef class IntervalTree(IntervalMixin):
"""
if self._na_count > 0:
return False
values = [self.right, self.left]

sort_order = np.lexsort(values)
sort_order = self.left_sorter
return is_monotonic(sort_order, False)[0]

def get_indexer(self, scalar_t[:] target) -> np.ndarray:
Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/tslibs/offsets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ from typing import (

import numpy as np

from pandas._libs.tslibs.nattype import NaTType
from pandas._typing import npt

from .timedeltas import Timedelta
Expand Down Expand Up @@ -51,6 +52,8 @@ class BaseOffset:
def __radd__(self, other: _DatetimeT) -> _DatetimeT: ...
@overload
def __radd__(self, other: _TimedeltaT) -> _TimedeltaT: ...
@overload
def __radd__(self, other: NaTType) -> NaTType: ...
def __sub__(self: _BaseOffsetT, other: BaseOffset) -> _BaseOffsetT: ...
@overload
def __rsub__(self, other: npt.NDArray[np.object_]) -> npt.NDArray[np.object_]: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ cdef inline bint is_integer_object(object obj) nogil:

cdef inline bint is_float_object(object obj) nogil:
"""
Cython equivalent of `isinstance(val, (float, np.complex_))`
Cython equivalent of `isinstance(val, (float, np.float_))`

Parameters
----------
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
from __future__ import annotations

from typing import final
import warnings

from pandas.util._decorators import doc
Expand All @@ -16,6 +17,7 @@ class DirNamesMixin:
_accessors: set[str] = set()
_hidden_attrs: frozenset[str] = frozenset()

@final
def _dir_deletions(self) -> set[str]:
"""
Delete unwanted __dir__ for this object.
Expand Down
22 changes: 9 additions & 13 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
Shared Base/Mixin class for DatetimeArray, TimedeltaArray, PeriodArray

Assumes that __new__/__init__ defines:
_data
_ndarray
_freq

and that the inheriting class has methods:
Expand Down Expand Up @@ -1418,9 +1418,8 @@ def __add__(self, other):
# as is_integer returns True for these
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other * self.freq.n, operator.add
)
obj = cast("PeriodArray", self)
result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.add)

# array-like others
elif is_timedelta64_dtype(other_dtype):
Expand All @@ -1435,9 +1434,8 @@ def __add__(self, other):
elif is_integer_dtype(other_dtype):
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other * self.freq.n, operator.add
)
obj = cast("PeriodArray", self)
result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.add)
else:
# Includes Categorical, other ExtensionArrays
# For PeriodDtype, if self is a TimedeltaArray and other is a
Expand Down Expand Up @@ -1477,9 +1475,8 @@ def __sub__(self, other):
# as is_integer returns True for these
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other * self.freq.n, operator.sub
)
obj = cast("PeriodArray", self)
result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.sub)

elif isinstance(other, Period):
result = self._sub_periodlike(other)
Expand All @@ -1500,9 +1497,8 @@ def __sub__(self, other):
elif is_integer_dtype(other_dtype):
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other * self.freq.n, operator.sub
)
obj = cast("PeriodArray", self)
result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.sub)
else:
# Includes ExtensionArrays, float_dtype
return NotImplemented
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ def argsort(
ascending = nv.validate_argsort_with_ascending(ascending, (), kwargs)

if ascending and kind == "quicksort" and na_position == "last":
# TODO: in an IntervalIndex we can re-use the cached
# IntervalTree.left_sorter
return np.lexsort((self.right, self.left))

# TODO: other cases we can use lexsort for? much more performant.
Expand Down
10 changes: 2 additions & 8 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class providing the base-class of operations.
cast,
final,
)
import warnings

import numpy as np

Expand Down Expand Up @@ -2200,13 +2199,8 @@ def sem(self, ddof: int = 1, numeric_only: bool = False):
counts = self.count()
result_ilocs = result.columns.get_indexer_for(cols)
count_ilocs = counts.columns.get_indexer_for(cols)
with warnings.catch_warnings():
# TODO(2.0): once iloc[:, foo] = bar depecation is enforced,
# this catching will be unnecessary
warnings.filterwarnings(
"ignore", ".*will attempt to set the values inplace.*"
)
result.iloc[:, result_ilocs] /= np.sqrt(counts.iloc[:, count_ilocs])

result.iloc[:, result_ilocs] /= np.sqrt(counts.iloc[:, count_ilocs])
return result

@final
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,6 @@ def column_setitem(

This is a method on the ArrayManager level, to avoid creating an
intermediate Series at the DataFrame level (`s = df[loc]; s[idx] = value`)


"""
if not is_integer(loc):
raise TypeError("The column index should be an integer")
Expand Down
18 changes: 4 additions & 14 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ def apply(self, func, **kwargs) -> list[Block]:

return self._split_op_result(result)

@final
def reduce(self, func) -> list[Block]:
# We will apply the function and reshape the result into a single-row
# Block with the same mgr_locs; squeezing will be done at a higher level
Expand Down Expand Up @@ -1957,19 +1958,6 @@ class ObjectBlock(NumpyBlock):
__slots__ = ()
is_object = True

def reduce(self, func) -> list[Block]:
"""
For object-dtype, we operate column-wise.
"""
assert self.ndim == 2

res = func(self.values)

assert isinstance(res, np.ndarray)
assert res.ndim == 1
res = res.reshape(-1, 1)
return [self.make_block_same_class(res)]

@maybe_split
def convert(
self,
Expand All @@ -1980,7 +1968,9 @@ def convert(
attempt to cast any object types to better types return a copy of
the block (if copy = True) by definition we ARE an ObjectBlock!!!!!
"""
if self.dtype != object:
if self.dtype != _dtype_obj:
# GH#50067 this should be impossible in ObjectBlock, but until
# that is fixed, we short-circuit here.
return [self]

values = self.values
Expand Down
1 change: 0 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,6 @@ def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:

# coercion
__float__ = _coerce_method(float)
__long__ = _coerce_method(int)
__int__ = _coerce_method(int)

# ----------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,7 @@ def _convert_listlike_datetimes(

elif is_datetime64_ns_dtype(arg_dtype):
if not isinstance(arg, (DatetimeArray, DatetimeIndex)):
try:
return DatetimeIndex(arg, tz=tz, name=name)
except ValueError:
pass
return DatetimeIndex(arg, tz=tz, name=name)
elif utc:
# DatetimeArray, DatetimeIndex
return arg.tz_localize("utc")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ def test_td64arr_floordiv_td64arr_with_nat(
expected = np.array([1.0, 1.0, np.nan], dtype=np.float64)
expected = tm.box_expected(expected, xbox)
if box is DataFrame and using_array_manager:
# INFO(ArrayManager) floorfiv returns integer, and ArrayManager
# INFO(ArrayManager) floordiv returns integer, and ArrayManager
# performs ops column-wise and thus preserves int64 dtype for
# columns without missing values
expected[[0, 1]] = expected[[0, 1]].astype("int64")
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,6 @@ def test_isin_nan_common_float64(self, nulls_fixture):
"index",
[
Index(["qux", "baz", "foo", "bar"]),
# float64 Index overrides isin, so must be checked separately
NumericIndex([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
],
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ def test_concat_retain_attrs(data):
@td.skip_array_manager_invalid_test
@pytest.mark.parametrize("df_dtype", ["float64", "int64", "datetime64[ns]"])
@pytest.mark.parametrize("empty_dtype", [None, "float64", "object"])
def test_concat_ignore_emtpy_object_float(empty_dtype, df_dtype):
def test_concat_ignore_empty_object_float(empty_dtype, df_dtype):
# https://github.com/pandas-dev/pandas/issues/45637
df = DataFrame({"foo": [1, 2], "bar": [1, 2]}, dtype=df_dtype)
empty = DataFrame(columns=["foo", "bar"], dtype=empty_dtype)
Expand Down