Skip to content

TYP: remove mypy ignore[assignment] from pandas/core/internals/construction.py #53135

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 3 commits into from
May 12, 2023
Merged
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Any,
Hashable,
Sequence,
cast,
)

import numpy as np
Expand Down Expand Up @@ -671,6 +672,8 @@ def reorder_arrays(
"""
Pre-emptively (cheaply) reindex arrays with new columns.
"""
from pandas._typing import ArrayLike

# reorder according to the columns
if columns is not None:
if not columns.equals(arr_columns):
Expand All @@ -687,10 +690,7 @@ def reorder_arrays(
arr = arrays[k]
new_arrays[i] = arr

# Incompatible types in assignment (expression has type
# "List[Union[ExtensionArray, ndarray[Any, Any], None]]", variable
# has type "List[Union[ExtensionArray, ndarray[Any, Any]]]")
arrays = new_arrays # type: ignore[assignment]
arrays = cast(list[ArrayLike], new_arrays)
Copy link
Member

@MarcoGorelli MarcoGorelli May 11, 2023

Choose a reason for hiding this comment

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

is it possible to refactor instead of casting? it might be possible to be more precise here

something like

            new_arrays: list[ArrayLike] = []
            indexer = arr_columns.get_indexer(columns)
            for i, k in enumerate(indexer):
                if k == -1:
                    # by convention default is all-NaN object dtype
                    arr = np.empty(length, dtype=object)
                    arr.fill(np.nan)
                else:
                    arr = arrays[k]
                new_arrays.append(arr)

then the cast might not be necessary? haven't tried running this (currently on a train 🚋), but hoping this might work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I did as you suggested. It works very well.

arr_columns = columns

return arrays, arr_columns
Expand Down