使用 iloc 查询 pandas 数据帧,为什么 pylance 抱怨切片 "slice" 与 "int" 不兼容?

using iloc to query a pandas dataframe, why pylance is complaining slice is "slice" is incompatible with "int"?

我正在使用 iloc 对我的数据帧进行切片,如下所示:

df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
new_df = df.iloc[1:]

它工作正常,但 VsCode 中的 pylance 抱怨:

Argument of type "slice" cannot be assigned to parameter "idx" of type "int | Tuple[IndexType | MaskType, IndexType | MaskType | int]" in function "__getitem__"
  Type "slice" cannot be assigned to type "int | Tuple[IndexType | MaskType, IndexType | MaskType | int]"
    "slice" is incompatible with "int"
    "slice" is incompatible with "Tuple[IndexType | MaskType, IndexType | MaskType | int]"

知道这是什么意思吗?

我是这样读的: “你放了括号所以我调用 get_item,里面应该是一个索引 (idx),这个索引应该是类型:int | Tuple[IndexType | MaskType, IndexType | MaskType | int] 但是,slice 是兼容的其中 none 个。"

您是否尝试过将 : 替换为 , ?它说他期待一个整数元组,也许这有帮助?

但是你说有用吗?所以这可能是 pylance 的问题,不是吗?

==编辑==

答案是:df.iloc[1:,:]