Skip to content

CLN: dtypes.cast #38327

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 1 commit into from
Dec 7, 2020
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
4 changes: 2 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -1496,7 +1496,7 @@ def infer_datetimelike_array(arr: object) -> object:

Parameters
----------
arr : object array
arr : ndarray[object]

Returns
-------
Expand Down
16 changes: 8 additions & 8 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down