-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": | ||
|
@@ -378,103 +377,99 @@ def __init__( | |
data = SingleArrayManager.from_array(data, index) | ||
if copy: | ||
data = data.copy() | ||
if index is None: | ||
index = data.index | ||
|
||
else: | ||
# skips validation of the name | ||
object.__setattr__(self, "_name", name) | ||
NDFrame.__init__(self, data) | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thefastpath
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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