Change function to match for loop
Question:
I am trying to re-write this for loop into a function to make it applicable to different time series. It is pretty straightforward but for some reason there are errors with the function. The goal is to subtract the current value with its preceding one:
# time series
p = np.array([2,5,7,23,78,2,14,7,3])
diff = [np.float(p[0])]
one_year = 1
for i in range(1,len(p)):
value = p[i] - p[i - one_year]
diff.append(value)
print(diff)
[2.0, 3, 2, 16, 55, -76, 12, -7, -4]
This is the function I want to make, and I keep getting this error about appending values:
def differing(data,time_interval):
diff = [data[0]]
for i in range(1,len(data)):
value = data[i] - data[i - time_interval]
diff = diff.append(value)
return diff
test = differing(p,1)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-410-5287a79bf3fd> in <module>
----> 1 test = differing(p,1)
<ipython-input-391-8787130f5f66> in differing(data, time_interval)
3 for i in range(1,len(data)):
4 value = data[i] - data[i - time_interval]
----> 5 diff = diff.append(value)
6 #output = np.array(diff.append(value))
7 return diff
AttributeError: 'NoneType' object has no attribute 'append'
How do I fix this?
Answers:
append
doesn’t return the modified list, it modifies it in place. This means that
diff = diff.append(value)
will be assigning None
to diff
, causing your problem.
You just need
diff.append(value)
as you had in the original loop.
I am trying to re-write this for loop into a function to make it applicable to different time series. It is pretty straightforward but for some reason there are errors with the function. The goal is to subtract the current value with its preceding one:
# time series
p = np.array([2,5,7,23,78,2,14,7,3])
diff = [np.float(p[0])]
one_year = 1
for i in range(1,len(p)):
value = p[i] - p[i - one_year]
diff.append(value)
print(diff)
[2.0, 3, 2, 16, 55, -76, 12, -7, -4]
This is the function I want to make, and I keep getting this error about appending values:
def differing(data,time_interval):
diff = [data[0]]
for i in range(1,len(data)):
value = data[i] - data[i - time_interval]
diff = diff.append(value)
return diff
test = differing(p,1)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-410-5287a79bf3fd> in <module>
----> 1 test = differing(p,1)
<ipython-input-391-8787130f5f66> in differing(data, time_interval)
3 for i in range(1,len(data)):
4 value = data[i] - data[i - time_interval]
----> 5 diff = diff.append(value)
6 #output = np.array(diff.append(value))
7 return diff
AttributeError: 'NoneType' object has no attribute 'append'
How do I fix this?
append
doesn’t return the modified list, it modifies it in place. This means that
diff = diff.append(value)
will be assigning None
to diff
, causing your problem.
You just need
diff.append(value)
as you had in the original loop.