2021年2月3日星期三

Is there a way to resample price data to OHLC without trading out the DateTime Index for a RangeIndex?

So I'm in a bit of a pickle.

I want to resample time and price ticker data into Open High Low Close.

To do that first I use df.date = pd.to_datetime['date'] followed by df = df.set_index('date')['price'].resample('1H').ohlc()

However in the process I lose my precious df.date - it is now df.index. And they are not the same. Other indicator functions that used to work with df.date no longer work with df.index

Even the price data given to me does not put the 'date' on the same level as the names of other columns, implying that it is not even the name of the column or something:

                             open          high           low         close  date                                                                         2021-01-28 01:00:00  30653.553694  30653.553694  30653.553694  30653.553694  2021-01-28 02:00:00  30994.198478  30994.198478  30994.198478  30994.198478  2021-01-28 03:00:00  31274.386041  31274.386041  31274.386041  31274.386041  2021-01-28 04:00:00  31441.260678  31441.260678  31441.260678  31441.260678  2021-01-28 05:00:00  31196.750744  31196.750744  31196.750744  31196.750744  ...                           ...           ...           ...           ...  2021-02-03 20:00:00  36708.821125  36708.821125  36708.821125  36708.821125  2021-02-03 21:00:00  37036.271097  37036.271097  37036.271097  37036.271097  2021-02-03 22:00:00  37266.377988  37266.377988  37266.377988  37266.377988  2021-02-03 23:00:00  37262.988292  37262.988292  37262.988292  37262.988292  2021-02-04 00:00:00  37725.264554  37808.578235  37725.264554  37808.578235  

I include my code, please help me to decipher this one. Basically I just need to have keep the df.dates as df.dates but in DateTime format while the prices given in OHLC format.

import pandas as pd  import json  import requests    API_URL = 'https://api.coingecko.com/api/v3'    r = requests.get(API_URL + '/coins/bitcoin/market_chart?vs_currency=usd&days=7&interval=hourly')    d = r.json()    df = pd.DataFrame(d['prices'])  df.columns = ['date', 'price']  df['date'] = pd.to_datetime(df['date'], unit='ms')  df = df.set_index('date')['price'].resample('1H').ohlc()  # THIS DOES NOT WORK --> df.index = df.date  
https://stackoverflow.com/questions/66037882/is-there-a-way-to-resample-price-data-to-ohlc-without-trading-out-the-datetime-i February 04, 2021 at 08:51AM

没有评论:

发表评论