-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
TYP: internals.construction #40154
TYP: internals.construction #40154
Changes from 10 commits
175e7a3
971da88
de8808e
071effc
14b7d21
26fe41e
bb8cd94
5131de7
ab0bef0
422f3f6
74be33c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ | |
if TYPE_CHECKING: | ||
from numpy.ma.mrecords import MaskedRecords | ||
|
||
|
||
# --------------------------------------------------------------------- | ||
# BlockManager Interface | ||
|
||
|
@@ -91,7 +92,7 @@ def arrays_to_mgr( | |
dtype: Optional[DtypeObj] = None, | ||
verify_integrity: bool = True, | ||
typ: Optional[str] = None, | ||
): | ||
) -> Union[BlockManager, ArrayManager]: | ||
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. There is a typing alias for this union ( 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. mind keeping this for a follow-up? id like to get the CI back to green 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. CI checks are also still failing on this PR |
||
""" | ||
Segregate Series based on type and coerce into matrices. | ||
|
||
|
@@ -109,11 +110,11 @@ def arrays_to_mgr( | |
# don't force copy because getting jammed in an ndarray anyway | ||
arrays = _homogenize(arrays, index, dtype) | ||
|
||
columns = ensure_index(columns) | ||
else: | ||
columns = ensure_index(columns) | ||
index = ensure_index(index) | ||
|
||
columns = ensure_index(columns) | ||
|
||
# from BlockManager perspective | ||
axes = [columns, index] | ||
|
||
|
@@ -140,9 +141,8 @@ def rec_array_to_mgr( | |
fdata = ma.getdata(data) | ||
if index is None: | ||
index = _get_names_from_index(fdata) | ||
if index is None: | ||
index = ibase.default_index(len(data)) | ||
index = ensure_index(index) | ||
else: | ||
index = ensure_index(index) | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if columns is not None: | ||
columns = ensure_index(columns) | ||
|
@@ -215,14 +215,14 @@ def mgr_to_mgr(mgr, typ: str): | |
|
||
def ndarray_to_mgr( | ||
values, index, columns, dtype: Optional[DtypeObj], copy: bool, typ: str | ||
): | ||
) -> Union[BlockManager, ArrayManager]: | ||
# used in DataFrame.__init__ | ||
# input must be a ndarray, list, Series, index | ||
# input must be a ndarray, list, Series, Index, ExtensionArray | ||
|
||
if isinstance(values, ABCSeries): | ||
if columns is None: | ||
if values.name is not None: | ||
columns = [values.name] | ||
columns = Index([values.name]) | ||
if index is None: | ||
index = values.index | ||
else: | ||
|
@@ -309,7 +309,9 @@ def ndarray_to_mgr( | |
return create_block_manager_from_blocks(block_values, [columns, index]) | ||
|
||
|
||
def dict_to_mgr(data: Dict, index, columns, dtype: Optional[DtypeObj], typ: str): | ||
def dict_to_mgr( | ||
data: Dict, index, columns, dtype: Optional[DtypeObj], typ: str | ||
) -> Union[BlockManager, ArrayManager]: | ||
""" | ||
Segregate Series based on type and coerce into matrices. | ||
Needs to handle a lot of exceptional cases. | ||
|
@@ -531,21 +533,18 @@ def extract_index(data) -> Index: | |
return ensure_index(index) | ||
|
||
|
||
def reorder_arrays(arrays, arr_columns, columns): | ||
def reorder_arrays( | ||
arrays: List[ArrayLike], arr_columns: Index, columns: Optional[Index] | ||
) -> Tuple[List[ArrayLike], Index]: | ||
# reorder according to the columns | ||
if ( | ||
columns is not None | ||
and len(columns) | ||
and arr_columns is not None | ||
and len(arr_columns) | ||
): | ||
if columns is not None and len(columns) and len(arr_columns): | ||
indexer = ensure_index(arr_columns).get_indexer(columns) | ||
arr_columns = ensure_index([arr_columns[i] for i in indexer]) | ||
arrays = [arrays[i] for i in indexer] | ||
return arrays, arr_columns | ||
|
||
|
||
def _get_names_from_index(data): | ||
def _get_names_from_index(data) -> Index: | ||
has_some_name = any(getattr(s, "name", None) is not None for s in data) | ||
if not has_some_name: | ||
return ibase.default_index(len(data)) | ||
|
@@ -560,7 +559,7 @@ def _get_names_from_index(data): | |
index[i] = f"Unnamed {count}" | ||
count += 1 | ||
|
||
return index | ||
return Index(index) | ||
|
||
|
||
def _get_axes( | ||
|
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.
This is ensured by
to_arrays
already?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.
yes