Reputation: 2046
I have an array that looks like this:
array([( 0, 1579014911203, b"19'03.5", b'2'),
( 1, 1579014911560, b"19'03.5", b'30'),
( 2, 1579014912968, b"19'04.0", b'23'), ...,
(14532, 1579124953407, b"19'12.0", b'15'),
(14533, 1579124955508, b"19'11.5", b'50'),
(14534, 1579124966108, b"19'11.5", b'27'],
dtype={'names':['order','time','degree','speed'], 'formats':['<u4','<u8','S15','S15']})
I can change the type of speed
using astype
, but how can I change the type of degree
into float
or decimal
? I want to remove any characters except for numbers and a dot. How could I do this without using Pandas?
Upvotes: 1
Views: 186
Reputation: 4537
I don't now of another way than looping over your data and transforming the string into a valid float. Depending on the notation you could use:
import re
import numb as np
degrees = np.zeros_like(arr['degree'], dtype=float)
for i, deg in enumerate(arr['degree']):
# Find all numbers in the string
numbers = re.findall(r"\d+", str(deg))
# Transform the numbers to your desired float
deg_new = float(numbers[0] + numbers[1] + '.' + numbers[2])
# deg_new = int(numbers[0]) + float(numbers[1] + '.' + numbers[2])/60
# deg_new = int(numbers[0]) + int(numbers[1])/60 + int(numbers[2])/3600
degrees[i] = deg_new
Upvotes: 1