Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataFrame.set_index returns 'str' object is not callable

Tags:

python

pandas

I'm not looking for a solution here as I found a workaround; mostly I'd just like to understand why my original approach didn't work given that the work around did.

I have a dataframe of 2803 rows with the default numeric key. I want to replace that with the values in column 0, namely TKR.

So I use f.set_index('TKR') and get

f.set_index('TKR')

Traceback (most recent call last):

  File "<ipython-input-4-39232ca70c3d>", line 1, in <module>
    f.set_index('TKR')

TypeError: 'str' object is not callable

So I think maybe there's some noise in my TKR column and rather than scrolling through 2803 rows I try f.head().set_index('TKR')

When that works I try f.head(100).set_index('TKR') which also works. I continue with parameters of 500, 1000, and 1500 all of which work. So do 2800, 2801, 2802 and 2803. Finally I settle on

f.head(len(f)).set_index('TKR')

which works and will handle a different size dataframe next month. I would just like to understand why this works and the original, simpler, and (I thought) by the book method doesn't.

I'm using Python 3.6 (64 bit) and Pandas 0.18.1 on a Windows 10 machine

like image 332
dborger Avatar asked Jun 16 '17 15:06

dborger


1 Answers

You might have accidentally assigned the pd.DataFrame.set_index() to a value.

example of this mistake: f.set_index = 'intended_col_name'

As a result for the rest of your code .set_index was changed into a str, which is not callable, resulting in this error.

Try restarting your notebook, remove the wrong code and replace it with f.set_index('TKR')

like image 200
TZH Avatar answered Sep 30 '22 14:09

TZH