Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. from pandas import DataFrame
  2. t = {'time': ['08:35', '08:38', '13:42', '13:46']}
  3. df = DataFrame(t)
  4.  
  5. import numpy as np
  6. time_array = np.array(df.time)
  7. print time_array
  8.  
  9. ['08:35' '08:38' '13:42' '13:46']
  10.  
  11. for i in range(len(time_array)):
  12. print np.fromstring(time_array[i], dtype=int, sep=":")
  13.  
  14. [ 8 35]
  15. [ 8 38]
  16. [13 42]
  17. [13 46]
  18.  
  19. def foo(array):
  20. for i in range(len(array)):
  21. array[i] = np.fromstring(array[i], dtype=int, sep=':')
  22.  
  23. %timeit foo(time_array)
  24.  
  25. def foo2(df):
  26. df['hour'] = df['time'].apply(lambda x: int(x.split(':')[0]))
  27. df['minute'] = df['time'].apply(lambda x: int(x.split(':')[1]))
  28.  
  29. %timeit foo2(df)
  30.  
  31. import time
  32. def foo3(df):
  33. df['hour'] = df['time'].apply(lambda x: time.strptime(x, '%H:%M').tm_hour)
  34. df['minute'] = df['time'].apply(lambda x: time.strptime(x, '%H:%M').tm_min)
  35.  
  36. %timeit foo3(df)
  37.  
  38. from pandas import DataFrame
  39. t = {'time': ['08:35', '08:38', '13:42', '13:46']}
  40. df = DataFrame(t)
  41. df['hour'] = df['time'].apply(lambda x: int(x.split(':')[0]))
  42. df['minute'] = df['time'].apply(lambda x: int(x.split(':')[1]))
  43. print(df)
  44.  
  45. time hour minute
  46. 0 08:35 8 35
  47. 1 08:38 8 38
  48. 2 13:42 13 42
  49. 3 13:46 13 46
  50.  
  51. import time
  52. df['hour'] = df.timestring.apply(lambda x: time.strptime(x, '%H:%M').tm_hour)
  53. df['minute'] = df.timestring.apply(lambda x: time.strptime(x, '%H:%M').tm_min)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement