diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index e06c781cb27a4..4e04425436af4 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1562,7 +1562,7 @@ def infer_datetimelike_array(arr: ndarray[object]) -> str: seen_tz_aware = True if seen_tz_naive and seen_tz_aware: - return 'mixed' + return "mixed" elif util.is_datetime64_object(v): # np.datetime64 seen_datetime = True diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 0917cf1787d5b..b991e3c7102ae 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -698,7 +698,7 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan): return dtype, fill_value -def _ensure_dtype_type(value, dtype: DtypeObj): +def _ensure_dtype_type(value, dtype: np.dtype): """ Ensure that the given value is an instance of the given dtype. @@ -708,21 +708,17 @@ def _ensure_dtype_type(value, dtype: DtypeObj): Parameters ---------- value : object - dtype : np.dtype or ExtensionDtype + dtype : np.dtype Returns ------- object """ # Start with exceptions in which we do _not_ cast to numpy types - if is_extension_array_dtype(dtype): - return value - elif dtype == np.object_: - return value - elif isna(value): - # e.g. keep np.nan rather than try to cast to np.float32(np.nan) + if dtype == np.object_: return value + # Note: before we get here we have already excluded isna(value) return dtype.type(value) @@ -1139,7 +1135,7 @@ def astype_nansafe( if isinstance(dtype, ExtensionDtype): return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy) - elif not isinstance(dtype, np.dtype): + elif not isinstance(dtype, np.dtype): # pragma: no cover raise ValueError("dtype must be np.dtype or ExtensionDtype") if arr.dtype.kind in ["m", "M"] and ( @@ -1389,9 +1385,7 @@ def maybe_castable(dtype: np.dtype) -> bool: return dtype.name not in POSSIBLY_CAST_DTYPES -def maybe_infer_to_datetimelike( - value: Union[np.ndarray, List], convert_dates: bool = False -): +def maybe_infer_to_datetimelike(value: Union[np.ndarray, List]): """ we might have a array (or single object) that is datetime like, and no dtype is passed don't change the value unless we find a @@ -1403,13 +1397,10 @@ def maybe_infer_to_datetimelike( Parameters ---------- value : np.ndarray or list - convert_dates : bool, default False - if True try really hard to convert dates (such as datetime.date), other - leave inferred dtype 'date' alone """ if not isinstance(value, (np.ndarray, list)): - raise TypeError(type(value)) + raise TypeError(type(value)) # pragma: no cover v = np.array(value, copy=False) @@ -1466,9 +1457,7 @@ def try_timedelta(v: np.ndarray) -> np.ndarray: inferred_type = lib.infer_datetimelike_array(ensure_object(v)) - if inferred_type == "date" and convert_dates: - value = try_datetime(v) - elif inferred_type == "datetime": + if inferred_type == "datetime": value = try_datetime(v) elif inferred_type == "timedelta": value = try_timedelta(v) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 18f9ece3e3812..9bfe852083390 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -248,13 +248,13 @@ def _convert_and_box_cache( return _box_as_indexlike(result, utc=None, name=name) -def _return_parsed_timezone_results(result, timezones, tz, name): +def _return_parsed_timezone_results(result: np.ndarray, timezones, tz, name) -> Index: """ Return results from array_strptime if a %z or %Z directive was passed. Parameters ---------- - result : ndarray + result : ndarray[int64] int64 date representations of the dates timezones : ndarray pytz timezone objects @@ -287,7 +287,7 @@ def _convert_listlike_datetimes( infer_datetime_format: Optional[bool] = None, dayfirst: Optional[bool] = None, yearfirst: Optional[bool] = None, - exact: Optional[bool] = None, + exact: bool = True, ): """ Helper function for to_datetime. Performs the conversions of 1D listlike @@ -311,7 +311,7 @@ def _convert_listlike_datetimes( dayfirst parsing behavior from to_datetime yearfirst : boolean yearfirst parsing behavior from to_datetime - exact : boolean + exact : bool, default True exact format matching behavior from to_datetime Returns