Skip to content

CLN: assorted #53932

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 1 commit into from
Jun 29, 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
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ NoDefault = Literal[_NoDefault.no_default]
i8max: int
u8max: int

def is_np_dtype(dtype: object, kinds: str | None = ...) -> bool: ...
def is_np_dtype(dtype: object, kinds: str | None = ...) -> TypeGuard[np.dtype]: ...
def item_from_zerodim(val: object) -> object: ...
def infer_dtype(value: object, skipna: bool = ...) -> str: ...
def is_iterator(obj: object) -> bool: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/array_algos/take.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def take_nd(
"""
if fill_value is lib.no_default:
fill_value = na_value_for_dtype(arr.dtype, compat=False)
elif isinstance(arr.dtype, np.dtype) and arr.dtype.kind in "mM":
elif lib.is_np_dtype(arr.dtype, "mM"):
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
if arr.dtype != dtype:
# EA.take is strict about returning a new object of the same type
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def _simple_new( # type: ignore[override]
dtype: np.dtype[np.timedelta64] = TD64NS_DTYPE,
) -> Self:
# Require td64 dtype, not unit-less, matching values.dtype
assert isinstance(dtype, np.dtype) and dtype.kind == "m"
assert lib.is_np_dtype(dtype, "m")
assert not tslibs.is_unitless(dtype)
assert isinstance(values, np.ndarray), type(values)
assert dtype == values.dtype
Expand Down Expand Up @@ -363,7 +363,7 @@ def astype(self, dtype, copy: bool = True):
# DatetimeLikeArrayMixin super call handles other cases
dtype = pandas_dtype(dtype)

if isinstance(dtype, np.dtype) and dtype.kind == "m":
if lib.is_np_dtype(dtype, "m"):
if dtype == self.dtype:
if copy:
return self.copy()
Expand Down
14 changes: 2 additions & 12 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,9 @@ def array(
# 1. datetime64[ns,us,ms,s]
# 2. timedelta64[ns,us,ms,s]
# so that a DatetimeArray is returned.
if (
lib.is_np_dtype(dtype, "M")
# error: Argument 1 to "py_get_unit_from_dtype" has incompatible type
# "Optional[dtype[Any]]"; expected "dtype[Any]"
and is_supported_unit(get_unit_from_dtype(dtype)) # type: ignore[arg-type]
):
if lib.is_np_dtype(dtype, "M") and is_supported_unit(get_unit_from_dtype(dtype)):
return DatetimeArray._from_sequence(data, dtype=dtype, copy=copy)
if (
lib.is_np_dtype(dtype, "m")
# error: Argument 1 to "py_get_unit_from_dtype" has incompatible type
# "Optional[dtype[Any]]"; expected "dtype[Any]"
and is_supported_unit(get_unit_from_dtype(dtype)) # type: ignore[arg-type]
):
if lib.is_np_dtype(dtype, "m") and is_supported_unit(get_unit_from_dtype(dtype)):
return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy)

return PandasArray._from_sequence(data, dtype=dtype, copy=copy)
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 @@ -1325,7 +1325,7 @@ def common_dtype_categorical_compat(
# GH#38240

# TODO: more generally, could do `not can_hold_na(dtype)`
if isinstance(dtype, np.dtype) and dtype.kind in "iu":
if lib.is_np_dtype(dtype, "iu"):
for obj in objs:
# We don't want to accientally allow e.g. "categorical" str here
obj_dtype = getattr(obj, "dtype", None)
Expand Down Expand Up @@ -1502,7 +1502,7 @@ def construct_1d_arraylike_from_scalar(
if length and dtype.kind in "iu" and isna(value):
# coerce if we have nan for an integer dtype
dtype = np.dtype("float64")
elif isinstance(dtype, np.dtype) and dtype.kind in "US":
elif lib.is_np_dtype(dtype, "US"):
# we need to coerce to object dtype to avoid
# to allow numpy to take our string as a scalar value
dtype = np.dtype("object")
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,9 +890,7 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool:
tipo = _get_dtype(arr_or_dtype)
except TypeError:
return False
return (isinstance(tipo, np.dtype) and tipo.kind == "M") or isinstance(
tipo, DatetimeTZDtype
)
return lib.is_np_dtype(tipo, "M") or isinstance(tipo, DatetimeTZDtype)


def is_datetime64_ns_dtype(arr_or_dtype) -> bool:
Expand Down Expand Up @@ -1329,9 +1327,7 @@ def is_ea_or_datetimelike_dtype(dtype: DtypeObj | None) -> bool:
-----
Checks only for dtype objects, not dtype-castable strings or types.
"""
return isinstance(dtype, ExtensionDtype) or (
isinstance(dtype, np.dtype) and dtype.kind in "mM"
)
return isinstance(dtype, ExtensionDtype) or (lib.is_np_dtype(dtype, "mM"))


def is_complex_dtype(arr_or_dtype) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,10 @@ def isna_all(arr: ArrayLike) -> bool:
chunk_len = max(total_len // 40, 1000)

dtype = arr.dtype
if dtype.kind == "f" and isinstance(dtype, np.dtype):
if lib.is_np_dtype(dtype, "f"):
checker = nan_checker

elif (isinstance(dtype, np.dtype) and dtype.kind in "mM") or isinstance(
elif (lib.is_np_dtype(dtype, "mM")) or isinstance(
dtype, (DatetimeTZDtype, PeriodDtype)
):
# error: Incompatible types in assignment (expression has type
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/interchange/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def dtype_to_arrow_c_fmt(dtype: DtypeObj) -> str:
if lib.is_np_dtype(dtype, "M"):
# Selecting the first char of resolution string:
# dtype.str -> '<M8[ns]'
resolution = re.findall(r"\[(.*)\]", typing.cast(np.dtype, dtype).str)[0][:1]
resolution = re.findall(r"\[(.*)\]", dtype.str)[0][:1]
return ArrowCTypes.TIMESTAMP.format(resolution=resolution, tz="")

raise NotImplementedError(
Expand Down
12 changes: 3 additions & 9 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def apply(
assert "filter" not in kwargs

align_keys = align_keys or []
result_arrays: list[np.ndarray] = []
result_arrays: list[ArrayLike] = []
# fillna: Series/DataFrame is responsible for making sure value is aligned

aligned_args = {k: kwargs[k] for k in align_keys}
Expand All @@ -247,16 +247,10 @@ def apply(
else:
applied = getattr(arr, f)(**kwargs)

# if not isinstance(applied, ExtensionArray):
# # TODO not all EA operations return new EAs (eg astype)
# applied = array(applied)
result_arrays.append(applied)

new_axes = self._axes

# error: Argument 1 to "ArrayManager" has incompatible type "List[ndarray]";
# expected "List[Union[ndarray, ExtensionArray]]"
return type(self)(result_arrays, new_axes) # type: ignore[arg-type]
return type(self)(result_arrays, new_axes)

def apply_with_block(self, f, align_keys=None, **kwargs) -> Self:
# switch axis to follow BlockManager logic
Expand Down Expand Up @@ -1319,7 +1313,7 @@ def concat_arrays(to_concat: list) -> ArrayLike:

if single_dtype:
target_dtype = to_concat_no_proxy[0].dtype
elif all(x.kind in "iub" and isinstance(x, np.dtype) for x in dtypes):
elif all(lib.is_np_dtype(x, "iub") for x in dtypes):
# GH#42092
target_dtype = np_find_common_type(*dtypes)
else:
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ def _downcast_2d(self, dtype, using_cow: bool = False) -> list[Block]:
refs = self.refs if using_cow and new_values is self.values else None
return [self.make_block(new_values, refs=refs)]

@final
def convert(
self,
*,
Expand Down Expand Up @@ -951,6 +952,7 @@ def set_inplace(self, locs, values: ArrayLike, copy: bool = False) -> None:
self.values = self.values.copy()
self.values[locs] = values

@final
def take_nd(
self,
indexer: npt.NDArray[np.intp],
Expand Down Expand Up @@ -1628,6 +1630,7 @@ class EABackedBlock(Block):

values: ExtensionArray

@final
def shift(self, periods: int, fill_value: Any = None) -> list[Block]:
"""
Shift the block by `periods`.
Expand All @@ -1640,6 +1643,7 @@ def shift(self, periods: int, fill_value: Any = None) -> list[Block]:
new_values = self.values.T.shift(periods=periods, fill_value=fill_value).T
return [self.make_block_same_class(new_values)]

@final
def setitem(self, indexer, value, using_cow: bool = False):
"""
Attempt self.values[indexer] = value, possibly creating a new array.
Expand Down Expand Up @@ -1698,6 +1702,7 @@ def setitem(self, indexer, value, using_cow: bool = False):
else:
return self

@final
def where(
self, other, cond, _downcast: str | bool = "infer", using_cow: bool = False
) -> list[Block]:
Expand Down Expand Up @@ -1768,6 +1773,7 @@ def where(
nb = self.make_block_same_class(res_values)
return [nb]

@final
def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
"""
See Block.putmask.__doc__
Expand Down Expand Up @@ -1835,6 +1841,7 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:

return [self]

@final
def delete(self, loc) -> list[Block]:
# This will be unnecessary if/when __array_function__ is implemented
if self.ndim == 1:
Expand All @@ -1846,10 +1853,12 @@ def delete(self, loc) -> list[Block]:
return []
return super().delete(loc)

@final
@cache_readonly
def array_values(self) -> ExtensionArray:
return self.values

@final
def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
"""
return object dtype as boxed values, such as Timestamps/Timedelta
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,11 +1607,7 @@ def as_array(
if na_value is not lib.no_default:
# We want to copy when na_value is provided to avoid
# mutating the original object
if (
isinstance(blk.dtype, np.dtype)
and blk.dtype.kind == "f"
and passed_nan
):
if lib.is_np_dtype(blk.dtype, "f") and passed_nan:
# We are already numpy-float and na_value=np.nan
pass
else:
Expand Down
1 change: 0 additions & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ def _convert_listlike_datetimes(
return arg

elif lib.is_np_dtype(arg_dtype, "M"):
arg_dtype = cast(np.dtype, arg_dtype)
if not is_supported_unit(get_unit_from_dtype(arg_dtype)):
# We go to closest supported reso, i.e. "s"
arg = astype_overflowsafe(
Expand Down
16 changes: 4 additions & 12 deletions pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import numpy as np
import pytest

from pandas._libs import lib

from pandas.core.dtypes.cast import find_common_type

from pandas import (
Expand Down Expand Up @@ -67,18 +69,8 @@ def test_union_different_types(index_flat, index_flat2, request):
warn = None
if not len(idx1) or not len(idx2):
pass
elif (
idx1.dtype.kind == "c"
and (
idx2.dtype.kind not in ["i", "u", "f", "c"]
or not isinstance(idx2.dtype, np.dtype)
)
) or (
idx2.dtype.kind == "c"
and (
idx1.dtype.kind not in ["i", "u", "f", "c"]
or not isinstance(idx1.dtype, np.dtype)
)
elif (idx1.dtype.kind == "c" and (not lib.is_np_dtype(idx2.dtype, "iufc"))) or (
idx2.dtype.kind == "c" and (not lib.is_np_dtype(idx1.dtype, "iufc"))
):
# complex objects non-sortable
warn = RuntimeWarning
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ def test_merge_asof_index_behavior(kwargs):
tm.assert_frame_equal(result, expected)


def test_merge_asof_numeri_column_in_index():
def test_merge_asof_numeric_column_in_index():
# GH#34488
left = pd.DataFrame({"b": [10, 11, 12]}, index=Index([1, 2, 3], name="a"))
right = pd.DataFrame({"c": [20, 21, 22]}, index=Index([0, 2, 3], name="a"))
Expand All @@ -1528,7 +1528,7 @@ def test_merge_asof_numeri_column_in_index():
tm.assert_frame_equal(result, expected)


def test_merge_asof_numeri_column_in_multiindex():
def test_merge_asof_numeric_column_in_multiindex():
# GH#34488
left = pd.DataFrame(
{"b": [10, 11, 12]},
Expand Down