2

I have simple Pandas Dataframe:

data.head(5)


> Date  Time    Open    High    Low Close   Vol OI
> 0 02/02/1993  16:00   44.23   44.38   44.13   44.34   201300  0
> 1 02/03/1993  16:00   44.41   44.84   44.38   44.82   529400  0
> 2 02/04/1993  16:00   44.97   45.10   44.88   45.00   531500  0
> 3 02/05/1993  16:00   44.97   45.06   44.73   44.97   492100  0
> 4 02/08/1993  16:00   44.97   45.13   44.92   44.98   596100  0

I want to set Column "Date" as index:

data.set_index('Date')

But get error "TypeError: 'list' object is not callable"

TypeError                                 Traceback (most recent call last)
<ipython-input-59-a610da45b82c> in <module>()
----> 1 data.set_index('Date')

TypeError: 'list' object is not callable

Currently the index in data is RangeIndex:

data.index
RangeIndex(start=0, stop=5873, step=1)

Any tip why I cannot set the index using Date column?

Thank you.

2
  • What does data['Date'].iloc[0] show? can you post also data.info() output also data.columns.tolist() Commented May 27, 2016 at 10:56
  • Please include the full traceback in cases like these. Commented May 27, 2016 at 11:02

4 Answers 4

3

I got the same issue of "TypeError: 'list' object is not callable" when using the set_index command. I got the solution by first calling 'reindex()' method and then using set_index. Hope it works for you. Aamir

Sign up to request clarification or add additional context in comments.

Comments

1

I think you have to add parameter inplace=True to set_index:

data.set_index('Date', inplace=True)

Another solution is:

data = data.set_index('Date')

Sample:

import pandas as pd

data = pd.DataFrame({'High': {0: 44.380000000000003, 1: 44.840000000000003, 2: 45.100000000000001, 3: 45.060000000000002, 4: 45.130000000000003}, 'Vol': {0: 201300, 1: 529400, 2: 531500, 3: 492100, 4: 596100}, 'Close': {0: 44.340000000000003, 1: 44.82, 2: 45.0, 3: 44.969999999999999, 4: 44.979999999999997}, 'Date': {0: '02/02/1993', 1: '02/03/1993', 2: '02/04/1993', 3: '02/05/1993', 4: '02/08/1993'}, 'Open': {0: 44.229999999999997, 1: 44.409999999999997, 2: 44.969999999999999, 3: 44.969999999999999, 4: 44.969999999999999}, 'Time': {0: '16:00', 1: '16:00', 2: '16:00', 3: '16:00', 4: '16:00'}, 'Low': {0: 44.130000000000003, 1: 44.380000000000003, 2: 44.880000000000003, 3: 44.729999999999997, 4: 44.920000000000002}, 'OI': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}})
print (data)

#   Close        Date   High    Low  OI   Open   Time     Vol
#0  44.34  02/02/1993  44.38  44.13   0  44.23  16:00  201300
#1  44.82  02/03/1993  44.84  44.38   0  44.41  16:00  529400
#2  45.00  02/04/1993  45.10  44.88   0  44.97  16:00  531500
#3  44.97  02/05/1993  45.06  44.73   0  44.97  16:00  492100
#4  44.98  02/08/1993  45.13  44.92   0  44.97  16:00  596100

print (data.columns)
#Index(['Close', 'Date', 'High', 'Low', 'OI', 'Open', 'Time', 'Vol'], dtype='object')


data.set_index('Date', inplace=True)
print (data)

#            Close   High    Low  OI   Open   Time     Vol
#Date                                                     
#02/02/1993  44.34  44.38  44.13   0  44.23  16:00  201300
#02/03/1993  44.82  44.84  44.38   0  44.41  16:00  529400
#02/04/1993  45.00  45.10  44.88   0  44.97  16:00  531500
#02/05/1993  44.97  45.06  44.73   0  44.97  16:00  492100
#02/08/1993  44.98  45.13  44.92   0  44.97  16:00  596100

If need set index and convert to datetime in read_csv use (if separator is , you can omit it, because by default sep=','):

import pandas as pd
import io

temp=u"""Date;Time;Open;High;Low;Close;Vol;OI
02/02/1993;16:00;44.23;44.38;44.13;44.34;201300;0
02/03/1993;16:00;44.41;44.84;44.38;44.82;529400;0
02/04/1993;16:00;44.97;45.10;44.88;45.00;531500;0
02/05/1993;16:00;44.97;45.06;44.73;44.97;492100;0
02/08/1993;16:00;44.97;45.13;44.92;44.98;596100;0"""
#after testing replace io.StringIO(temp) to filename
data = pd.read_csv(io.StringIO(temp), sep=";", index_col='Date', parse_dates=['Date'])
print (data)

             Time   Open   High    Low  Close     Vol  OI
Date                                                     
1993-02-02  16:00  44.23  44.38  44.13  44.34  201300   0
1993-02-03  16:00  44.41  44.84  44.38  44.82  529400   0
1993-02-04  16:00  44.97  45.10  44.88  45.00  531500   0
1993-02-05  16:00  44.97  45.06  44.73  44.97  492100   0
1993-02-08  16:00  44.97  45.13  44.92  44.98  596100   0

11 Comments

This doesnot help in this case. Still getting "TypeError: 'list' object is not callable"
Ok, what is print (data.columns) ?
And what is your pandas version? print (pd.show_versions()) ?
data['Date'].iloc[0] shows '02/02/1993' data.info()is:<class 'pandas.core.frame.DataFrame'> RangeIndex: 5873 entries, 0 to 5872 Data columns (total 8 columns): Date 5873 non-null object Time 5873 non-null object Open 5873 non-null float64 High 5873 non-null float64 Low 5873 non-null float64 Close 5873 non-null float64 Vol 5873 non-null int64 OI 5873 non-null int64 dtypes: float64(4), int64(2), object(2) memory usage: 367.1+ KB
Very interesting. If convert to datetime column Date by data['Date'] = pd.to_datetime(data['Date']) before set_index in your real data, it work?
|
0

Check if you have used .to_list() method in the code I used same method and was getting error again and again. So make sure to use set_index method before it.

Comments

0

I was having the same issue and I tried the solutions of the other answers but they wouldn't work for me. (re-indexing, setting in-place=true, or assigning to another variable)

I was using jupyter-lab, and what did work for me was restarting all the variables from the kernel. Maybe if you were also using notebooks, that'd fix it for you too.

If i had to guess, my index somehow got corrupted along my attempts, and thats why resetting the kernel fixed it, and it might be the same for you.

Leaving it here for future reference, hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.