Home > database >  Removing utc info from yfinance dataframe
Removing utc info from yfinance dataframe

Time:01-25

How can I remove the utc portion of a DF created from a yfinance? Every example I and approach I seen has failed. eg:

df = yf.download('2022-01-01', '2023-01-06', interval = '60m' )
pd.to_datetime(df['Datetime'])

error: 3806 #If we have a listlike key, _check_indexing_error will raise

        KeyError: 'Datetime'

As well as the following approaches enter code heredf = df.reset_index()

df = pd.DataFrame(df, columns = ['Datetime', "Close"])

df.rename(columns = {'Date': 'ds'}, inplace = True)
df.rename(columns = {'Close':'y'}, inplace = True)
#df['ds'] = df['ds'].dt.date
#df['ds'] = datetime.fromtimestamp(df['ds'], tz = None)
#df['ds'] = df['ds'].dt.floor("Min")
#df['ds'] = pd.to_datetime(df['ds'].dt.tz_convert(None))
#df['ds'] = pd.to_datetime['ds']
#pd.to_datetime(df['ds'])
df['ds'].dt.tz_localize(None)
print(df)

with similar errors, Any help or pointer will greatly appreciated I have spent the entire morning on this.

Thanks in advance BTT

CodePudding user response:

Your code interprets '2022-01-01' as the first and required argument tickers. This date is not a valid ticker, so yf.download() does not return any price and volume data. Try:

df = yf.download(tickers='AAPL', start='2022-01-01', end='2023-01-06', interval = '60m' )
df.index = df.index.tz_localize(None)
  • Related