Skip to content
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

CLN: clean-up sanitize_array series construction #26979

Merged
Merged
Changes from 2 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
64 changes: 22 additions & 42 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
is_extension_array_dtype, is_extension_type, is_float_dtype,
is_integer_dtype, is_iterator, is_list_like, is_object_dtype, pandas_dtype)
from pandas.core.dtypes.generic import (
ABCDataFrame, ABCDatetimeIndex, ABCIndexClass, ABCPandasArray,
ABCPeriodIndex, ABCSeries, ABCTimedeltaIndex)
ABCDataFrame, ABCDatetimeIndex, ABCIndexClass, ABCPeriodIndex,
ABCSeries, ABCTimedeltaIndex)
from pandas.core.dtypes.missing import isna

from pandas.core import algorithms, common as com
Expand Down Expand Up @@ -549,59 +549,39 @@ def sanitize_array(data, index, dtype=None, copy=False,
else:
data = data.copy()

# extract ndarray or ExtensionArray, ensure we have no PandasArray
data = extract_array(data, extract_numpy=True)

# GH#846
if isinstance(data, np.ndarray):

if dtype is not None:
subarr = np.array(data, copy=False)

if (dtype is not None
and is_float_dtype(data.dtype) and is_integer_dtype(dtype)):
Copy link
Contributor

Choose a reason for hiding this comment

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

future cleanup is to remove this and condition (as I think _try_cast already handles this)

# possibility of nan -> garbage
if is_float_dtype(data.dtype) and is_integer_dtype(dtype):
try:
subarr = _try_cast(data, True, dtype, copy,
True)
except ValueError:
if copy:
subarr = data.copy()
else:
subarr = _try_cast(data, True, dtype, copy, raise_cast_failure)
elif isinstance(data, Index):
Copy link
Member Author

Choose a reason for hiding this comment

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

It was already checked above that data is an ndarray, so can never be an index

# don't coerce Index types
# e.g. indexes can have different conversions (so don't fast path
# them)
# GH#6140
subarr = sanitize_index(data, index, copy=copy)
try:
subarr = _try_cast(data, dtype, copy, True)
except ValueError:
if copy:
subarr = data.copy()
else:
subarr = np.array(data, copy=False)
else:

# we will try to copy be-definition here
subarr = _try_cast(data, True, dtype, copy, raise_cast_failure)
subarr = _try_cast(data, dtype, copy, raise_cast_failure)

elif isinstance(data, ExtensionArray):
if isinstance(data, ABCPandasArray):
Copy link
Member Author

Choose a reason for hiding this comment

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

The extract_array(data, extract_numpy=True) above already did this

# We don't want to let people put our PandasArray wrapper
# (the output of Series/Index.array), into a Series. So
# we explicitly unwrap it here.
subarr = data.to_numpy()
else:
subarr = data

# everything else in this block must also handle ndarray's,
# becuase we've unwrapped PandasArray into an ndarray.

# it is already ensured above this is not a PandasArray
subarr = data
if dtype is not None:
subarr = data.astype(dtype)

subarr = subarr.astype(dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

add copy=False here (not sure if it matters but can't hurt)

if copy:
subarr = data.copy()
subarr = subarr.copy()
jreback marked this conversation as resolved.
Show resolved Hide resolved
return subarr

elif isinstance(data, (list, tuple)) and len(data) > 0:
if dtype is not None:
try:
subarr = _try_cast(data, False, dtype, copy,
raise_cast_failure)
subarr = _try_cast(data, dtype, copy, raise_cast_failure)
except Exception:
if raise_cast_failure: # pragma: no cover
raise
Expand All @@ -616,9 +596,9 @@ def sanitize_array(data, index, dtype=None, copy=False,
elif isinstance(data, range):
# GH#16804
arr = np.arange(data.start, data.stop, data.step, dtype='int64')
subarr = _try_cast(arr, False, dtype, copy, raise_cast_failure)
subarr = _try_cast(arr, dtype, copy, raise_cast_failure)
else:
subarr = _try_cast(data, False, dtype, copy, raise_cast_failure)
subarr = _try_cast(data, dtype, copy, raise_cast_failure)

# scalar like, GH
if getattr(subarr, 'ndim', 0) == 0:
Expand Down Expand Up @@ -677,10 +657,10 @@ def sanitize_array(data, index, dtype=None, copy=False,
return subarr


def _try_cast(arr, take_fast_path, dtype, copy, raise_cast_failure):
def _try_cast(arr, dtype, copy, raise_cast_failure):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add types here?

Copy link
Member

Choose a reason for hiding this comment

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

Nice removing the fast path arg!

Copy link
Contributor

Choose a reason for hiding this comment

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

@jorisvandenbossche can you add types here?


Copy link
Contributor

Choose a reason for hiding this comment

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

can you try to doc-string / type this?

# perf shortcut as this is the most common case
if take_fast_path:
if isinstance(arr, np.ndarray):
if maybe_castable(arr) and not copy and dtype is None:
return arr

Expand Down