Advertisement
KillianMills

time_shifter.py

Dec 1st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. """
  2. Unable to get the timestamp_shift working on the billing tool
  3. Taking matters into my own hands
  4.  
  5. In this case, these  is 5 hours behind the CDRs that we have
  6. """
  7.  
  8. import pandas as pd
  9.  
  10.  
  11. input_file = "sample.csv"
  12. output_file = "time_fixed.csv"
  13.  
  14. # set this to the amount of hours you want to roll forward or backwards
  15. time_shift = -5
  16.  
  17. date_time = 2
  18.  
  19.  
  20. def print_checks(input_df, time_shift, date_time):
  21.     print(input_df.head())
  22.  
  23.     print("__________")
  24.  
  25.     print(time_shift)
  26.  
  27.     print("__________")
  28.  
  29.     print(input_df[input_df.columns[date_time]])
  30.  
  31.  
  32.  
  33. def time_shifter(input_df, time_shift, date_time):
  34.     print_checks(input_df, time_shift, date_time)
  35.  
  36.  
  37.     input_df.set_index(input_df.columns[date_time], inplace=True)
  38.     print(input_df.head())
  39.     #input_df[input_df.columns[date_time]] = input_df.to_datetime(input_df.columns[date_time])
  40.     input_df.index = input_df.index.to_datetime()
  41.  
  42.     input_df.index = input_df.index + pd.DateOffset(hours=time_shift)
  43.     print(input_df.head())
  44.  
  45.     input_df.reset_index(inplace=True)
  46.  
  47.  
  48.     return input_df
  49.  
  50.  
  51.  
  52.  
  53. input_df = pd.read_csv(input_file)
  54. output_df = time_shifter(input_df, time_shift, date_time)
  55. output_df.to_csv(output_file, index=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement