Skip to content

Commit 4ec7f33

Browse files
MarcoGorelliluckyvs1
authored andcommitted
CLN refactor core dtypes (pandas-dev#37584)
1 parent 5a9278a commit 4ec7f33

File tree

6 files changed

+44
-44
lines changed

6 files changed

+44
-44
lines changed

pandas/core/dtypes/cast.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""
22
Routines for casting.
33
"""
4+
5+
from __future__ import annotations
6+
47
from contextlib import suppress
58
from datetime import datetime, timedelta
69
from typing import (
@@ -114,12 +117,11 @@ def is_nested_object(obj) -> bool:
114117
This may not be necessarily be performant.
115118
116119
"""
117-
if isinstance(obj, ABCSeries) and is_object_dtype(obj.dtype):
118-
119-
if any(isinstance(v, ABCSeries) for v in obj._values):
120-
return True
121-
122-
return False
120+
return bool(
121+
isinstance(obj, ABCSeries)
122+
and is_object_dtype(obj.dtype)
123+
and any(isinstance(v, ABCSeries) for v in obj._values)
124+
)
123125

124126

125127
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,
707709

708710
# a 1-element ndarray
709711
if isinstance(val, np.ndarray):
710-
msg = "invalid ndarray passed to infer_dtype_from_scalar"
711712
if val.ndim != 0:
713+
msg = "invalid ndarray passed to infer_dtype_from_scalar"
712714
raise ValueError(msg)
713715

714716
dtype = val.dtype
@@ -976,7 +978,7 @@ def astype_dt64_to_dt64tz(
976978
result = result.copy()
977979
return result
978980

979-
elif values.tz is not None and not aware:
981+
elif values.tz is not None:
980982
result = values.tz_convert("UTC").tz_localize(None)
981983
if copy:
982984
result = result.copy()
@@ -1574,7 +1576,7 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
15741576
numpy.find_common_type
15751577
15761578
"""
1577-
if len(types) == 0:
1579+
if not types:
15781580
raise ValueError("no types given")
15791581

15801582
first = types[0]
@@ -1853,12 +1855,16 @@ def validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None:
18531855
------
18541856
ValueError
18551857
"""
1856-
if issubclass(dtype.type, (np.integer, np.bool_)):
1857-
if is_float(value) and np.isnan(value):
1858-
raise ValueError("Cannot assign nan to integer series")
1858+
if (
1859+
issubclass(dtype.type, (np.integer, np.bool_))
1860+
and is_float(value)
1861+
and np.isnan(value)
1862+
):
1863+
raise ValueError("Cannot assign nan to integer series")
18591864

1860-
if issubclass(dtype.type, (np.integer, np.floating, complex)) and not issubclass(
1861-
dtype.type, np.bool_
1865+
if (
1866+
issubclass(dtype.type, (np.integer, np.floating, complex))
1867+
and not issubclass(dtype.type, np.bool_)
1868+
and is_bool(value)
18621869
):
1863-
if is_bool(value):
1864-
raise ValueError("Cannot assign bool to float/integer series")
1870+
raise ValueError("Cannot assign bool to float/integer series")

pandas/core/dtypes/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ def infer_dtype_from_object(dtype):
17241724
elif dtype in ["period"]:
17251725
raise NotImplementedError
17261726

1727-
if dtype == "datetime" or dtype == "timedelta":
1727+
if dtype in ["datetime", "timedelta"]:
17281728
dtype += "64"
17291729
try:
17301730
return infer_dtype_from_object(getattr(np, dtype))
@@ -1759,7 +1759,7 @@ def _validate_date_like_dtype(dtype) -> None:
17591759
typ = np.datetime_data(dtype)[0]
17601760
except ValueError as e:
17611761
raise TypeError(e) from e
1762-
if typ != "generic" and typ != "ns":
1762+
if typ not in ["generic", "ns"]:
17631763
raise ValueError(
17641764
f"{repr(dtype.name)} is too specific of a frequency, "
17651765
f"try passing {repr(dtype.type.__name__)}"

pandas/core/dtypes/concat.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ def _cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike:
9191
# are not coming from Index/Series._values), eg in BlockManager.quantile
9292
arr = ensure_wrapped_if_datetimelike(arr)
9393

94-
if is_extension_array_dtype(dtype):
95-
if isinstance(arr, np.ndarray):
96-
# numpy's astype cannot handle ExtensionDtypes
97-
return array(arr, dtype=dtype, copy=False)
94+
if is_extension_array_dtype(dtype) and isinstance(arr, np.ndarray):
95+
# numpy's astype cannot handle ExtensionDtypes
96+
return array(arr, dtype=dtype, copy=False)
9897
return arr.astype(dtype, copy=False)
9998

10099

pandas/core/dtypes/dtypes.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,13 @@ def _hash_categories(categories, ordered: Ordered = True) -> int:
421421
categories = list(categories) # breaks if a np.array of categories
422422
cat_array = hash_tuples(categories)
423423
else:
424-
if categories.dtype == "O":
425-
if len({type(x) for x in categories}) != 1:
426-
# TODO: hash_array doesn't handle mixed types. It casts
427-
# everything to a str first, which means we treat
428-
# {'1', '2'} the same as {'1', 2}
429-
# find a better solution
430-
hashed = hash((tuple(categories), ordered))
431-
return hashed
424+
if categories.dtype == "O" and len({type(x) for x in categories}) != 1:
425+
# TODO: hash_array doesn't handle mixed types. It casts
426+
# everything to a str first, which means we treat
427+
# {'1', '2'} the same as {'1', 2}
428+
# find a better solution
429+
hashed = hash((tuple(categories), ordered))
430+
return hashed
432431

433432
if DatetimeTZDtype.is_dtype(categories.dtype):
434433
# Avoid future warning.
@@ -905,7 +904,7 @@ def __hash__(self) -> int:
905904

906905
def __eq__(self, other: Any) -> bool:
907906
if isinstance(other, str):
908-
return other == self.name or other == self.name.title()
907+
return other in [self.name, self.name.title()]
909908

910909
return isinstance(other, PeriodDtype) and self.freq == other.freq
911910

pandas/core/dtypes/inference.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ def is_file_like(obj) -> bool:
125125
if not (hasattr(obj, "read") or hasattr(obj, "write")):
126126
return False
127127

128-
if not hasattr(obj, "__iter__"):
129-
return False
130-
131-
return True
128+
return bool(hasattr(obj, "__iter__"))
132129

133130

134131
def is_re(obj) -> bool:

pandas/core/dtypes/missing.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ def isna_compat(arr, fill_value=np.nan) -> bool:
358358
-------
359359
True if we can fill using this fill_value
360360
"""
361-
dtype = arr.dtype
362361
if isna(fill_value):
362+
dtype = arr.dtype
363363
return not (is_bool_dtype(dtype) or is_integer_dtype(dtype))
364364
return True
365365

@@ -447,9 +447,10 @@ def array_equivalent(
447447
right = right.view("i8")
448448

449449
# if we have structured dtypes, compare first
450-
if left.dtype.type is np.void or right.dtype.type is np.void:
451-
if left.dtype != right.dtype:
452-
return False
450+
if (
451+
left.dtype.type is np.void or right.dtype.type is np.void
452+
) and left.dtype != right.dtype:
453+
return False
453454

454455
return np.array_equal(left, right)
455456

@@ -637,8 +638,6 @@ def isna_all(arr: ArrayLike) -> bool:
637638
else:
638639
checker = lambda x: _isna_ndarraylike(x, inf_as_na=INF_AS_NA)
639640

640-
for i in range(0, total_len, chunk_len):
641-
if not checker(arr[i : i + chunk_len]).all():
642-
return False
643-
644-
return True
641+
return all(
642+
checker(arr[i : i + chunk_len]).all() for i in range(0, total_len, chunk_len)
643+
)

0 commit comments

Comments
 (0)