1

i tried to rename a column in a Dataframe by using rename function but I get an error indicates: "builtins.TypeError: rename() got an unexpected keyword argument 'columns'"

my code is :

import pandas as pd
import pandas_datareader as web
import datetime as dt

#this is latest 5 days data selection
prev=30
endDate=dt.datetime.today().date()
startDate=endDate-pd.to_timedelta(prev,unit='d')


def get_data(ticker):
    stockData=web.DataReader(ticker,'yahoo',startDate,endDate)['Adj Close']
    stockData.rename(columns={'Adj Close':str(ticker)},inplace=True)
    return stockData
TSLA=get_data('TSLA')
VTI=get_data('VTI')

I wonder why it happens

0

1 Answer 1

4

Series.rename does not have a columns argument. Series are 1D so there is only ever one thing to rename. On the other hand, DataFrame.rename does have a columns argument, since there can be multiple columns to rename


Either select the DataFrame slice [[ ]]:

stockData = web.DataReader(ticker, 'yahoo', startDate, endDate)[['Adj Close']]
stockData.rename(columns={'Adj Close': str(ticker)}, inplace=True)

or rename the Series:

stockData = web.DataReader(ticker, 'yahoo', startDate, endDate)['Adj Close']
stockData.rename(str(ticker), inplace=True)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.