-
-
Notifications
You must be signed in to change notification settings - Fork 157
TYP: DataFrame.__setitem__ with None
#1550
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
746c760
setitem with none
cmp0xff 7308eb9
more tests and typings
cmp0xff fe2890b
Merge branch 'main' of github.com:pandas-dev/pandas-stubs into featur…
cmp0xff 4682c1a
python/mypy#20420 https://github.com/pandas-dev/pandas-stubs/pull/155…
cmp0xff 3f139e5
Merge branch 'main' of github.com:pandas-dev/pandas-stubs into featur…
cmp0xff 9bfe743
Merge branch 'main' of github.com:pandas-dev/pandas-stubs into featur…
cmp0xff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,11 @@ def test_types_setitem() -> None: | |
| df[a] = [[1, 2], [3, 4]] | ||
| df[i] = [8, 9] | ||
|
|
||
| df["col1"] = [None, pd.NaT] | ||
cmp0xff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # TODO: mypy bug, remove after python/mypy#20420 has been resolved | ||
| df[["col1"]] = [[None], [pd.NA]] # type: ignore[assignment,list-item] | ||
| df[iter(["col1"])] = [[None], [pd.NA]] # type: ignore[assignment] | ||
|
Comment on lines
+98
to
+99
Contributor
Author
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. Not too sure why they don't work for
Contributor
Author
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. |
||
|
|
||
|
|
||
| def test_types_setitem_mask() -> None: | ||
| df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4], 5: [6, 7]}) | ||
|
|
@@ -368,17 +373,6 @@ def test_isetframe() -> None: | |
| check(assert_type(frame.isetitem([0], [10, 12]), None), type(None)) | ||
|
|
||
|
|
||
| def test_setitem_none() -> None: | ||
| df = pd.DataFrame( | ||
| {"A": [1, 2, 3], "B": ["abc", "def", "ghi"]}, index=["x", "y", "z"] | ||
| ) | ||
| df.loc["x", "B"] = None | ||
| df.iloc[2, 0] = None | ||
cmp0xff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sb = pd.Series([1, 2, 3], dtype=int) | ||
| sb.loc["y"] = None | ||
| sb.iloc[0] = None | ||
cmp0xff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_getsetitem_multiindex() -> None: | ||
| # GH 466 | ||
| rows = pd.Index(["project A", "project B", "project C"]) | ||
|
|
@@ -418,12 +412,32 @@ def test_frame_setitem_na() -> None: | |
|
|
||
| df.loc[ind, :] = pd.NA | ||
| df.iloc[[0, 2], :] = pd.NA | ||
| df.at["a", "x"] = pd.NA | ||
| df.iat[0, 0] = pd.NA | ||
|
|
||
| # reveal_type(df["y"]) gives Series[Any], so we have to cast to tell the | ||
| # type checker what kind of type it is when adding to a Timedelta | ||
| df["x"] = cast("pd.Series[pd.Timestamp]", df["y"]) + pd.Timedelta(days=3) | ||
| df.loc[ind, :] = pd.NaT | ||
| df.iloc[[0, 2], :] = pd.NaT | ||
| df.at["a", "y"] = pd.NaT | ||
| df.iat[0, 0] = pd.NaT | ||
|
|
||
| df.loc["a", "x"] = None | ||
| df.iloc[2, 0] = None | ||
cmp0xff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| df.at["a", "y"] = None | ||
| df.iat[0, 0] = None | ||
|
|
||
| df.loc[:, "x"] = [None, pd.NA, pd.NaT] | ||
| df.iloc[:, 0] = [None, pd.NA, pd.NaT] | ||
|
|
||
| # TODO: mypy bug, remove after python/mypy#20420 has been resolved | ||
| df.loc[:, ["x"]] = [[None], [pd.NA], [pd.NaT]] # type: ignore[assignment,index] | ||
| df.iloc[:, [0]] = [[None], [pd.NA], [pd.NaT]] # type: ignore[assignment,index] | ||
|
|
||
| # TODO: mypy bug, remove after python/mypy#20420 has been resolved | ||
| df.loc[:, iter(["x"])] = [[None], [pd.NA], [pd.NaT]] # type: ignore[assignment,index] | ||
| df.iloc[:, iter([0])] = [[None], [pd.NA], [pd.NaT]] # type: ignore[assignment,index] | ||
|
|
||
|
|
||
| def test_loc_set() -> None: | ||
|
|
@@ -574,6 +588,9 @@ def test_df_loc_dict() -> None: | |
| df.iloc[0] = {"X": 0} | ||
| check(assert_type(df, pd.DataFrame), pd.DataFrame) | ||
|
|
||
| df.loc[0] = {None: None, pd.NA: pd.NA, pd.NaT: pd.NaT} | ||
| df.iloc[0] = {None: None, pd.NA: pd.NA, pd.NaT: pd.NaT} | ||
cmp0xff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_iloc_npint() -> None: | ||
| # GH 69 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.