piccolomo / plotext

plotting on terminal
MIT License
1.79k stars 85 forks source link

Feature Request: plot directly datetime objects #154

Open realSAH opened 2 years ago

realSAH commented 2 years ago

I'm plotting with x axis being datetime object and got this pandas error:

File ~\venvs\ve\lib\site-packages\plotext\_monitor.py:412, in <listcomp>(.0)
    410 xlabels = x if x_string else map(str, x)
    411 x = xticks if x_string else x
--> 412 x = [el + offset for el in x]
    413 xbar, ybar = ut.bars(x, y, width, minimum)
    414 xbar, ybar = [xbar, ybar]  if orientation[0] == 'v' else [ybar, xbar]

File ~\venvs\ve\lib\site-packages\pandas\_libs\tslibs\timestamps.pyx:296, in pandas._libs.tslibs.timestamps._Timestamp.__add__()

TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported.  Instead of adding/subtracting `n`, use `n * obj.freq`
piccolomo commented 2 years ago

Hi @southaustralia,

thanks for reporting. Try to pass date/time strings instead using the datetime_to_string() or datetimes_to_string() methods. Does it work?

plotext does not allow datetime object directly in the plotting functions for now. Which makes me think, that it could probably be a good added feature. If you think it could, we could rename this issue as a Feature Request about it.

thanks, Savino

realSAH commented 2 years ago

Hi, thanks for the suggestion. I was forced to do this df['date'].apply(lambda x: x.strftime("%d/%m/%Y")) Anything other than literally this will cause an error saying ValueError: Date Form should be: %d/%m/%Y

My datetime column happen to be in hours range while the year, month and day don't change, so even thought the API accepted my syntax the plot wasn't correct.

I think supporting datetime object out of the box will implicitly solve the problem. Thank you for your time.

piccolomo commented 2 years ago

Hi @southaustralia,

this is something I could definitively work on. In the mean time here is a guide to deal with date/time plots for now: basically one needs to specify the datetime form with date_form() and convert to string with datetime_to_string() or datetimes_to_string() methods, and pass the string to the plot (instead of the datetime objects for now).

If the guide is not clear, some feedback is appreciated to improve it.

Thanks, Savino

piccolomo commented 2 months ago

Just to update and apologise: I have been quite busy rewriting the package from ground up and will update.