Home > Software design >  Python Pandas Dataframe: Datetime not stored in column
Python Pandas Dataframe: Datetime not stored in column

Time:09-27

I started programming Python a few weeks ago. Since yesterday evening I'm stuck because of a probably simple problem that I just can't solve.

I'm trying to download data via the Tradingview API, it works so far. The downloaded data is then stored in a dataframe, which consists of 6 columns. Now the problem is that I don't know how to get the datetime data, simply because it isn't a column. Is there a way to take the datetime data and put it in a separate column?

To help you understand my problem better, here is my code:

#import lib
from tvDatafeed import TvDatafeed, Interval
#login tradingview api
tv = TvDatafeed()
#download data
us500 = tv.get_hist(symbol='US500',exchange='GLOBALPRIME',
interval=Interval.in_1_minute,n_bars=1000)
#show dataframe
us500

Output:

    symbol  open    high    low close   volume
    datetime                        
    2022-09-26 22:54:00 GLOBALPRIME:US500   3653.90 3654.20 3653.70 3654.10 68.0
    2022-09-26 22:55:00 GLOBALPRIME:US500   3654.10 3654.40 3653.65 3653.90 87.0
    2022-09-26 22:56:00 GLOBALPRIME:US500   3653.95 3654.05 3653.75 3653.90 46.0
    2022-09-26 22:57:00 GLOBALPRIME:US500   3653.90 3654.70 3653.90 3654.50 81.0
    2022-09-26 22:58:00 GLOBALPRIME:US500   3654.70 3654.95 3653.30 3653.65 107.0
    ... ... ... ... ... ... ...
    2022-09-27 16:28:00 GLOBALPRIME:US500   3686.35 3686.50 3682.35 3683.55 1049.0
    2022-09-27 16:29:00 GLOBALPRIME:US500   3683.70 3684.80 3681.10 3681.50 1067.0
    2022-09-27 16:30:00 GLOBALPRIME:US500   3681.60 3688.35 3681.60 3688.00 1111.0
    2022-09-27 16:31:00 GLOBALPRIME:US500   3688.00 3690.35 3686.45 3687.85 1246.0
    2022-09-27 16:32:00 GLOBALPRIME:US500   3688.10 3690.75 3688.10 3690.00 139.0
    1000 rows × 6 columns

CodePudding user response:

The easiest solution would be :

us501 = us500.reset_index(drop=False)
us501
  • Related