0
import numpy as np
import pandas as pd

df = pd.DataFrame({'dt': ['2021-2-13', '2022-2-15'],
                   'w': [5, 7],
                   'n': [11, 8]})
df.reset_index()
print(list(df.loc[:,'dt'].values))

gives: ['2021-2-13', '2022-2-15']

NEEDED: [('2021-2-13'), ('2022-2-15')]

Important (at comment's Q): "NEEDED" is the way "mplfinance" accepts vlines argument for plot (checked) - I need to draw vertical lines for specified dates at x-axis of chart

import mplfinance as mpf

RES['Date'] = RES['Date'].dt.strftime('%Y-%m-%d')
my_vlines=RES.loc[:,'Date'].values  # NOT WORKS

fig, axlist = mpf.plot( ohlc_df, type="candle",  vlines= my_vlines, xrotation=30, returnfig=True, figsize=(6,4))

will only work if explcit my_vlines= [('2022-01-18'), ('2022-02-25')]

SOLVED: Oh, it really appears to be so simple after all

my_vlines=list(RES.loc[:,'Date'].values) 
JeeyCi
  • 354
  • 2
  • 9
  • Why do you want the values in as a list of single tuples? They are just [created as strings](https://stackoverflow.com/q/12876177/10852841). – m13op22 Oct 14 '22 at 16:51
  • "NEEDED" is the way "mplfinance" accepts vlines argument for plot (checked) - I need to draw vertical lines for specified dates at x-axis of chart – JeeyCi Oct 14 '22 at 16:55
  • res - is df of dates for vlines, ohlc is df['date','open','high','low','close'] as usually for candle_chart -- your questions seems out of the topic's problem (if you didn't use mplfinance - I can not describe it here) – JeeyCi Oct 14 '22 at 17:46
  • I corrected 1st post with Question & title of the problem – JeeyCi Oct 14 '22 at 17:48
  • As noted in the [mplfinance documentation](https://github.com/matplotlib/mplfinance/blob/master/examples/using_lines.ipynb) (see **`In [7]`** on that page) for `vlines` mplfinance takes a **list of dates or datetimes** ... ***not*** a list of tuples. This question is incorrect in its premise. – Daniel Goldfarb Oct 14 '22 at 19:25
  • I’m voting to close this question because the question itself is based on an incorrect premise (that mplfinance `vlines` kwarg requires a list of Numpy arrays). This is not correct. It needs simply a list of dates or datetimes (or strings convertable to dates or datetimes). – Daniel Goldfarb Oct 14 '22 at 19:28
  • I appologize for incorrect premise, that causes my real problem, & I changed the topic's title... because really type( ('a') ) is & type( ('a',) ) is -- neither "list of arrays" – JeeyCi Oct 15 '22 at 10:41

2 Answers2

0

Your question asks for a list of Numpy arrays but your desired output looks like Tuples. If you need Tuples, note that it's the comma that makes the tuple not the parentheses, so you'd do something like this:

desired_format = [(x,) for x in list(df.loc[:,'dt'].values)]

If you want numpy arrays, you could do this

desired_format = [np.array(x) for x in list(df.loc[:,'dt'].values)]
lamdoug
  • 33
  • 4
  • 1st version leaves commas [('2021-2-13',), ('2022-2-15',)] – JeeyCi Oct 14 '22 at 17:09
  • with 2nd in *mplfinance* still TypeError: kwarg "vlines" validator returned False for value: "[array('2021-11-16T00:00:00.000000000', dtype='datetime64[ns]'), ---> perhaps .dt.strftime('%Y-%m-%d') is needed somewhere? – JeeyCi Oct 14 '22 at 17:39
  • [np.array(x) for x in list(df.loc[:,'tradeDate'].dt.strftime('%Y-%m-%d').values)] also TypeError: kwarg "vlines" validator returned False for value: "[array('2021-11-16', dtype=' – JeeyCi Oct 14 '22 at 18:00
0

I think I understand your problem. Please see the example code below and let me know if this resolves your problem. I expanded on your dataframe to meet mplfinance plot criteria.

import pandas as pd
import numpy as np
import mplfinance as mpf

df = pd.DataFrame({'dt': ['2021-2-13', '2022-2-15'],'Open': [5,7],'Close': [11, 8],'High': [21,30],'Low': [7, 3]})

df['dt']=pd.to_datetime(df['dt'])
df.set_index('dt', inplace = True)

mpf.plot(df, vlines = dict(vlines = df.index.tolist()))
Ravi
  • 11
  • 3
  • I DON'T need commas in (... ,) -- further drawing plot I get TypeError: kwarg "vlines" validator returned False for value: ... vlines is desired format in the starting question in original code – JeeyCi Oct 14 '22 at 17:15
  • 3
    Can you please share that code as well? From mplfinance I don't see any need to convert your list to tuple as well - `mpf.plot(daily,type='candle',vlines=dict(vlines=['2019-11-06','2019-11-15','2019-11-25'],linewidths=(1,2,3)))` (this seems to work for them) – Ravi Oct 14 '22 at 17:20
  • Ravi , I corrected question above – JeeyCi Oct 14 '22 at 17:31
  • @JeeyCi, please let me know if your issue gets resolved now. – Ravi Oct 14 '22 at 18:07
  • Ravi, thanks a lot - I didn't use your suggestion about dict, but you inspired me to use just list -- solution published in initial post – JeeyCi Oct 14 '22 at 18:11
  • JeeyCi, cool. I'm glad that it got resolved. – Ravi Oct 14 '22 at 18:15