DataFrame.set_index returns 'str' object not callable

I am not looking for a solution here as I found a workaround; basically I just wanted to understand why my original approach didn't work, given that the job was done.

I have a data block of 2803 lines with a default numeric key. I want to replace this with the values ​​in column 0, namely TKR.

So I use f.set_index('TKR')

and get

f.set_index ('TKP')

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 is some noise in my TKR column and instead of scrolling through 2803 lines I am trying f.head().set_index('TKR')

When it works I try f.head(100).set_index('TKR')

, which also works. I continue to work with parameters 500, 1000 and 1500. This is how 2800, 2801, 2802 and 2803 do. Finally, I stop at

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

which is working and will handle a different data format next month. I just would like to understand why this works, while the original, simpler and (I thought) book method does not.

I am using Python 3.6 (64 bit) and Pandas 0.18.1 on Windows 10 machine

+5


source to share


2 answers


I know it was a long time ago, but I think some people may need an answer in the future.

What you are doing with f.set_index('TKR')

is perfectly correct if "TKR" is the column of DataFrame f.



That is, this is a bug that you shouldn't have. This is always because you are overriding some built-in python functional methods or functions in your previous steps (probably 'set_index'). So the way to fix this is to check your code to figure out which part is wrong.

If you are using a Jupiter notebook, restart it and start this block just to fix this problem.

+1


source


You may have accidentally assigned pd.DataFrame.set_index () a value.

an example of this error: f.set_index = 'intended_col_name'



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

Try restarting your laptop , remove the wrong code and replace it withf.set_index('TKR')

+1


source







All Articles