|
1 | 1 | """
|
2 | 2 | Routines for casting.
|
3 | 3 | """
|
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
4 | 7 | from contextlib import suppress
|
5 | 8 | from datetime import datetime, timedelta
|
6 | 9 | from typing import (
|
@@ -114,12 +117,11 @@ def is_nested_object(obj) -> bool:
|
114 | 117 | This may not be necessarily be performant.
|
115 | 118 |
|
116 | 119 | """
|
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 | + ) |
123 | 125 |
|
124 | 126 |
|
125 | 127 | 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,
|
707 | 709 |
|
708 | 710 | # a 1-element ndarray
|
709 | 711 | if isinstance(val, np.ndarray):
|
710 |
| - msg = "invalid ndarray passed to infer_dtype_from_scalar" |
711 | 712 | if val.ndim != 0:
|
| 713 | + msg = "invalid ndarray passed to infer_dtype_from_scalar" |
712 | 714 | raise ValueError(msg)
|
713 | 715 |
|
714 | 716 | dtype = val.dtype
|
@@ -976,7 +978,7 @@ def astype_dt64_to_dt64tz(
|
976 | 978 | result = result.copy()
|
977 | 979 | return result
|
978 | 980 |
|
979 |
| - elif values.tz is not None and not aware: |
| 981 | + elif values.tz is not None: |
980 | 982 | result = values.tz_convert("UTC").tz_localize(None)
|
981 | 983 | if copy:
|
982 | 984 | result = result.copy()
|
@@ -1574,7 +1576,7 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
|
1574 | 1576 | numpy.find_common_type
|
1575 | 1577 |
|
1576 | 1578 | """
|
1577 |
| - if len(types) == 0: |
| 1579 | + if not types: |
1578 | 1580 | raise ValueError("no types given")
|
1579 | 1581 |
|
1580 | 1582 | first = types[0]
|
@@ -1853,12 +1855,16 @@ def validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None:
|
1853 | 1855 | ------
|
1854 | 1856 | ValueError
|
1855 | 1857 | """
|
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") |
1859 | 1864 |
|
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) |
1862 | 1869 | ):
|
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") |
0 commit comments