Skip to content

CLN: remove never-hit branch from maybe_infer_to_datetimelike #40109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @MarcoGorelli would not be averse to a pre-commit to make sure quoting is uniform per file (e.g. either ' or "), ideally we should just be uniform across the entire codebase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

black ensures we use double in the python code; the cython is a mishmash but mostly double at this point

elif util.is_datetime64_object(v):
# np.datetime64
seen_datetime = True
Expand Down
27 changes: 8 additions & 19 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)


Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down