dborger
dborger

Reputation: 89

DataFrame.set_index returns 'str' object is not callable

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

Upvotes: 6

Views: 3330

Answers (5)

Yusuf Ganiyu
Yusuf Ganiyu

Reputation: 1095

I once had this same issue.

This simple line of code keep throwing TypeError: 'series' object is not callable error again and again.

df = df.set_index('Date')

I had to shutdown my kernel and restart the jupyter notebook to fix it.

Upvotes: 0

TZH
TZH

Reputation: 83

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')

Upvotes: 6

HippoHip
HippoHip

Reputation: 1

I have the same problem here.

import tushare as ts
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

ts.set_token('*************************************')

tspro = ts.pro_api()

gjyx = tspro.daily(ts_code='000516.SZ', start_date='20190101')

# this doesn't work
# out:'str' object is not callable
gjyx = gjyx.set_index('trade_date')


# this works
gjyx = gjyx.head(len(gjyx)).set_index('trade_date')

jupyter notebook 6.1.6, python 3.9.1, miniconda3, win10

But when I upload this ipynb to ubuntu on AWS, it works.

Upvotes: 0

Yuca
Yuca

Reputation: 6101

I believe I have a solution for you.

I ran into the same problem and I was constructing my dataframes from a dictionary, like this:

df_beta = df['Beta']
df_returns = df['Returns']

then, trying to do df_beta.set_index(Date) would fail. My workaround was

df_beta = df['Beta'].copy()
df_returns = df['Returns'].copy()

So apparently, if you build your dataframes as a "view" of another existing dataframe, you can't set index and it will raise 'Series not callable' error. If instead you create an explicit new object copying the original dataframes, then you can call reset_index, which is what you kind of do when you compute the head.

Hope this helps, 2 years later :)

Upvotes: 0

iPrince
iPrince

Reputation: 125

I know it's been a long while, but I think some people may need the answer in the future.

What you do with f.set_index('TKR') is totally right as long as 'TKR' is a column of DataFrame f.

That is to say, this is a bug you are not supposed to have. It is always because that you redefine some build-in function methods or functions of python in your former steps(Possibly 'set_index'). So, the way to fix is to review your code to find out which part is wrong.

If you are using Jupiter notebook, restart it and run this block only can fix this problem.

Upvotes: 1

Related Questions