Skip to content

Use_iloc

Sourcery rule id: use_iloc

Description

Use the .iloc attribute for index-based selection

Before

df: pandas.DataFrame
data = df[1]

After

df: pandas.DataFrame
data = df.iloc[1]

Explanation

Using 'iloc' is optimized for selecting data in Pandas based on index position (https://pandas.pydata.org/pandas-docs/stable/user_guide/10min.html#selection). This rule replaces the generic indexing with 'iloc' for index-based selection.

Before:

data = df[5]

After:

data = df.iloc[5]