Guest User

Untitled

a guest
Nov 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. #%% make some data frame for testing
  6.  
  7. phi = np.linspace(0, 2*np.pi, 1024)
  8. df = pd.DataFrame({
  9. 'cos': np.cos(phi),
  10. 'sin': np.sin(phi),
  11. 'cosxsin': np.cos(phi) * np.sin(phi)
  12. }, index=phi)
  13.  
  14. #%% function which resamples a dataframe to a new number of rows
  15. def resample_fixed(df, n_new):
  16. n_old, m = df.values.shape
  17. mat_old = df.values
  18. mat_new = np.zeros((n_new, m))
  19. x_old = np.linspace(df.index.min(), df.index.max(), n_old)
  20. x_new = np.linspace(df.index.min(), df.index.max(), n_new)
  21.  
  22. for j in range(m):
  23. y_old = mat_old[:, j]
  24. y_new = np.interp(x_new, x_old, y_old)
  25. mat_new[:, j] = y_new
  26.  
  27. return pd.DataFrame(mat_new, index=x_new, columns=df.columns)
  28.  
  29. #%% resample the data frame and show the values
  30.  
  31. df2 = resample_fixed(df, 100)
  32. print(df2.shape)
  33. df2.plot()
Add Comment
Please, Sign In to add comment