Mplfinance: TypeError: Expect data.index as DatetimeIndex

Created on 11 Apr 2020  路  4Comments  路  Source: matplotlib/mplfinance

how to deal with the error and mpf.plot(pdata) didn't work.
thx

import requests
import json
import pandas as pd
import mplfinance as mpf
import matplotlib.pyplot as plt

url = "https://api.huobi.pro/market/history/kline?period=5min&size=3&symbol=btcusdt"
r = requests.get(url)
data = json.loads(r.content)['data']
print("data:", data)
pdata = pd.DataFrame(data)
pdata.index.name = 'id'
pdata = pdata[['id', 'open', 'close', 'high', 'low', 'vol', 'amount', 'count']]
pdata['id'] = pd.to_datetime(pdata['id'], unit='s')
mpf.plot(pdata)

question

Most helpful comment

Hi franklee00,

It appears the issues lies in the fact that the data that API call is returning isn't in the format that mplfinance expects. The data you specified is a list of dictionaries which each have id, open, close, high, low, volume, etc values. mplfinance expects a dictionary which keys such as 'Data', 'Open', 'Close', 'High', 'Low', 'Volume' each with a value in the form of a list of values.

I went ahead and reformatted your data for you here:

import requests
import json
import pandas as pd
import datetime
import mplfinance as mpf
import matplotlib.pyplot as plt

url = "https://api.huobi.pro/market/history/kline?period=5min&size=3&symbol=btcusdt"
r = requests.get(url)
data = json.loads(r.content)['data']
reformatted_data = dict()
reformatted_data['Date'] = []
reformatted_data['Open'] = []
reformatted_data['High'] = []
reformatted_data['Low'] = []
reformatted_data['Close'] = []
reformatted_data['Volume'] = []
for dict in data:
    reformatted_data['Date'].append(datetime.datetime.fromtimestamp(dict['id']))
    reformatted_data['Open'].append(dict['open'])
    reformatted_data['High'].append(dict['high'])
    reformatted_data['Low'].append(dict['low'])
    reformatted_data['Close'].append(dict['close'])
    reformatted_data['Volume'].append(dict['vol'])
print("reformatted data:", reformatted_data)
pdata = pd.DataFrame.from_dict(reformatted_data) 
pdata.set_index('Date', inplace=True)
mpf.plot(pdata)

Another note is that you need to convert your time stamps into date time objects within your data. This can done with the datetime.datetime.fromtimestamp(TIMESTAMP) function available when you import datetime.

Hopefully, this helps!

-- Caleb

All 4 comments

Hi franklee00,

It appears the issues lies in the fact that the data that API call is returning isn't in the format that mplfinance expects. The data you specified is a list of dictionaries which each have id, open, close, high, low, volume, etc values. mplfinance expects a dictionary which keys such as 'Data', 'Open', 'Close', 'High', 'Low', 'Volume' each with a value in the form of a list of values.

I went ahead and reformatted your data for you here:

import requests
import json
import pandas as pd
import datetime
import mplfinance as mpf
import matplotlib.pyplot as plt

url = "https://api.huobi.pro/market/history/kline?period=5min&size=3&symbol=btcusdt"
r = requests.get(url)
data = json.loads(r.content)['data']
reformatted_data = dict()
reformatted_data['Date'] = []
reformatted_data['Open'] = []
reformatted_data['High'] = []
reformatted_data['Low'] = []
reformatted_data['Close'] = []
reformatted_data['Volume'] = []
for dict in data:
    reformatted_data['Date'].append(datetime.datetime.fromtimestamp(dict['id']))
    reformatted_data['Open'].append(dict['open'])
    reformatted_data['High'].append(dict['high'])
    reformatted_data['Low'].append(dict['low'])
    reformatted_data['Close'].append(dict['close'])
    reformatted_data['Volume'].append(dict['vol'])
print("reformatted data:", reformatted_data)
pdata = pd.DataFrame.from_dict(reformatted_data) 
pdata.set_index('Date', inplace=True)
mpf.plot(pdata)

Another note is that you need to convert your time stamps into date time objects within your data. This can done with the datetime.datetime.fromtimestamp(TIMESTAMP) function available when you import datetime.

Hopefully, this helps!

-- Caleb

Hi franklee00,

It appears the issues lies in the fact that the data that API call is returning isn't in the format that mplfinance expects. The data you specified is a list of dictionaries which each have id, open, close, high, low, volume, etc values. mplfinance expects a dictionary which keys such as 'Data', 'Open', 'Close', 'High', 'Low', 'Volume' each with a value in the form of a list of values.

I went ahead and reformatted your data for you here:

import requests
import json
import pandas as pd
import datetime
import mplfinance as mpf
import matplotlib.pyplot as plt

url = "https://api.huobi.pro/market/history/kline?period=5min&size=3&symbol=btcusdt"
r = requests.get(url)
data = json.loads(r.content)['data']
reformatted_data = dict()
reformatted_data['Date'] = []
reformatted_data['Open'] = []
reformatted_data['High'] = []
reformatted_data['Low'] = []
reformatted_data['Close'] = []
reformatted_data['Volume'] = []
for dict in data:
    reformatted_data['Date'].append(datetime.datetime.fromtimestamp(dict['id']))
    reformatted_data['Open'].append(dict['open'])
    reformatted_data['High'].append(dict['high'])
    reformatted_data['Low'].append(dict['low'])
    reformatted_data['Close'].append(dict['close'])
    reformatted_data['Volume'].append(dict['vol'])
print("reformatted data:", reformatted_data)
pdata = pd.DataFrame.from_dict(reformatted_data) 
pdata.set_index('Date', inplace=True)
mpf.plot(pdata)

Another note is that you need to convert your time stamps into date time objects within your data. This can done with the datetime.datetime.fromtimestamp(TIMESTAMP) function available when you import datetime.

Hopefully, this helps!

-- Caleb

thank u very much.
the new API is so convenient!

What if I m taking data from Api like NSEPY
Then how to solve it

What if I m taking data from Api like NSEPY
Then how to solve it

@Kartket You would need to convert the NSEPY data to be the same format that mplfinance is expecting. The specifics on how you would do this will be dependent on the format of the NSEPY data, but the solution will be very similar to the code I provided last year.

Was this page helpful?
5 / 5 - 1 ratings

Related issues

skfnxh picture skfnxh  路  3Comments

toksis picture toksis  路  5Comments

Full4me picture Full4me  路  3Comments

obahat picture obahat  路  3Comments

mattcambs picture mattcambs  路  3Comments