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?

6
  • You can try to set you x-axis label as you date column if it's sorted based on time or stackoverflow.com/questions/19079143/… Commented Nov 6, 2019 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] Commented Nov 6, 2019 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. Commented Nov 6, 2019 at 22:30
  • did you use df['date'] = pd.to_datetime(df['date']) in pandas or? Commented Nov 6, 2019 at 22:32
  • @steven yes, my date columns is of type datetime64[ns] already. Commented Nov 6, 2019 at 22:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.