1

I am trying to plot multiple time series curves and setting x axis date range and interval and I got an error of ValueError: Could not convert object to NumPy timedelta. no sure why? Thanks for help


df

    05A75   08A77   13A78   15A77
Date                
2020-01-01  396.2   0.0 0.0 310.7
2020-01-02  390.4   0.0 3.2 332.7
2020-01-03  394.5   0.0 13.1    340.2
2020-01-04  392.9   0.0 0.0 297.4
2020-01-05  393.2   0.0 133.9   244.5

date_min=df.index[0]
date_max=df.index[-1]
date_step=np.timedelta64(30,'D')

plt.figure(figsize=(20,12))
plt.plot(df.index,df.rolling(window=30,center=True,win_type='exponential').mean(),label=df.columns)
plt.xticks(np.arange(date_min,date_max,step=date_step))
plt.legend()
plt.show()

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21828/184957987.py in <module>
      1 plt.figure(figsize=(20,12))
      2 plt.plot(df.index,df.rolling(window=30,center=True,win_type='exponential').mean(),label=df.columns)
----> 3 plt.xticks(np.arange(date_min,date_max,step=date_step))
      4 plt.legend()
      5 plt.show()

ValueError: Could not convert object to NumPy timedelta

I am strugllting with it so decide to just use df.plot, then i got date range very strange

enter image description here

roudan
  • 3,082
  • 5
  • 31
  • 72
  • 1
    without an actual MWE this is hard to tell. – mapf Jan 07 '22 at 16:18
  • Thanks mapf, I added some data. – roudan Jan 07 '22 at 16:20
  • Can you print your `date_min` and `date_max`? I reckon that might solve the problem. – mapf Jan 07 '22 at 16:27
  • Thanks. date_min is '2020-01-01', date_max is '2021-01-01' – roudan Jan 07 '22 at 16:29
  • Ah ok, nvm then. I guess maybe `numpy` doesn't understand the format. – mapf Jan 07 '22 at 16:29
  • Can you maybe try to convert `date_min` and `date_max` to `numpy.datetime64` first? – mapf Jan 07 '22 at 16:32
  • Thanks, I just did, and got another error of TypeError: 'value' must be an instance of str or bytes, not a datetime.date – roudan Jan 07 '22 at 16:35
  • I think it is related to moving average, here is the detailed error: ConversionError: Failed to convert value(s) to axis units: array(['2020-01-01', '2020-01-31', '2020-03-01', '2020-03-31', '2020-04-30', '2020-05-30', '2020-06-29', '2020-07-29', '2020-08-28', '2020-09-27', '2020-10-27', '2020-11-26', '2020-12-26', '2021-01-25', '2021-02-24', '2021-03-26', '2021-04-25', '2021-05-25', '2021-06-24', '2021-07-24', '2021-08-23', '2021-09-22', '2021-10-22', '2021-11-21', '2021-12-21'], dtype='datetime64[D]') – roudan Jan 07 '22 at 16:37
  • I see. Maybe this thread will help: https://stackoverflow.com/q/13703720/5472354 – mapf Jan 07 '22 at 16:41

0 Answers0