[SOLVED] TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]')

Issue

I am trying to set an ARIMA model to some data, for this, I used ‘autocorrelation_plot()’ with my time series. It’s generates however the error in the title.

I have an attribute table composed, among others, of a Date and time fiels.
I extracted them (after transforming the attribute table into a numpy table), put them in a ‘datetime’ variable and appended them all in a list:


   O,A = [],[]
   dt = datetime.strptime(dt1, "%Y/%m/%d %H:%M")
   A.append(dt)

I tried then to create time series and printed them to be sure of the results:


   data2 = pd.Series(A, O)
   print data2

The results were satisfying, until I decided to auto-correlate :

Auto-correlation command :


autocorrelation_plot(data2)

After this command, it returns:

TypeError: ufunc add cannot use operands with types dtype(‘M8[ns]’) and dtype(‘M8[ns]’)

I guess it’s due to the conversion of the datetime.strptime to a numpy ?
I tried to follow some suggestions from previous questions
index.to_pydatetime() , dtype, M8[ns] error …, in vain.

Minimal reproducible example:


  from pandas import datetime
  from pandas import DataFrame
  import pandas as pd
  from matplotlib import pyplot as plt
  from pandas.tools.plotting import autocorrelation_plot

  arr = arcpy.da.TableToNumPyArray(inTable ,("PROVINCE","ZONE_CODE","MEAN", "Datetime","Time"))
  arr_length = len(arr)
  j = 1
  O,A = [],[]
  while j<=55: #I have 55 provinces
       i = 0
       while i<arr_length:
           if arr[i][1]== j:
               O.append(arr[i][2])
               c = str(arr[i][3])
               d = str(c[0:4]+"/"+c[5:7]+"/"+c[8:10])
               t = str(arr[i][4])
               if t=="10":
                   dt1 = str(d+" 10:00")
               else:
                   dt1 = str(d+" 14:00")
               dt = datetime.strptime(dt1, "%Y/%m/%d %H:%M")

               A.append(dt)
           i = i+1
       data2 = pd.Series(A, O)
       print data2
       autocorrelation_plot(data2)
       del A[:]
       del O[:]
       j += 1


Screenshot of the results:
results

Solution

I used this to solve my issue:

import matplotlib.dates as mpl_dates
df.reset_index(inplace=True)
df['Date']=df['Date'].apply(mpl_dates.date2num)
df = df.astype(float)

Answered By – Fill.i.Pillow

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *