datacollect icon indicating copy to clipboard operation
datacollect copied to clipboard

Issue : Incorrect pandas indexing with `.ix`

Open PrinceSajjadHussain opened this issue 7 months ago • 1 comments

The code uses .ix for indexing in pandas DataFrames, which is deprecated. It should be replaced with .loc or .iloc depending on whether label-based or integer-based indexing is intended. Using .loc is generally preferred when dealing with labels.

PrinceSajjadHussain avatar Jun 22 '25 18:06 PrinceSajjadHussain

Hi! 👋

Just a heads-up: the code is currently using .ix for indexing pandas DataFrames, which has been deprecated since pandas 0.20.0 and removed in later versions.

To ensure compatibility with modern versions of pandas:

  • Use .loc[] for label-based indexing
  • Use .iloc[] for integer-based indexing

Old (deprecated)

df.ix[2, 'column_name']

Replace with either:

df.loc[2, 'column_name'] # if 2 is an index label df.iloc[2, df.columns.get_loc('column_name')] # if 2 is a row number

df = pd.DataFrame({"A": [1, 2, 3]}, index=[100, 101, 102]) df.loc[101] # Valid: 101 is a label

df = pd.DataFrame({"A": [1, 2, 3]}, index=["a", "b", "c"]) df.loc[1] # ❌ KeyError, because "1" is not a label

df = pd.DataFrame({"A": [1, 2, 3]}, index=["a", "b", "c"]) df.loc["b"] # Valid

buildwithmobi avatar Jul 14 '25 05:07 buildwithmobi