Skip to content

CLN: collect fastpath in Series.__init__ #49575

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
Nov 9, 2022
Merged
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
171 changes: 83 additions & 88 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ def __init__(

# we are called internally, so short-circuit
if fastpath:

# data is an ndarray, index is defined
# data is a ndarray, index is defined
if not isinstance(data, (SingleBlockManager, SingleArrayManager)):
manager = get_option("mode.data_manager")
if manager == "block":
Expand All @@ -378,103 +377,99 @@ def __init__(
data = SingleArrayManager.from_array(data, index)
if copy:
data = data.copy()
if index is None:
Copy link
Member

Choose a reason for hiding this comment

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

Why is this if index is None: check no longer needed in the fastpath?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you follow the code all the way down to the end of the method you can see that this actually is a noop; It's superflous code and can be removed. It was just difficult to see in the old code.

Copy link
Member

Choose a reason for hiding this comment

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

Ah okay I see it now. Thanks

index = data.index

else:
# skips validation of the name
object.__setattr__(self, "_name", name)
NDFrame.__init__(self, data)
return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fastpath now ends here and everything below can simply be moved one level to the left.


name = ibase.maybe_extract_name(name, data, type(self))
name = ibase.maybe_extract_name(name, data, type(self))

if index is not None:
index = ensure_index(index)
if index is not None:
index = ensure_index(index)

if data is None:
data = {}
if dtype is not None:
dtype = self._validate_dtype(dtype)
if data is None:
data = {}
if dtype is not None:
dtype = self._validate_dtype(dtype)

if isinstance(data, MultiIndex):
raise NotImplementedError(
"initializing a Series from a MultiIndex is not supported"
)
if isinstance(data, Index):

if dtype is not None:
# astype copies
data = data.astype(dtype)
else:
# GH#24096 we need to ensure the index remains immutable
data = data._values.copy()
copy = False
if isinstance(data, MultiIndex):
raise NotImplementedError(
"initializing a Series from a MultiIndex is not supported"
)
if isinstance(data, Index):

elif isinstance(data, np.ndarray):
if len(data.dtype):
# GH#13296 we are dealing with a compound dtype, which
# should be treated as 2D
raise ValueError(
"Cannot construct a Series from an ndarray with "
"compound dtype. Use DataFrame instead."
)
elif isinstance(data, Series):
if index is None:
index = data.index
else:
data = data.reindex(index, copy=copy)
copy = False
data = data._mgr
elif is_dict_like(data):
data, index = self._init_dict(data, index, dtype)
dtype = None
copy = False
elif isinstance(data, (SingleBlockManager, SingleArrayManager)):
if index is None:
index = data.index
elif not data.index.equals(index) or copy:
# GH#19275 SingleBlockManager input should only be called
# internally
raise AssertionError(
"Cannot pass both SingleBlockManager "
"`data` argument and a different "
"`index` argument. `copy` must be False."
)

elif isinstance(data, ExtensionArray):
pass
if dtype is not None:
# astype copies
data = data.astype(dtype)
else:
data = com.maybe_iterable_to_list(data)
if is_list_like(data) and not len(data) and dtype is None:
# GH 29405: Pre-2.0, this defaulted to float.
dtype = np.dtype(object)

# GH#24096 we need to ensure the index remains immutable
data = data._values.copy()
copy = False

elif isinstance(data, np.ndarray):
if len(data.dtype):
# GH#13296 we are dealing with a compound dtype, which
# should be treated as 2D
raise ValueError(
"Cannot construct a Series from an ndarray with "
"compound dtype. Use DataFrame instead."
)
elif isinstance(data, Series):
if index is None:
if not is_list_like(data):
data = [data]
index = default_index(len(data))
elif is_list_like(data):
com.require_length_match(data, index)

# create/copy the manager
if isinstance(data, (SingleBlockManager, SingleArrayManager)):
if dtype is not None:
data = data.astype(dtype=dtype, errors="ignore", copy=copy)
elif copy:
data = data.copy()
index = data.index
else:
data = sanitize_array(data, index, dtype, copy)
data = data.reindex(index, copy=copy)
copy = False
data = data._mgr
elif is_dict_like(data):
data, index = self._init_dict(data, index, dtype)
dtype = None
copy = False
elif isinstance(data, (SingleBlockManager, SingleArrayManager)):
if index is None:
index = data.index
elif not data.index.equals(index) or copy:
# GH#19275 SingleBlockManager input should only be called
# internally
raise AssertionError(
"Cannot pass both SingleBlockManager "
"`data` argument and a different "
"`index` argument. `copy` must be False."
)

manager = get_option("mode.data_manager")
if manager == "block":
data = SingleBlockManager.from_array(data, index)
elif manager == "array":
data = SingleArrayManager.from_array(data, index)
elif isinstance(data, ExtensionArray):
pass
else:
data = com.maybe_iterable_to_list(data)
if is_list_like(data) and not len(data) and dtype is None:
# GH 29405: Pre-2.0, this defaulted to float.
dtype = np.dtype(object)

if index is None:
if not is_list_like(data):
data = [data]
index = default_index(len(data))
elif is_list_like(data):
com.require_length_match(data, index)

# create/copy the manager
if isinstance(data, (SingleBlockManager, SingleArrayManager)):
if dtype is not None:
data = data.astype(dtype=dtype, errors="ignore", copy=copy)
elif copy:
data = data.copy()
else:
data = sanitize_array(data, index, dtype, copy)

manager = get_option("mode.data_manager")
if manager == "block":
data = SingleBlockManager.from_array(data, index)
elif manager == "array":
data = SingleArrayManager.from_array(data, index)

NDFrame.__init__(self, data)
if fastpath:
# skips validation of the name
object.__setattr__(self, "_name", name)
else:
self.name = name
self._set_axis(0, index)
self.name = name
self._set_axis(0, index)

def _init_dict(
self, data, index: Index | None = None, dtype: DtypeObj | None = None
Expand Down