I'm using this Python script:
import pandas as pd
import numpy as np
from utils import timedelta64_to_secs
if __name__ == '__main__':
data_frame = pd.read_csv("/path/to/file.csv", nrows=1000000, skiprows=0, header=0)
data_array = data_frame.iloc[:, 1]
activations = get_activations(data_array, min_off_duration=30, min_on_duration=12, border=1, on_power_threshold=200)
where get_activations() is defined here.
At line 954, the function timedelta64_to_secs() is called; it generates the error:
File "C:\Users\username\Documents\PyCharmProjects\NILMTK\nilmtk\utils.py", line 65, in timedelta64_to_secs
return timedelta / np.timedelta64(1, 's')
numpy.core._exceptions.UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('int64') and dtype('<m8[s]')
The definition of timedelta64_to_secs() (which can be found here) is:
def timedelta64_to_secs(timedelta):
"""Convert `timedelta` to seconds.
Parameters | timedelta : np.timedelta64
Returns | float : seconds
"""
if len(timedelta) == 0:
return np.array([])
else:
return timedelta / np.timedelta64(1, 's')
where timedelta is a 712-element numpy array, while np.timedelta64(1, 's') is timedelta64 type.
How could I solve?
CodePudding user response:
You are dividing numpy arrays of different types. In order to divide you should make the types consistent, in this case I suggest changing the numpy "<m8[s]" (a datetime dtype) into a relevant type, e.g. "int64". A suggested update to the function timedelta64_to_secs is shown below (docstrings removed for brevity).
import numpy as np
def timedelta64_to_secs(timedelta):
if len(timedelta) == 0:
return np.array([])
else:
return timedelta / np.timedelta64(1, 's').astype('int64')
CodePudding user response:
The function should work with a proper timedelta input:
In [24]: x=np.array([3,2,1], 'datetime64[m]')
In [25]: x
Out[25]:
array(['1970-01-01T00:03', '1970-01-01T00:02', '1970-01-01T00:01'],
dtype='datetime64[m]')
In [26]: y=x[:-1]-x[1:]
In [27]: y
Out[27]: array([1, 1], dtype='timedelta64[m]')
In [28]: y/np.timedelta64(1,'s')
Out[28]: array([60., 60.])
your error:
In [29]: 1/np.timedelta64(1,'s')
Traceback (most recent call last):
File "<ipython-input-29-d9febc47850f>", line 1, in <module>
1/np.timedelta64(1,'s')
UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('int64') and dtype('<m8[s]')
another converter option:
In [31]: y.astype('timedelta64[s]')
Out[31]: array([60, 60], dtype='timedelta64[s]')
