Skip to content

CLN: assorted #52820

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
Apr 21, 2023
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
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ from pandas._libs.tslibs.nattype cimport (
c_nat_strings as nat_strings,
)
from pandas._libs.tslibs.parsing cimport parse_datetime_string
from pandas._libs.tslibs.timestamps cimport _Timestamp
from pandas._libs.tslibs.timezones cimport (
get_utcoffset,
is_utc,
Expand Down Expand Up @@ -761,7 +760,7 @@ cdef int64_t parse_pydatetime(
_ts.ensure_reso(NPY_FR_ns)
result = _ts.value
else:
if isinstance(val, _Timestamp):
if isinstance(val, ABCTimestamp):
result = val.as_unit("ns")._value
else:
result = pydatetime_to_dt64(val, dts)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ from numpy cimport (

cnp.import_array()

from cpython.datetime cimport ( # alias bc `tzinfo` is a kwarg below
from cpython.datetime cimport ( # alias tzinfo_type bc `tzinfo` is a kwarg below
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm i had tried to put this comment adjacent to the tzinfo_type import but looks like one of the hooks moved it back

PyDate_Check,
PyDateTime_Check,
PyDelta_Check,
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cdef extern from "Python.h":
bint PyComplex_Check(object obj) nogil
bint PyObject_TypeCheck(object obj, PyTypeObject* type) nogil

# TODO(cython3): cimport this, xref GH#49670
# Note that following functions can potentially raise an exception,
# thus they cannot be declared 'nogil'. Also PyUnicode_AsUTF8AndSize() can
# potentially allocate memory inside in unlikely case of when underlying
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/computation/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ def _align_core(terms):
w, category=PerformanceWarning, stacklevel=find_stack_level()
)

f = partial(ti.reindex, reindexer, axis=axis, copy=False)

terms[i].update(f())
obj = ti.reindex(reindexer, axis=axis, copy=False)
terms[i].update(obj)

terms[i].update(terms[i].value.values)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
ensure_str,
is_bool,
is_complex,
is_extension_array_dtype,
is_float,
is_integer,
is_object_dtype,
Expand Down Expand Up @@ -903,7 +902,8 @@ def infer_dtype_from_array(
if not is_list_like(arr):
raise TypeError("'arr' must be list-like")

if pandas_dtype and is_extension_array_dtype(arr):
arr_dtype = getattr(arr, "dtype", None)
if pandas_dtype and isinstance(arr_dtype, ExtensionDtype):
return arr.dtype, arr

elif isinstance(arr, ABCSeries):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
TD64NS_DTYPE,
ensure_object,
is_dtype_equal,
is_extension_array_dtype,
is_scalar,
is_string_or_object_np_dtype,
)
Expand Down Expand Up @@ -56,6 +55,7 @@
npt,
)

from pandas import Series
from pandas.core.indexes.base import Index


Expand Down Expand Up @@ -642,11 +642,11 @@ def na_value_for_dtype(dtype: DtypeObj, compat: bool = True):
return np.nan


def remove_na_arraylike(arr):
def remove_na_arraylike(arr: Series | Index | np.ndarray):
"""
Return array-like containing only true/non-NaN values, possibly empty.
"""
if is_extension_array_dtype(arr):
if isinstance(arr.dtype, ExtensionDtype):
return arr[notna(arr)]
else:
return arr[notna(np.asarray(arr))]
Expand Down
26 changes: 26 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ def _get_cleaned_column_resolvers(self) -> dict[Hashable, Series]:
clean_column_name(k): v for k, v in self.items() if not isinstance(k, int)
}

@final
@property
def _info_axis(self) -> Index:
return getattr(self, self._info_axis_name)
Expand Down Expand Up @@ -969,6 +970,7 @@ def squeeze(self, axis: Axis | None = None):
# ----------------------------------------------------------------------
# Rename

@final
def _rename(
self,
mapper: Renamer | None = None,
Expand Down Expand Up @@ -3476,6 +3478,7 @@ def _wrap(x, alt_format_):
render_kwargs=render_kwargs_,
)

@final
def _to_latex_via_styler(
self,
buf=None,
Expand Down Expand Up @@ -4293,6 +4296,7 @@ def _check_setitem_copy(self, t: str = "setting", force: bool_t = False):
if value == "warn":
warnings.warn(t, SettingWithCopyWarning, stacklevel=find_stack_level())

@final
def __delitem__(self, key) -> None:
"""
Delete item
Expand Down Expand Up @@ -6069,6 +6073,7 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:

return self

@final
def __getattr__(self, name: str):
"""
After regular attribute access, try looking up the name
Expand All @@ -6085,6 +6090,7 @@ def __getattr__(self, name: str):
return self[name]
return object.__getattribute__(self, name)

@final
def __setattr__(self, name: str, value) -> None:
"""
After regular attribute access, try setting the name
Expand Down Expand Up @@ -6255,6 +6261,7 @@ def dtypes(self):
data = self._mgr.get_dtypes()
return self._constructor_sliced(data, index=self._info_axis, dtype=np.object_)

@final
def astype(
self, dtype, copy: bool_t | None = None, errors: IgnoreRaise = "raise"
) -> Self:
Expand Down Expand Up @@ -7128,6 +7135,7 @@ def ffill(
) -> Self | None:
...

@final
@doc(klass=_shared_doc_kwargs["klass"])
def ffill(
self,
Expand All @@ -7149,6 +7157,7 @@ def ffill(
method="ffill", axis=axis, inplace=inplace, limit=limit, downcast=downcast
)

@final
@doc(klass=_shared_doc_kwargs["klass"])
def pad(
self,
Expand Down Expand Up @@ -7211,6 +7220,7 @@ def bfill(
) -> Self | None:
...

@final
@doc(klass=_shared_doc_kwargs["klass"])
def bfill(
self,
Expand All @@ -7232,6 +7242,7 @@ def bfill(
method="bfill", axis=axis, inplace=inplace, limit=limit, downcast=downcast
)

@final
@doc(klass=_shared_doc_kwargs["klass"])
def backfill(
self,
Expand Down Expand Up @@ -7501,6 +7512,7 @@ def replace(
else:
return result.__finalize__(self, method="replace")

@final
def interpolate(
self,
method: Literal[
Expand Down Expand Up @@ -8185,6 +8197,7 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
# GH 40420
return self.where(subset, threshold, axis=axis, inplace=inplace)

@final
def clip(
self,
lower=None,
Expand Down Expand Up @@ -9996,6 +10009,7 @@ def where(
) -> Self | None:
...

@final
@doc(
klass=_shared_doc_kwargs["klass"],
cond="True",
Expand Down Expand Up @@ -10188,6 +10202,7 @@ def mask(
) -> Self | None:
...

@final
@doc(
where,
klass=_shared_doc_kwargs["klass"],
Expand Down Expand Up @@ -10368,6 +10383,7 @@ def shift(
result = self.set_axis(new_ax, axis=axis)
return result.__finalize__(self, method="shift")

@final
def truncate(
self,
before=None,
Expand Down Expand Up @@ -11693,46 +11709,56 @@ def _inplace_method(self, other, op) -> Self:
)
return self

@final
def __iadd__(self, other) -> Self:
# error: Unsupported left operand type for + ("Type[NDFrame]")
return self._inplace_method(other, type(self).__add__) # type: ignore[operator]

@final
def __isub__(self, other) -> Self:
# error: Unsupported left operand type for - ("Type[NDFrame]")
return self._inplace_method(other, type(self).__sub__) # type: ignore[operator]

@final
def __imul__(self, other) -> Self:
# error: Unsupported left operand type for * ("Type[NDFrame]")
return self._inplace_method(other, type(self).__mul__) # type: ignore[operator]

@final
def __itruediv__(self, other) -> Self:
# error: Unsupported left operand type for / ("Type[NDFrame]")
return self._inplace_method(
other, type(self).__truediv__ # type: ignore[operator]
)

@final
def __ifloordiv__(self, other) -> Self:
# error: Unsupported left operand type for // ("Type[NDFrame]")
return self._inplace_method(
other, type(self).__floordiv__ # type: ignore[operator]
)

@final
def __imod__(self, other) -> Self:
# error: Unsupported left operand type for % ("Type[NDFrame]")
return self._inplace_method(other, type(self).__mod__) # type: ignore[operator]

@final
def __ipow__(self, other) -> Self:
# error: Unsupported left operand type for ** ("Type[NDFrame]")
return self._inplace_method(other, type(self).__pow__) # type: ignore[operator]

@final
def __iand__(self, other) -> Self:
# error: Unsupported left operand type for & ("Type[NDFrame]")
return self._inplace_method(other, type(self).__and__) # type: ignore[operator]

@final
def __ior__(self, other) -> Self:
# error: Unsupported left operand type for | ("Type[NDFrame]")
return self._inplace_method(other, type(self).__or__) # type: ignore[operator]

@final
def __ixor__(self, other) -> Self:
# error: Unsupported left operand type for ^ ("Type[NDFrame]")
return self._inplace_method(other, type(self).__xor__) # type: ignore[operator]
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,18 +922,20 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):

return self.__array_wrap__(result)

@final
def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc and other functions e.g. np.split.
"""
result = lib.item_from_zerodim(result)
if (
(not isinstance(result, Index) and is_bool_dtype(result))
(not isinstance(result, Index) and is_bool_dtype(result.dtype))
or lib.is_scalar(result)
or np.ndim(result) > 1
):
# exclude Index to avoid warning from is_bool_dtype deprecation;
# in the Index case it doesn't matter which path we go down.
# reached in plotting tests with e.g. np.nonzero(index)
return result

return Index(result, name=self.name)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)

from pandas.core.dtypes.common import is_scalar
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas.core.dtypes.missing import (
is_valid_na_for_dtype,
Expand Down Expand Up @@ -478,7 +479,6 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
)
except TypeError:
# not all to_concat elements are among our categories (or NA)
from pandas.core.dtypes.concat import concat_compat

res = concat_compat([x._values for x in to_concat])
return Index(res, name=name)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@


def _get_next_label(label):
# see test_slice_locs_with_ints_and_floats_succeeds
dtype = getattr(label, "dtype", type(label))
if isinstance(label, (Timestamp, Timedelta)):
dtype = "datetime64[ns]"
Expand All @@ -129,6 +130,7 @@ def _get_next_label(label):


def _get_prev_label(label):
# see test_slice_locs_with_ints_and_floats_succeeds
dtype = getattr(label, "dtype", type(label))
if isinstance(label, (Timestamp, Timedelta)):
dtype = "datetime64[ns]"
Expand Down
8 changes: 3 additions & 5 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

from pandas.util._decorators import Appender

from pandas.core.dtypes.common import (
is_extension_array_dtype,
is_list_like,
)
from pandas.core.dtypes.common import is_list_like
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.missing import notna

Expand Down Expand Up @@ -126,7 +123,8 @@ def melt(
mdata: dict[Hashable, AnyArrayLike] = {}
for col in id_vars:
id_data = frame.pop(col)
if is_extension_array_dtype(id_data):
if not isinstance(id_data.dtype, np.dtype):
# i.e. ExtensionDtype
if K > 0:
mdata[col] = concat([id_data] * K, ignore_index=True)
else:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

from pandas.core.dtypes.cast import maybe_downcast_to_dtype
from pandas.core.dtypes.common import (
is_extension_array_dtype,
is_integer_dtype,
is_list_like,
is_nested_list_like,
is_scalar,
)
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCSeries,
Expand Down Expand Up @@ -326,7 +326,7 @@ def _add_margins(
row_names = result.index.names
# check the result column and leave floats
for dtype in set(result.dtypes):
if is_extension_array_dtype(dtype):
if isinstance(dtype, ExtensionDtype):
# Can hold NA already
continue

Expand Down
Loading