Home > Mobile >  using iloc to query a pandas dataframe, why pylance is complaining slice is "slice" is inc
using iloc to query a pandas dataframe, why pylance is complaining slice is "slice" is inc

Time:01-19

I am using iloc to slice my dataframe as below:

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

It is working fine but pylance in VsCode is complaining that:

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]"

Any idea why and what does it mean?

CodePudding user response:

I read it like this : "you put brackets so i call get_item, and inside should be an index (idx), and this index should be of type : int | Tuple[IndexType | MaskType, IndexType | MaskType | int] however, slice is compatible with none of it."

Have you tried replacing : with , ? it says he expect a Tuple of int, maybe that helps?

But you say it works? so it may be an issue with pylance, no?

== Edit ==

The answer is : df.iloc[1:,:]

  •  Tags:  
  • Related