Skip to content

Allow None in RHS in df.loc.__setitem__  #382

@Dr-Irv

Description

@Dr-Irv

Describe the bug
Should we allow None in RHS of a __setitem__() in a DataFrame ?

On the one hand, people shouldn't be placing None as a value in a DataFrame.
On the other hand, it is accepted by pandas.

Would like opinion of @bashtage and @twoertwein on my question above.

To Reproduce

df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4], 5: [6, 7]})
df.loc[df["col1"] > 1, "col1"] = None

pyright reports (after adding this line to test_frame.py:

  c:\Code\pandas-stubs\tests\test_frame.py:174:5 - error: Argument of type "tuple[Series[bool], Literal['col1']]" cannot be assigned to parameter "idx" of type "tuple[_IndexSliceTuple[Unknown], HashableT@__setitem__]" in function "__setitem__"
    "tuple[Series[bool], Literal['col1']]" is incompatible with "tuple[_IndexSliceTuple[Unknown], HashableT@__setitem__]"
      Tuple entry 1 is incorrect type
        "Series[bool]" is incompatible with "_IndexSliceTuple[Unknown]" (reportGeneralTypeIssues)
  c:\Code\pandas-stubs\tests\test_frame.py:174:5 - error: Argument of type "None" cannot be assigned to parameter "value" of type "ArrayLike | S1@__setitem__ | Series[S1@__setitem__] | list[Unknown]" in function "__setitem__"
    Type "None" cannot be assigned to type "ArrayLike | S1@__setitem__ | Series[S1@__setitem__] | list[Unknown]"
      Type "None" is incompatible with constrained type variable "S1"
      Type "None" cannot be assigned to type "ExtensionArray"
      Type "None" cannot be assigned to type "ndarray[Unknown, Unknown]"
      Type "None" cannot be assigned to type "Series[S1@__setitem__]"
      Type "None" cannot be assigned to type "list[Unknown]" (reportGeneralTypeIssues)

Fix is to add None to value argument here:

value: S1 | ArrayLike | Series | DataFrame,

and in other places with __setitem__() declarations in core/frame.pyi and core/series.pyi

Please complete the following information:

  • OS: Windows 10
  • python version: 3.10
  • version of type checker: pyright 1.1.273
  • version of installed pandas-stubs 1.5.0.221010

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions