2021年1月1日星期五

How to index out open and close in pandas datetime dataframe?

Okay so I have a csv with minute data for the S&P 500 index for 2020, and I am looking how to index out only the close and open for 9:30 and 4:00 only. In essence I just want what the market open and close was. So far the code is:

import pandas as pd  import datetime as dt  import numpy as np    d = pd.read_csv('/Volumes/Seagate Portable/usindex_2020_all_tickers_awvbxk9/SPX_2020_2020.txt')  d.columns = ['Dates', 'Open', 'High', 'Low', 'Close']  d.drop(['High', 'Low'], axis=1, inplace=True)  d.set_index('Dates', inplace=True)    d.head()   

It wont let me share the csv file but this is what the output looks like:

                    Open    Close  Dates         2020-01-02 09:31:00 3247.19 3245.22  2020-01-02 09:32:00 3245.07 3244.66  2020-01-02 09:33:00 3244.89 3247.61  2020-01-02 09:34:00 3247.38 3246.92  2020-01-02 09:35:00 3246.89 3249.09  

I have tried using loc and dt.time, which I am assmuning is the right way to code I just cannot think of the exact code to index out these 2 times. Any ideas? Thank you!

https://stackoverflow.com/questions/65535328/how-to-index-out-open-and-close-in-pandas-datetime-dataframe January 02, 2021 at 09:38AM

没有评论:

发表评论