i have following code : import pandas as pd import os from concurrent.futures import…
- User
i have following code : import pandas as pd import os from concurrent.futures import ThreadPoolExecutor, as_completed
def create_weekly(filename, csv_path, out_directory_path): try: # Load the dataset df = pd.read_csv(csv_path)
codedf['Date'] = pd.to_datetime(df['Date'], format="ISO8601", utc=True) # 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('W').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) output_path = os.path.join(out_directory_path, f"w_{filename}") # 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}")def process_files(directory, out_directory_path): with ThreadPoolExecutor() as executor: futures = [] for filename in os.listdir(directory): if filename.endswith('.csv'): csv_path = os.path.join(directory, filename) print(f"Processing {filename}…") futures.append(executor.submit(create_weekly, filename, csv_path, out_directory_path))
codefor future in as_completed(futures): pass # Future results or exceptions can be processed here if neededdef main(): # Replace the path with the directory containing your CSV files directory_path = r"..\downloaded_files\Binance\00_rename_columns_sort\1h_spot" out_directory_path = r"..\downloaded_files\Binance\00_rename_columns_sort\week_spot" process_files(directory_path, out_directory_path)
- Assistant