Skip to content

GH1248/GH1249 DataFrame.assign and MultiIndex.from_product Series/Index #1250

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,12 @@ TimeZones: TypeAlias = str | tzinfo | None | int

# Evaluates to a DataFrame column in DataFrame.assign context.
IntoColumn: TypeAlias = (
AnyArrayLike | Scalar | Callable[[DataFrame], AnyArrayLike | Scalar] | None
AnyArrayLike
| Scalar
| Callable[[DataFrame], AnyArrayLike | Scalar | list[Scalar] | range]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
| Callable[[DataFrame], AnyArrayLike | Scalar | list[Scalar] | range]
| Callable[[DataFrame], AnyArrayLike | Scalar | list[Scalar] | range] |
list[Scalar] | range

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed

| list[Scalar]
| range
| None
)

DatetimeLike: TypeAlias = datetime.datetime | np.datetime64 | Timestamp
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/multi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MultiIndex(Index[Any]):
@classmethod
def from_product(
cls,
iterables: Sequence[SequenceNotStr[Hashable]],
iterables: Sequence[SequenceNotStr[Hashable] | pd.Series | pd.Index],
sortorder: int | None = ...,
names: SequenceNotStr[Hashable] = ...,
) -> Self: ...
Expand Down
23 changes: 21 additions & 2 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
)
import xarray as xr

from pandas._typing import Scalar
from pandas._typing import (
Scalar,
)

from tests import (
PD_LTE_23,
Expand Down Expand Up @@ -305,9 +307,26 @@ def test_types_head_tail() -> None:

def test_types_assign() -> None:
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
df.assign(col3=lambda frame: frame.sum(axis=1))

check(
assert_type(df.assign(col3=lambda frame: frame.sum(axis=1)), pd.DataFrame),
pd.DataFrame,
)
df["col3"] = df.sum(axis=1)

df = pd.DataFrame({"a": [1, 2, 3]})
check(
assert_type(
df.assign(b=lambda df: range(len(df)), c=lambda _: [10, 20, 30]),
pd.DataFrame,
),
pd.DataFrame,
)
check(
assert_type(df.assign(b=range(len(df)), c=[10, 20, 30]), pd.DataFrame),
pd.DataFrame,
)


def test_assign() -> None:
df = pd.DataFrame({"a": [1, 2, 3], 1: [4, 5, 6]})
Expand Down
14 changes: 14 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ def test_index_astype() -> None:
pd.DataFrame,
)

df = pd.DataFrame({"a": [1, 2, 3]})
check(
assert_type(
pd.MultiIndex.from_product([["x", "y"], df.columns]), pd.MultiIndex
),
pd.MultiIndex,
)
check(
assert_type(
pd.MultiIndex.from_product([["x", "y"], pd.Series([1, 2])]), pd.MultiIndex
),
pd.MultiIndex,
)


def test_multiindex_get_level_values() -> None:
mi = pd.MultiIndex.from_product([["a", "b"], ["c", "d"]], names=["ab", "cd"])
Expand Down