I’m building a project for the Raspberry Pi that turns a relay on and off random times in a specific time window. To manage the time slots, I want to use a two-dimensional array that’s generated daily. So, my Python application needs to empty the previous day’s array, and populate it with the on/off time slots being generated randomly. I can’t figure out how to append my time values to the array. Can someone help me? Here’s my code:
daily_slots = np.empty([1], dtype=[('onTime', np.dtype(int)), ('offTime', np.dtype(int))])# numpy populates the array with whatever is in memory at that time,# so delete the existing, 'empty' array rowdaily_slots = np.delete(daily_slots, 0, 0)
With that in place, how do I append values? The numpy documentation says I should do something like the following:
daily_slots = np.append(daily_slots, [700, 800])
But that’s not working, I get
Traceback (most recent call last):File "./controller.py", line 351, in <module>init_app()File "./controller.py", line 128, in init_appbuild_daily_slots_array()File "./controller.py", line 307, in build_daily_slots_arraydaily_slots = np.append(daily_slots, [700, 800])File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 3555, in appendreturn concatenate((arr, values), axis=axis)
TypeError: invalid type promotion
I've tried everything I can think of and I've poked and prodded at the numpy documentation and I've not found the answer.
Am I not declaring the array correctly? I want each array 'row' to consist of a two-element array (the ontime and offtime).