samar2170
samar2170

Reputation: 67

Pandas Attribute error: Nonetype has no attribute rename

learning this from a tutorial, the code isn't working on my machine. error in line with df.rename

def compile_data():
colist = pd.read_csv("nse500symbolistnov2020.csv")
tickers = colist['Symbol']

maindf = pd.DataFrame()

for count,ticker in enumerate(tickers):
    df = pd.read_csv('stock_dfs/{}.csv'.format(ticker))
    df = df.set_index('Date',inplace=True)
    df = df.rename(columns={'Adj Close': ticker},inplace=True)
    df.drop(['Open','High','Low','CLose','Volume'],1,inplace=True)

    if maindf.empty:
        maindf = df
    else:
        maindf = maindf.join(df, how='outer')

    if count % 10 == 0:
        print(count)

print(maindf.head())
maindf.to_csv('NSE60joined.csv')

Upvotes: 4

Views: 13951

Answers (2)

FilipA
FilipA

Reputation: 612

When you specify inplace=True and want to see it's contents, it would return None as they merely mutate the DF instead of creating a new copy of it. Basically, you're assigning None to the result and hence it complains of the AttributeError as it isn't a df.DataFrame object anymore to access it's .head() method.

You can do it now in two ways:

No assigning with inplace parameter

df.rename(columns={'Adj Close': ticker},inplace=True)

assign without inplace parameter

df= df.rename(columns={'Y':l}) 

 

Upvotes: 0

Ivan Gorin
Ivan Gorin

Reputation: 391

The problem is in the line

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

Either remove inplace=True, or remove the assignment df =, leaving just

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

The same goes for the next line. Either use inplace=True, or assign the new dataframe to df, not both.

Upvotes: 5

Related Questions