I have the following unstructured data (read from a csv).
data = [[b'id' b'datetime' b'anomaly_length' b'affected_sensors' b'reason']
[b'1' b'2019-12-20 08:09' b'26' b'all' b'Open Windows']
[b'1' b'2019-12-20 08:10' b'26' b'all' b'Open Windows']
[b'1' b'2019-12-20 08:11' b'26' b'all' b'Open Windows']
[b'1' b'2019-12-20 08:12' b'26' b'all' b'Open Windows']
[b'1' b'2019-12-20 08:13' b'26' b'all' b'Open Windows']
[b'1' b'2019-12-20 08:14' b'26' b'all' b'Open Windows']
[b'1' b'2019-12-20 08:15' b'26' b'all' b'Open Windows']
[b'1' b'2019-12-20 08:16' b'26' b'all' b'Open Windows']
[b'1' b'2019-12-20 08:17' b'26' b'all' b'Open Windows']]
...
I currently create structured arrays by using the following code:
labels_id = np.array(data[1:,0], dtype=int)
labels = [dt.datetime.strptime(date.decode("utf-8"), '%Y-%m-%d %H:%M') for date in np.array(data[1:,1])]
labels_length = np.array(data[1:,2], dtype=int)
This code is necessary because I need data with the correct datatype. In the function, I pass all the arrays and access them by index. I don't like this solution but because the function is called multiple times I don't want to cast the data inside the function each time.
Origin function code:
def special_find(labels_id, labels, labels_length):
for i, id in enumerate(labels_id):
print(id)
print(labels[i])
print(labels_length[i])
...
Expected: I want to have a structured array which only contains the needed columns:
structured_data = [[1 datetime.datetime(2019, 12, 20, 8, 9) b'2019-12-20 08:09' 26],
[1 datetime.datetime(2019, 12, 20, 8, 10) 26],
[1 datetime.datetime(2019, 12, 20, 8, 11) 26],
[1 datetime.datetime(2019, 12, 20, 8, 12) 26],
[1 datetime.datetime(2019, 12, 20, 8, 13) 26],
[1 datetime.datetime(2019, 12, 20, 8, 14) 26],
...
I know I could concat all the created arrays but I don't think this is a good solution. Instead, I am searching for something like this:
structured_data = np.array(data[1:, 0:3], dtype=...)
UPDATE: here are some values for a csv file
id,datetime,anomaly_length,affected_sensors,reason
1,2019-12-20 08:09,26,all,Open Windows
1,2019-12-20 08:10,26,all,Open Windows
1,2019-12-20 08:11,26,all,Open Windows
1,2019-12-20 08:12,26,all,Open Windows
1,2019-12-20 08:13,26,all,Open Windows
1,2019-12-20 08:14,26,all,Open Windows
1,2019-12-20 08:15,26,all,Open Windows
1,2019-12-20 08:16,26,all,Open Windows
1,2019-12-20 08:17,26,all,Open Windows