Guest User

Untitled

a guest
Jun 11th, 2021
40
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2. import numpy as np
  3. import timeit
  4.  
  5. from math import log
  6.  
  7. my_stat = pd.DataFrame({'x': np.random.random(size=10000)})
  8.  
  9. def with_pd():
  10.     my_stat['y'] = my_stat['x'].map(log)
  11.    
  12. def with_pd_numexpr():
  13.     my_stat.eval('y = log(x)', engine='numexpr', inplace=True)
  14.  
  15. def with_np():
  16.     my_stat['y'] = np.log(my_stat['x'])
  17.    
  18. print(timeit.timeit('with_pd()', setup='from __main__ import with_pd', number = 100)) #0.525s
  19. print(timeit.timeit('with_pd_numexpr()', setup='from __main__ import with_pd_numexpr', number = 100)) #0.305s
  20. print(timeit.timeit('with_np()', setup='from __main__ import with_np', number = 100)) #0.122s
RAW Paste Data