0

I have this dataframe:

df1.head()
Out[107]: 
   crashes       date
0     90.0 2019-10-31
1     77.0 2019-10-30
2     93.0 2019-10-29
3     79.0 2019-10-28
4     72.0 2019-10-27

Now, I want to apply an anomaly detection operation to this data. I picked the SD-ESD method. Here is the script:

outliers_indices = sesd.seasonal_esd(df1.crashes,seasonality = 25, hybrid=True, max_anomalies=365, alpha = 3)

x= df1.crashes[:320]
y=df1.date[:320]

outliers = []
sorted_outliers_indices = np.sort(outliers_indices)
test_outliers_indices = sorted_outliers_indices
for idx in test_outliers_indices:
  outliers.append(df1.crashes[idx])


marks = []
for i in x:
    if i in outliers:
        marks.append(i)
    else:
        marks.append(np.NAN)

plt.figure(figsize = (20,8))
plt.plot(x)
plt.plot(marks, 'ro', markersize = "3")
plt.legend(handles=[mpatches.Patch(color='#62A3C9', label='Crashes'), mpatches.Patch(color='red', label='Crash Anomaly')])
plt.ylabel('Crashes')
plt.xlabel('Date')
display()

My chart looks like this and as you can see, the Dates are not plotting in the right sequence. Instead, it uses data points indices. enter image description here

When I tried plt.plot(x,y), it throws an "ValueError: view limit minimum -36457.6 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units."

My date columns is datetime64[ns]. Can someone help with this?

LaLaTi
  • 1,455
  • 3
  • 18
  • 31
  • You can try to set you x-axis label as you date column if it's sorted based on time or https://stackoverflow.com/questions/19079143/how-to-plot-time-series-in-python/19079248 – steven Nov 06 '19 at 22:15
  • @steven when I use plt.plot(x,y), is throws an error: ValueError: view limit minimum -36457.6 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units. My date columns is datetime64[ns] – LaLaTi Nov 06 '19 at 22:19
  • sorry I don't know what's going on based on your code. You may wanna to add your dependencies as well so that folks could know the issue. – steven Nov 06 '19 at 22:30
  • did you use `df['date'] = pd.to_datetime(df['date'])` in pandas or? – steven Nov 06 '19 at 22:32
  • @steven yes, my date columns is of type datetime64[ns] already. – LaLaTi Nov 06 '19 at 22:34
  • @LaLaTi you have defined `y = df1.date[:320]`. Did you try `plt.plot(y,x)` and `plt.plot(y, marks, 'ro', markersize = "3")` – impopularGuy Nov 07 '19 at 05:42

0 Answers0