How can I deal with Expect data.index as DatetimeIndex?

13.5k Views Asked by At

I am planning to get a candlestick plot from the bitcoin data. Here is my code for select the dataframe I want after loading the csv file.

df['Date'] = pd.to_datetime(df['Date'])
start_date = '2016-02-27'
end_date = '2021-02-27'
mask = (df['Date'] >= start_date) & (df['Date'] <= end_date)
df = df.loc[mask]
df

and then, I entered the code for making the candlestick plot like below:

import matplotlib.pyplot as plt
! pip install --upgrade mplfinance
import mplfinance as mpf
import matplotlib.dates as mpl_dates


mpf.plot(df, type = 'candle', style = 'charles',
        title = 'Bitcoin Price',
        ylabel = 'Price (USD$)',
        volume = True,
        ylabel_lower = 'Shares \nTraded',
        mav = (3,6,9),
        savefig = 'chart-mplfinance.png')

It said "TypeError: Expect data.index as DatetimeIndex". So I looked up the solution for this on google and I tried out this:

df = dict()
df['Date'] = []
df['High'] = []
df['Low'] = []
df['Open'] = []
df['Close'] = []
df['Volume'] = []
for dict in df:
    df['Date'].append(datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')
    df['High'].append(dict['High'])
    df['Low'].append(dict['Low'])
    df['Open'].append(dict['Open'])
    df['Close'].append(dict['Close'])
    df['Volume'].append(dict['Vol'])
print("df:", df)
pdata = pd.DataFrame.from_dict(df) 
pdata.set_index('Date', inplace=True)
mpf.plot(pdata)

This time, it said "invalid syntax" I'm not sure where I get this wrong, is there anything that I have missed?

1

There are 1 best solutions below

0
On

There are two easy ways to make sure your dataframe has a pandas.DatetimeIndex as the dataframe index:

  1. When calling read_csv() indicate which column you want to use for the index (which should be the column that contains the dates/datetimes), and also set kwarg parse_dates=True.
     
    This will will automatically convert the datetimes column (which is normally strings within a csv file) into a DatetimeIndex object, and set it as the index
     
    You can see this being done in the examples in the mplfinance repository, for example, click here, and look under basic usage where you can see index_col=0, parse_dates=True in the call to read_csv().
     
     

  2. Use the pandas.DatetimeIndex() constructor.
     
    For example, instead of what you wrote,
    df['Date'] = pd.to_datetime(df['Date']), you would write:

    df.index = pd.DatetimeIndex(df['Date'])

As a side note, once the dataframe has a DatetimeIndex, you don't need the mask in the next section of code, but can simply slice as follows:

start_date = '2016-02-27'
end_date   = '2021-02-27'
df = df.loc[start_date:end_date]

hth.