Skip to content
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

type DataFrame.assign #1176

Merged
merged 2 commits into from
Mar 18, 2025
Merged
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
5 changes: 5 additions & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -837,4 +837,9 @@ ExcelWriteEngine: TypeAlias = Literal["openpyxl", "odf", "xlsxwriter"]
# https://github.com/pandas-dev/pandas-stubs/pull/1151#issuecomment-2715130190
TimeZones: TypeAlias = str | tzinfo | None | int

# Evaluates to a DataFrame column in DataFrame.assign context.
IntoColumn: TypeAlias = (
AnyArrayLike | Scalar | Callable[[DataFrame], AnyArrayLike | Scalar]
)

__all__ = ["npt", "type_t"]
3 changes: 2 additions & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ from pandas._typing import (
InterpolateOptions,
IntervalClosedType,
IntervalT,
IntoColumn,
JoinHow,
JsonFrameOrient,
Label,
Expand Down Expand Up @@ -728,7 +729,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
value: Scalar | ListLikeU | None,
allow_duplicates: _bool = ...,
) -> None: ...
def assign(self, **kwargs) -> Self: ...
def assign(self, **kwargs: IntoColumn) -> Self: ...
def align(
self,
other: NDFrameT,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,38 @@ def test_types_assign() -> None:
df["col3"] = df.sum(axis=1)


def test_assign() -> None:
df = pd.DataFrame({"a": [1, 2, 3], 1: [4, 5, 6]})

my_unnamed_func = lambda df: df["a"] * 2

def my_named_func_1(df: pd.DataFrame) -> pd.Series[str]:
return df["a"]

def my_named_func_2(df: pd.DataFrame) -> pd.Series[Any]:
return df["a"]

check(assert_type(df.assign(c=lambda df: df["a"] * 2), pd.DataFrame), pd.DataFrame)
check(
assert_type(df.assign(c=lambda df: df["a"].index), pd.DataFrame), pd.DataFrame
)
check(
assert_type(df.assign(c=lambda df: df["a"].to_numpy()), pd.DataFrame),
pd.DataFrame,
)
check(
assert_type(df.assign(c=lambda df: df["a"].max()), pd.DataFrame),
pd.DataFrame,
)
check(assert_type(df.assign(c=df["a"] * 2), pd.DataFrame), pd.DataFrame)
check(assert_type(df.assign(c=df["a"].index), pd.DataFrame), pd.DataFrame)
check(assert_type(df.assign(c=df["a"].to_numpy()), pd.DataFrame), pd.DataFrame)
check(assert_type(df.assign(c=2), pd.DataFrame), pd.DataFrame)
check(assert_type(df.assign(c=my_unnamed_func), pd.DataFrame), pd.DataFrame)
check(assert_type(df.assign(c=my_named_func_1), pd.DataFrame), pd.DataFrame)
check(assert_type(df.assign(c=my_named_func_2), pd.DataFrame), pd.DataFrame)


def test_types_sample() -> None:
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
# GH 67
Expand Down