SHARE
TWEET

Python - pandas - filter by date and time

furas Jun 15th, 2018 (edited) 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2.  
  3. #
  4. # Filter rows by strings with date and time.
  5. # It will not work if date is in format 04-06-2018 .
  6. #
  7.  
  8. text = '''
  9. 293  2018-06-14  14:55  1832.15  1833.40  1831.90  1833.35   37947
  10. 294  2018-06-14  15:00  1833.40  1834.95  1833.30  1834.90  106271
  11. 295  2018-06-14  15:05  1834.95  1835.05  1834.40  1834.85  102107
  12. 296  2018-06-14  15:10  1834.80  1834.85  1832.95  1833.80   95931
  13. 297  2018-06-14  15:15  1833.75  1834.85  1833.05  1834.85  124639
  14. 298  2018-06-14  15:20  1834.85  1834.85  1831.55  1832.50  121206
  15. '''
  16.  
  17. data = [line.split() for line in text.split('\n') if line.strip()]
  18.  
  19. df = pd.DataFrame(data, columns=['index', 'Date','Time','open','high','low','close','volume'])
  20. df.set_index('index')
  21.  
  22. d1 = '2018-06-14'
  23. t1 = '15:00'
  24. t2 = '15:15'
  25.  
  26. new_df = df[ (df["Date"] == d1) & (df["Time"] == t1) ]
  27. print(new_df)
  28.  
  29. new_df = df[ (df["Date"] == d1) & (df["Time"] >= t1) & (df["Time"] <= t2) ]
  30. print(new_df)
  31. print(new_df["high"].max())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top