0

I have a csv file that's formatted like this:

enter image description here

I'm trying to plot this stock data using the below code, but am getting the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_17802/940727204.py in <module>
----> 1 mpf.plot(data)

~/.local/lib/python3.9/site-packages/mplfinance/plotting.py in plot(data, **kwargs)
    296     config['type'] = _get_valid_plot_types(config['type'])
    297 
--> 298     dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
    299 
    300     config['xlim'] = _check_and_convert_xlim_configuration(data, config)

~/.local/lib/python3.9/site-packages/mplfinance/_arg_validators.py in _check_and_prepare_data(data, config)
     28 
     29     if not isinstance(data.index,pd.core.indexes.datetimes.DatetimeIndex):
---> 30         raise TypeError('Expect data.index as DatetimeIndex')
     31 
     32     if (len(data.index) > config['warn_too_much_data'] and

TypeError: Expect data.index as DatetimeIndex

This is the code I'm using with Jupyter, I can see the problem is that I need a DatetimeIndex, but I can't figure out the correct syntax to do it for two columns.

%matplotlib notebook 
import pandas as pd
import mplfinance as mpf
file = 'stock_data.csv'
data = pd.read_csv(file, parse_dates = True, header = None, index_col = [0,1])
mpf.plot(data)

I tried doing this:

data.Date = pd.to_datetime(data.index)
data.Time = pd.to_datetime(data.index)

But that produces this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_17802/3337290719.py in <module>
----> 1 data.Date = pd.to_datetime(data.index)
      2 data.Time = pd.to_datetime(data.index)
      3 data.info()

~/.local/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
    894             result = _convert_and_box_cache(arg, cache_array, name=arg.name)
    895         else:
--> 896             result = convert_listlike(arg, format, name=arg.name)
    897     elif is_list_like(arg):
    898         try:

~/.local/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    399     assert format is None or infer_datetime_format
    400     utc = tz == "utc"
--> 401     result, tz_parsed = objects_to_datetime64ns(
    402         arg,
    403         dayfirst=dayfirst,

~/.local/lib/python3.9/site-packages/pandas/core/arrays/datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object, allow_mixed)
   2173     order: Literal["F", "C"] = "F" if flags.f_contiguous else "C"
   2174     try:
-> 2175         result, tz_parsed = tslib.array_to_datetime(
   2176             data.ravel("K"),
   2177             errors=errors,

~/.local/lib/python3.9/site-packages/pandas/_libs/tslib.pyx in pandas._libs.tslib.array_to_datetime()

~/.local/lib/python3.9/site-packages/pandas/_libs/tslib.pyx in pandas._libs.tslib.array_to_datetime()

~/.local/lib/python3.9/site-packages/pandas/_libs/tslib.pyx in pandas._libs.tslib._array_to_datetime_object()

~/.local/lib/python3.9/site-packages/pandas/_libs/tslib.pyx in pandas._libs.tslib.array_to_datetime()

TypeError: <class 'tuple'> is not convertible to datetime

What else should I try?

  • **[Don't Post Screenshots](https://meta.stackoverflow.com/questions/303812/)**. Always provide a [mre], with **code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. It's likely the question will be down-voted and closed. You're discouraging assistance, as no one wants to retype data/code, and screenshots are often illegible. [edit] the question and **add text**. Plots are ok. See [How to provide a reproducible copy of your DataFrame using `df.head(30).to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246). – Trenton McKinney Sep 09 '21 at 03:11
  • Don't do `index_col = [0,1]`, and then do `df['Datetime'] = pd.to_datetime(df.Date + ' ' + df.Time)` and then set that as the index. Alternatively, try `.read_csv` with `parse_dates=[['Date', 'Time']]` – Trenton McKinney Sep 09 '21 at 03:14

0 Answers0