I have the following code:
import pandas as pd
import yfinance as yf
import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd, mplfinance as mpf, matplotlib.pyplot as plt
import numpy as np
db = yf.download(tickers='goog', start=datetime.datetime.now()-relativedelta(days=7), end= datetime.datetime.now(), interval="5m")
db = db.dropna()
If i wanted to get the float value i would just do this "
db.index.get_loc(pd.Timestamp('2022-02-16 12:05:00'))
and it would produce 34
So :
b =db[33:34]
But if i do this:
db.index.get_loc(pd.Timestamp(b.index.strftime('%Y-%m-%d %I:%M:%S')))
I get:
TypeError: Cannot convert input [Index(['2022-02-22 09:15:00'], dtype='object', name='Datetime')] of type <class 'pandas.core.indexes.base.Index'> to Timestamp
Could you please advise how can i get the index from datetime?
CodePudding user response:
Here b.index.strftime('%Y-%m-%d %I:%M:%S')
return one element Index, so for scalar need select first value:
b = db.iloc[33:34]
print (b)
Adj Close Volume
Datetime
2022-02-16 12:15:00-05:00 2709.629883 8000
print (b.index.strftime('%Y-%m-%d %I:%M:%S'))
Index(['2022-02-16 12:15:00'], dtype='object', name='Datetime')
print (b.index.strftime('%Y-%m-%d %I:%M:%S')[0])
2022-02-16 12:15:00
a = db.index.get_loc(pd.Timestamp(b.index.strftime('%Y-%m-%d %I:%M:%S')[0]))
print (a)
33