115

I am getting this error when I try to use the .ix attribute of a pandas data frame to pull out a column, e.g. df.ix[:, 'col_header'].

AttributeError: 'DataFrame' object has no attribute 'ix'

The script worked this morning, but this afternoon I ran it in a new Linux environment with a fresh install of Pandas. Has anybody else seen this error before? I've searched here and elsewhere but can't find it.

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
Diarmid Roberts
  • 1,251
  • 2
  • 6
  • 4
  • 6
    You were running an older version of pandas. See this? https://stackoverflow.com/questions/43838999/pandas-replacement-for-ix – StupidWolf Jan 30 '20 at 17:45

16 Answers16

110

try df.iloc[:, integer]

.ix is deprecated

By the way, df.loc[:,'col_header'] is for str or Boolean indexing

Code42
  • 2,292
  • 1
  • 17
  • 22
  • I receive "Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types" for that. – Ben May 15 '20 at 08:01
  • "*By the way, df.loc[:,'col_header'] is for str indexing*" and will be required if the indexer was a Boolean mask. – mins Dec 15 '20 at 11:04
  • 3
    *"`.ix` is deprecated"* - `.ix` was removed, not deprecated (which means discouraged but still available). – wisbucky Jul 21 '21 at 18:28
52

Change .ix to .loc and it should work correctly.

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
Abidi Mohamed
  • 723
  • 7
  • 7
27

A fresh install today (Jan 30, 2020) would install pd.__version__ == '1.0.0'. With that comes a removal of many deprecated features.

Removed Series.ix and DataFrame.ix (GH26438)

ALollz
  • 57,915
  • 7
  • 66
  • 89
4

Try following steps: 1) installing new version of Pandas 2) use .loc instead of .ix

Sau
  • 99
  • 5
3

had same issue with pandas 1.0.0, this worked for me

Open Anaconda Prompt (cmd) as Administrator, then

conda install pandas==0.25.1

Your newer pandas version will be overwritten by older one!

Eric Stralsund
  • 541
  • 1
  • 5
  • 17
3

it works for me

Use df.loc[] instade of ix[]

Dhiren Biren
  • 472
  • 4
  • 7
2

as ix is removed

use iloc or loc inplace of ix.

use .loc if you have string or userdefined indexing.

Luicfer Ai
  • 56
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 09 '22 at 12:06
  • "as ix is removed" What does this mean exactly? – ryanwebjackson Mar 11 '22 at 20:03
  • Ix is now not available to be used in pandas. You have to use .iloc Or loc these will give you same result. – Luicfer Ai Mar 13 '22 at 03:27
1

I used .loc() instead of .ix() and it worked.

1

replace .ix with .iloc after replacing its works well for me also

predictions_ARIMA_log = pd.Series(ts_log.iloc[0], index=ts_log.index)

1

If you need to use .ix[], you must downgrade your pandas version to 0.20.0. In newer versions, you could use .loc[] or .iloc[] instead.

Kyle F Hartzenberg
  • 2,567
  • 3
  • 6
  • 24
Abolfazl
  • 637
  • 4
  • 7
0

one column:

df[['sepal width']]

two columns:

df[['sepal width','petal width']]

special columns(select column include 'length'):

df[[c for c in df.columns if 'length' in c]]
HelloNewWorld
  • 335
  • 3
  • 11
0

I am reading the book 'Python for data analysis' by Wes McKinney and I met the same problem of Dataframe.ix[] while retrieving the rows with index. I replace ix by iloc and it works perfectly.

Avicii
  • 98
  • 1
  • 8
0

I'm using .ix as I have mixed indexing, labels and integers. .loc() does not solve the issue as well as .iloc; both are ending in errors. I was intentionally using .ix because it was the fast lane when the index is a mix of integers and labels.

As example a df like:

enter image description here

My way out is to back-up columns and index, replace with integers, use .iat and then restore the df as it was at the beginning. I have something like:

# Save the df and replace indec and columns with integers
lista_colonne = list(df.columns)  
df.columns = range(0,len(lista_colonne))    
nome_indice = df.index.name
lista_indice = list(df.index)
df['Indice'] = range(0,len(lista_indice))
df.index = df['Indice']
del df['Indice']

  ... indexing here with .iat in place of .ix


# Now back as it was
df.columns = lista_colonne
df['Indice'] = lista_indice
df.index = df['Indice']
del df['Indice']
df.index.name = nome_indice

Bye, Fabio.

Fabio Pomi
  • 317
  • 1
  • 3
  • 9
0

I had to do this:

returns.ix['2015-01-01':'2015-12-31'].std()

After much ado I made it happen using this:

returns.xs(key='2015',axis=0).std()

I believe at least for this case we can use cross section and filter using 2015 as key.

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
0

Yes, that's right. Replace df.ix[] with df.iloc[] or df.loc[]

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
Pravu Chaudhary
  • 101
  • 1
  • 4
-2
  1. Guys try to update current pandas
  2. replace .ix with .iloc after replacing its works well for me For details refers documentations
SIVA001
  • 17
  • 2