From 6a20bb1c8e47a3f9c98f074cc13b7f215d8a206c Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 5 Dec 2020 19:31:46 -0800 Subject: [PATCH] CLN: dtypes.cast --- pandas/_libs/lib.pyx | 4 ++-- pandas/core/dtypes/cast.py | 16 ++++++++-------- pandas/core/internals/construction.py | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 1ca18bae4e2c4..c5fb20596d7b6 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1483,7 +1483,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str: return "mixed" -def infer_datetimelike_array(arr: object) -> object: +def infer_datetimelike_array(arr: ndarray[object]) -> str: """ Infer if we have a datetime or timedelta array. - date: we have *only* date and maybe strings, nulls @@ -1496,7 +1496,7 @@ def infer_datetimelike_array(arr: object) -> object: Parameters ---------- - arr : object array + arr : ndarray[object] Returns ------- diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 12974d56dacdc..12b5017cf34a0 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -495,7 +495,7 @@ def maybe_casted_values( Parameters ---------- index : Index - codes : sequence of integers (optional) + codes : np.ndarray[intp] or None, default None Returns ------- @@ -1330,7 +1330,7 @@ def maybe_infer_to_datetimelike( return value shape = v.shape - if not v.ndim == 1: + if v.ndim != 1: v = v.ravel() if not len(v): @@ -1400,7 +1400,7 @@ def try_timedelta(v): return value -def maybe_cast_to_datetime(value, dtype: DtypeObj, errors: str = "raise"): +def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]): """ try to cast the array/value to a datetimelike dtype, converting float nan to iNaT @@ -1469,7 +1469,7 @@ def maybe_cast_to_datetime(value, dtype: DtypeObj, errors: str = "raise"): elif np.prod(value.shape) or not is_dtype_equal(value.dtype, dtype): try: if is_datetime64: - value = to_datetime(value, errors=errors) + value = to_datetime(value, errors="raise") # GH 25843: Remove tz information since the dtype # didn't specify one if value.tz is not None: @@ -1481,7 +1481,7 @@ def maybe_cast_to_datetime(value, dtype: DtypeObj, errors: str = "raise"): # datetime64tz is assumed to be naive which should # be localized to the timezone. is_dt_string = is_string_dtype(value.dtype) - value = to_datetime(value, errors=errors).array + value = to_datetime(value, errors="raise").array if is_dt_string: # Strings here are naive, so directly localize value = value.tz_localize(dtype.tz) @@ -1490,7 +1490,7 @@ def maybe_cast_to_datetime(value, dtype: DtypeObj, errors: str = "raise"): # so localize and convert value = value.tz_localize("UTC").tz_convert(dtype.tz) elif is_timedelta64: - value = to_timedelta(value, errors=errors)._values + value = to_timedelta(value, errors="raise")._values except OutOfBoundsDatetime: raise except (AttributeError, ValueError, TypeError): @@ -1522,7 +1522,7 @@ def maybe_cast_to_datetime(value, dtype: DtypeObj, errors: str = "raise"): value = conversion.ensure_datetime64ns(value) elif dtype.kind == "m" and dtype != TD64NS_DTYPE: - value = to_timedelta(value) + value = conversion.ensure_timedelta64ns(value) # only do this if we have an array and the dtype of the array is not # setup already we are not an integer/object, so don't bother with this @@ -1659,7 +1659,7 @@ def construct_1d_arraylike_from_scalar( # GH36541: can't fill array directly with pd.NaT # > np.empty(10, dtype="datetime64[64]").fill(pd.NaT) # ValueError: cannot convert float NaN to integer - value = np.datetime64("NaT") + value = dtype.type("NaT", "ns") subarr = np.empty(length, dtype=dtype) subarr.fill(value) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index eefd1a604f894..9c2d08bd796cb 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -611,7 +611,7 @@ def _list_of_series_to_arrays( def _list_of_dict_to_arrays( - data: List, + data: List[Dict], columns: Union[Index, List], coerce_float: bool = False, dtype: Optional[DtypeObj] = None,