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


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")

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 :


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:
               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")
                   dt1 = str(d+" 14:00")
               dt = datetime.strptime(dt1, "%Y/%m/%d %H:%M")

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

Screenshot of the results:


I used this to solve my issue:

import matplotlib.dates as mpl_dates
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 *