Skip to content

Commit 8e0aa38

Browse files
topper-123Terji Petersen
authored andcommitted
CLN: collect fastpath in Series.__init__ (pandas-dev#49575)
* CLN: collect fastpath in Series.__init__ * small clean Co-authored-by: Terji Petersen <[email protected]>
1 parent 9adaf8c commit 8e0aa38

File tree

1 file changed

+83
-88
lines changed

1 file changed

+83
-88
lines changed

pandas/core/series.py

+83-88
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ def __init__(
368368

369369
# we are called internally, so short-circuit
370370
if fastpath:
371-
372-
# data is an ndarray, index is defined
371+
# data is a ndarray, index is defined
373372
if not isinstance(data, (SingleBlockManager, SingleArrayManager)):
374373
manager = get_option("mode.data_manager")
375374
if manager == "block":
@@ -378,103 +377,99 @@ def __init__(
378377
data = SingleArrayManager.from_array(data, index)
379378
if copy:
380379
data = data.copy()
381-
if index is None:
382-
index = data.index
383-
384-
else:
380+
# skips validation of the name
381+
object.__setattr__(self, "_name", name)
382+
NDFrame.__init__(self, data)
383+
return
385384

386-
name = ibase.maybe_extract_name(name, data, type(self))
385+
name = ibase.maybe_extract_name(name, data, type(self))
387386

388-
if index is not None:
389-
index = ensure_index(index)
387+
if index is not None:
388+
index = ensure_index(index)
390389

391-
if data is None:
392-
data = {}
393-
if dtype is not None:
394-
dtype = self._validate_dtype(dtype)
390+
if data is None:
391+
data = {}
392+
if dtype is not None:
393+
dtype = self._validate_dtype(dtype)
395394

396-
if isinstance(data, MultiIndex):
397-
raise NotImplementedError(
398-
"initializing a Series from a MultiIndex is not supported"
399-
)
400-
if isinstance(data, Index):
401-
402-
if dtype is not None:
403-
# astype copies
404-
data = data.astype(dtype)
405-
else:
406-
# GH#24096 we need to ensure the index remains immutable
407-
data = data._values.copy()
408-
copy = False
395+
if isinstance(data, MultiIndex):
396+
raise NotImplementedError(
397+
"initializing a Series from a MultiIndex is not supported"
398+
)
399+
if isinstance(data, Index):
409400

410-
elif isinstance(data, np.ndarray):
411-
if len(data.dtype):
412-
# GH#13296 we are dealing with a compound dtype, which
413-
# should be treated as 2D
414-
raise ValueError(
415-
"Cannot construct a Series from an ndarray with "
416-
"compound dtype. Use DataFrame instead."
417-
)
418-
elif isinstance(data, Series):
419-
if index is None:
420-
index = data.index
421-
else:
422-
data = data.reindex(index, copy=copy)
423-
copy = False
424-
data = data._mgr
425-
elif is_dict_like(data):
426-
data, index = self._init_dict(data, index, dtype)
427-
dtype = None
428-
copy = False
429-
elif isinstance(data, (SingleBlockManager, SingleArrayManager)):
430-
if index is None:
431-
index = data.index
432-
elif not data.index.equals(index) or copy:
433-
# GH#19275 SingleBlockManager input should only be called
434-
# internally
435-
raise AssertionError(
436-
"Cannot pass both SingleBlockManager "
437-
"`data` argument and a different "
438-
"`index` argument. `copy` must be False."
439-
)
440-
441-
elif isinstance(data, ExtensionArray):
442-
pass
401+
if dtype is not None:
402+
# astype copies
403+
data = data.astype(dtype)
443404
else:
444-
data = com.maybe_iterable_to_list(data)
445-
if is_list_like(data) and not len(data) and dtype is None:
446-
# GH 29405: Pre-2.0, this defaulted to float.
447-
dtype = np.dtype(object)
448-
405+
# GH#24096 we need to ensure the index remains immutable
406+
data = data._values.copy()
407+
copy = False
408+
409+
elif isinstance(data, np.ndarray):
410+
if len(data.dtype):
411+
# GH#13296 we are dealing with a compound dtype, which
412+
# should be treated as 2D
413+
raise ValueError(
414+
"Cannot construct a Series from an ndarray with "
415+
"compound dtype. Use DataFrame instead."
416+
)
417+
elif isinstance(data, Series):
449418
if index is None:
450-
if not is_list_like(data):
451-
data = [data]
452-
index = default_index(len(data))
453-
elif is_list_like(data):
454-
com.require_length_match(data, index)
455-
456-
# create/copy the manager
457-
if isinstance(data, (SingleBlockManager, SingleArrayManager)):
458-
if dtype is not None:
459-
data = data.astype(dtype=dtype, errors="ignore", copy=copy)
460-
elif copy:
461-
data = data.copy()
419+
index = data.index
462420
else:
463-
data = sanitize_array(data, index, dtype, copy)
421+
data = data.reindex(index, copy=copy)
422+
copy = False
423+
data = data._mgr
424+
elif is_dict_like(data):
425+
data, index = self._init_dict(data, index, dtype)
426+
dtype = None
427+
copy = False
428+
elif isinstance(data, (SingleBlockManager, SingleArrayManager)):
429+
if index is None:
430+
index = data.index
431+
elif not data.index.equals(index) or copy:
432+
# GH#19275 SingleBlockManager input should only be called
433+
# internally
434+
raise AssertionError(
435+
"Cannot pass both SingleBlockManager "
436+
"`data` argument and a different "
437+
"`index` argument. `copy` must be False."
438+
)
464439

465-
manager = get_option("mode.data_manager")
466-
if manager == "block":
467-
data = SingleBlockManager.from_array(data, index)
468-
elif manager == "array":
469-
data = SingleArrayManager.from_array(data, index)
440+
elif isinstance(data, ExtensionArray):
441+
pass
442+
else:
443+
data = com.maybe_iterable_to_list(data)
444+
if is_list_like(data) and not len(data) and dtype is None:
445+
# GH 29405: Pre-2.0, this defaulted to float.
446+
dtype = np.dtype(object)
447+
448+
if index is None:
449+
if not is_list_like(data):
450+
data = [data]
451+
index = default_index(len(data))
452+
elif is_list_like(data):
453+
com.require_length_match(data, index)
454+
455+
# create/copy the manager
456+
if isinstance(data, (SingleBlockManager, SingleArrayManager)):
457+
if dtype is not None:
458+
data = data.astype(dtype=dtype, errors="ignore", copy=copy)
459+
elif copy:
460+
data = data.copy()
461+
else:
462+
data = sanitize_array(data, index, dtype, copy)
463+
464+
manager = get_option("mode.data_manager")
465+
if manager == "block":
466+
data = SingleBlockManager.from_array(data, index)
467+
elif manager == "array":
468+
data = SingleArrayManager.from_array(data, index)
470469

471470
NDFrame.__init__(self, data)
472-
if fastpath:
473-
# skips validation of the name
474-
object.__setattr__(self, "_name", name)
475-
else:
476-
self.name = name
477-
self._set_axis(0, index)
471+
self.name = name
472+
self._set_axis(0, index)
478473

479474
def _init_dict(
480475
self, data, index: Index | None = None, dtype: DtypeObj | None = None

0 commit comments

Comments
 (0)