code: def create4h(filename, csvpath, outdirectorypath): try: outputpath =…
- User
code:
def create_4h(filename, csv_path, out_directory_path): try: output_path = os.path.join(out_directory_path, f"4h_{filename}") if os.path.exists(output_path): print(f"{filename} already exists. Skipping aggregate.") return
code# Load the dataset df = pd.read_csv(csv_path, parse_dates=['Date']) df['Date'] = pd.to_datetime(df['Date']) # Ensure the 'Date' column is the index df.set_index('Date', inplace=True) # Resample data to 4-hour periods # 'ohlc' computes the open price of the first record, # the high price of the highest record, the low price of the lowest record, # and the close price of the final record within each group df_resampled = df.resample('4h', on='Date').agg({ 'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last', 'volume_crypto': 'sum', 'volume_base': 'sum', 'tradecount': 'sum' }) # Reset index to add the Date column back df_resampled.reset_index(inplace=True) # Save the 4-hour timeframe data to a new CSV df_resampled.to_csv(output_path, index=False) print(f"success {output_path}") except Exception as e: print(f"Failed to process {csv_path} due to {e}")example of my data:
Unix Date Symbol Open High Low Close volume_crypto volume_base tradecount 1.50294E+12 8/17/2017 4:00 BTCUSDT 4261.48 4313.62 4261.32 4308.83 47.181009 202366.1384 171 1.50295E+12 8/17/2017 5:00 BTCUSDT 4308.83 4328.69 4291.37 4315.32 23.234916 100304.8236 102 1.50295E+12 8/17/2017 6:00 BTCUSDT 4330.29 4345.45 4309.37 4324.35 7.229691 31282.31267 36 1.50295E+12 8/17/2017 7:00 BTCUSDT 4316.62 4349.99 4287.41 4349.99 4.443249 19241.0583 25 1.50296E+12 8/17/2017 8:00 BTCUSDT 4333.32 4377.85 4333.32 4360.69 0.972807 4239.503586 28 1.50296E+12 8/17/2017 9:00 BTCUSDT 4360 4445.78 4360 4444 10.763623 47219.35594 43 1.50296E+12 8/17/2017 10:00 BTCUSDT 4441.1 4470 4399.81 4460 24.865399 110290.729 82 1.50297E+12 8/17/2017 11:00 BTCUSDT 4460 4485.39 4427.3 4427.3 27.018053 120751.5692 95
- Assistant
- User
time data "2017-11-21 02:00:00" doesn't match format "%m/%d/%Y %H:%M", at position 0. You might want to try: - passing
formatif your strings have a consistent format; - passingformat='ISO8601'if your strings are all ISO8601 but not necessarily in exactly the same format; - passingformat='mixed', and the format will be inferred for each element individually. You might want to usedayfirstalongside this. - Assistant
The error message indicates that the date string "2017-11-21 02:00:00" doesn't match the format "%m/%d/%Y %H:%M", which is the format you specified for parsing. The date string, however, is in a different format corresponding to "%Y-%m-%d %H:%M:%S". There might be mixed date formats in your dataset, or the actual format of dates might be different from what you expected based on the sample data provided earlier.