Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pandas import DataFrame
- t = {'time': ['08:35', '08:38', '13:42', '13:46']}
- df = DataFrame(t)
- import numpy as np
- time_array = np.array(df.time)
- print time_array
- ['08:35' '08:38' '13:42' '13:46']
- for i in range(len(time_array)):
- print np.fromstring(time_array[i], dtype=int, sep=":")
- [ 8 35]
- [ 8 38]
- [13 42]
- [13 46]
- def foo(array):
- for i in range(len(array)):
- array[i] = np.fromstring(array[i], dtype=int, sep=':')
- %timeit foo(time_array)
- def foo2(df):
- df['hour'] = df['time'].apply(lambda x: int(x.split(':')[0]))
- df['minute'] = df['time'].apply(lambda x: int(x.split(':')[1]))
- %timeit foo2(df)
- import time
- def foo3(df):
- df['hour'] = df['time'].apply(lambda x: time.strptime(x, '%H:%M').tm_hour)
- df['minute'] = df['time'].apply(lambda x: time.strptime(x, '%H:%M').tm_min)
- %timeit foo3(df)
- from pandas import DataFrame
- t = {'time': ['08:35', '08:38', '13:42', '13:46']}
- df = DataFrame(t)
- df['hour'] = df['time'].apply(lambda x: int(x.split(':')[0]))
- df['minute'] = df['time'].apply(lambda x: int(x.split(':')[1]))
- print(df)
- time hour minute
- 0 08:35 8 35
- 1 08:38 8 38
- 2 13:42 13 42
- 3 13:46 13 46
- import time
- df['hour'] = df.timestring.apply(lambda x: time.strptime(x, '%H:%M').tm_hour)
- df['minute'] = df.timestring.apply(lambda x: time.strptime(x, '%H:%M').tm_min)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement