diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 1cfa9957874ac..87f6e73e09d66 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1,6 +1,9 @@ """ Routines for casting. """ + +from __future__ import annotations + from contextlib import suppress from datetime import datetime, timedelta from typing import ( @@ -114,12 +117,11 @@ def is_nested_object(obj) -> bool: This may not be necessarily be performant. """ - if isinstance(obj, ABCSeries) and is_object_dtype(obj.dtype): - - if any(isinstance(v, ABCSeries) for v in obj._values): - return True - - return False + return bool( + isinstance(obj, ABCSeries) + and is_object_dtype(obj.dtype) + and any(isinstance(v, ABCSeries) for v in obj._values) + ) def maybe_box_datetimelike(value: Scalar, dtype: Optional[Dtype] = None) -> Scalar: @@ -707,8 +709,8 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, # a 1-element ndarray if isinstance(val, np.ndarray): - msg = "invalid ndarray passed to infer_dtype_from_scalar" if val.ndim != 0: + msg = "invalid ndarray passed to infer_dtype_from_scalar" raise ValueError(msg) dtype = val.dtype @@ -976,7 +978,7 @@ def astype_dt64_to_dt64tz( result = result.copy() return result - elif values.tz is not None and not aware: + elif values.tz is not None: result = values.tz_convert("UTC").tz_localize(None) if copy: result = result.copy() @@ -1574,7 +1576,7 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj: numpy.find_common_type """ - if len(types) == 0: + if not types: raise ValueError("no types given") first = types[0] @@ -1853,12 +1855,16 @@ def validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None: ------ ValueError """ - if issubclass(dtype.type, (np.integer, np.bool_)): - if is_float(value) and np.isnan(value): - raise ValueError("Cannot assign nan to integer series") + if ( + issubclass(dtype.type, (np.integer, np.bool_)) + and is_float(value) + and np.isnan(value) + ): + raise ValueError("Cannot assign nan to integer series") - if issubclass(dtype.type, (np.integer, np.floating, complex)) and not issubclass( - dtype.type, np.bool_ + if ( + issubclass(dtype.type, (np.integer, np.floating, complex)) + and not issubclass(dtype.type, np.bool_) + and is_bool(value) ): - if is_bool(value): - raise ValueError("Cannot assign bool to float/integer series") + raise ValueError("Cannot assign bool to float/integer series") diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 46aff11835cec..1993c41db03f8 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1724,7 +1724,7 @@ def infer_dtype_from_object(dtype): elif dtype in ["period"]: raise NotImplementedError - if dtype == "datetime" or dtype == "timedelta": + if dtype in ["datetime", "timedelta"]: dtype += "64" try: return infer_dtype_from_object(getattr(np, dtype)) @@ -1759,7 +1759,7 @@ def _validate_date_like_dtype(dtype) -> None: typ = np.datetime_data(dtype)[0] except ValueError as e: raise TypeError(e) from e - if typ != "generic" and typ != "ns": + if typ not in ["generic", "ns"]: raise ValueError( f"{repr(dtype.name)} is too specific of a frequency, " f"try passing {repr(dtype.type.__name__)}" diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py index b8d2e7e1a40a9..d768f83e4d36b 100644 --- a/pandas/core/dtypes/concat.py +++ b/pandas/core/dtypes/concat.py @@ -91,10 +91,9 @@ def _cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike: # are not coming from Index/Series._values), eg in BlockManager.quantile arr = ensure_wrapped_if_datetimelike(arr) - if is_extension_array_dtype(dtype): - if isinstance(arr, np.ndarray): - # numpy's astype cannot handle ExtensionDtypes - return array(arr, dtype=dtype, copy=False) + if is_extension_array_dtype(dtype) and isinstance(arr, np.ndarray): + # numpy's astype cannot handle ExtensionDtypes + return array(arr, dtype=dtype, copy=False) return arr.astype(dtype, copy=False) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 123196f43ef2a..f31bcd97eafcf 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -421,14 +421,13 @@ def _hash_categories(categories, ordered: Ordered = True) -> int: categories = list(categories) # breaks if a np.array of categories cat_array = hash_tuples(categories) else: - if categories.dtype == "O": - if len({type(x) for x in categories}) != 1: - # TODO: hash_array doesn't handle mixed types. It casts - # everything to a str first, which means we treat - # {'1', '2'} the same as {'1', 2} - # find a better solution - hashed = hash((tuple(categories), ordered)) - return hashed + if categories.dtype == "O" and len({type(x) for x in categories}) != 1: + # TODO: hash_array doesn't handle mixed types. It casts + # everything to a str first, which means we treat + # {'1', '2'} the same as {'1', 2} + # find a better solution + hashed = hash((tuple(categories), ordered)) + return hashed if DatetimeTZDtype.is_dtype(categories.dtype): # Avoid future warning. @@ -905,7 +904,7 @@ def __hash__(self) -> int: def __eq__(self, other: Any) -> bool: if isinstance(other, str): - return other == self.name or other == self.name.title() + return other in [self.name, self.name.title()] return isinstance(other, PeriodDtype) and self.freq == other.freq diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index 329c4445b05bc..8f8ded2ad54b1 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -125,10 +125,7 @@ def is_file_like(obj) -> bool: if not (hasattr(obj, "read") or hasattr(obj, "write")): return False - if not hasattr(obj, "__iter__"): - return False - - return True + return bool(hasattr(obj, "__iter__")) def is_re(obj) -> bool: diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index e80e6f370f46e..f0455c01fa085 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -358,8 +358,8 @@ def isna_compat(arr, fill_value=np.nan) -> bool: ------- True if we can fill using this fill_value """ - dtype = arr.dtype if isna(fill_value): + dtype = arr.dtype return not (is_bool_dtype(dtype) or is_integer_dtype(dtype)) return True @@ -447,9 +447,10 @@ def array_equivalent( right = right.view("i8") # if we have structured dtypes, compare first - if left.dtype.type is np.void or right.dtype.type is np.void: - if left.dtype != right.dtype: - return False + if ( + left.dtype.type is np.void or right.dtype.type is np.void + ) and left.dtype != right.dtype: + return False return np.array_equal(left, right) @@ -637,8 +638,6 @@ def isna_all(arr: ArrayLike) -> bool: else: checker = lambda x: _isna_ndarraylike(x, inf_as_na=INF_AS_NA) - for i in range(0, total_len, chunk_len): - if not checker(arr[i : i + chunk_len]).all(): - return False - - return True + return all( + checker(arr[i : i + chunk_len]).all() for i in range(0, total_len, chunk_len) + )