code: import pandas as pd import os from concurrent.futures import ThreadPoolExecutor,…
- User
code: import pandas as pd import os from concurrent.futures import ThreadPoolExecutor, as_completed
def load_data(csv_path): df = pd.read_csv(csv_path, skiprows=1) return df
def aggregate_to_4h(df): df.set_index(‘Date’, inplace=True)
Aggregate according to the specified column names
aggregation = { ‘Open’: ‘first’, ‘High’: ‘max’, ‘Low’: ‘min’, ‘Close’: ‘last’, ‘volume_crypto’: ‘sum’, ‘volume_base’: ‘sum’, ‘tradecount’: ‘sum’ }
df_4h = df.resample(‘4H’).agg(aggregation) df.sort_index(inplace=True) df_4h[‘Open’] = df.groupby(pd.Grouper(freq=‘4H’))[‘Open’].first().values return df_4h
def create_4h(filename, csv_path, out_directory_path): try: df = load_data(csv_path)
df_4h = aggregate_to_4h(df)
Save the aggregated data to a new CSV file
output_path = os.path.join(out_directory_path, f’4h_{filename}‘) df_4h.to_csv(output_path) print(f"4h created {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_4h, filename, csv_path, out_directory_path))
for future in as_completed(futures): pass # Future results or exceptions can be processed here if needed
def 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\4h_spot" process_files(directory_path, out_directory_path)
main()
- Assistant