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

Geno C
  • 1,401
  • 3
  • 11
  • 26
Austin Jin
  • 141
  • 3
  • 11

1 Answers1

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)
ALollz
  • 57,915
  • 7
  • 66
  • 89