Advertisement
jack06215

[Python] time as sine cosine values

Jun 13th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3.  
  4. def rand_times(n):
  5.     """Generate n rows of random 24-hour times (seconds past midnight)"""
  6.     rand_seconds = np.random.randint(0, 24*60*60, n)
  7.     return pd.DataFrame(data=dict(seconds=rand_seconds))
  8.  
  9. # Create raw data
  10. n_rows = 1000
  11.  
  12. df = rand_times(n_rows)
  13. # sort for the sake of graphing
  14. df = df.sort_values('seconds').reset_index(drop=True)
  15. df.seconds.plot()
  16.  
  17. # Sine & cosine transform
  18. seconds_in_day = 24*60*60
  19.  
  20. df['sin_time'] = np.sin(2*np.pi*df.seconds/seconds_in_day)
  21. df['cos_time'] = np.cos(2*np.pi*df.seconds/seconds_in_day)
  22.  
  23. df.drop('seconds', axis=1, inplace=True)
  24.  
  25. # Combine the two yield a cyclical pattern
  26. df.sin_time.plot()
  27. df.cos_time.plot()
  28. df.sample(30).plot.scatter('sin_time','cos_time').set_aspect('equal')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement