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)
Convert Unix timestamp to datetime (assuming Unix timestamp is in milliseconds)
df[‘Date’] = pd.to_datetime(df[‘Unix’], unit=‘ms’)
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()
some example data of my csv files: Unix Date Symbol Open High Low Close volume_crypto volume_base tradecount 1.60887E+12 12/25/2020 5:00 1INCHBTC 8.51E-06 0.000168 8.51E-06 0.00010947 2207916 256.8839429 17168 1.60888E+12 12/25/2020 6:00 1INCHBTC 0.00010947 0.00011329 9.41E-05 0.00010693 1833765.2 186.9121981 11343 1.60888E+12 12/25/2020 7:00 1INCHBTC 0.00010634 0.00012298 0.00010047 0.00011025 2067113.5 236.2882339 12743 1.60888E+12 12/25/2020 8:00 1INCHBTC 0.00011162 0.0001195 0.00010347 0.00011029 1570496.5 175.0973559 10418 1.60889E+12 12/25/2020 9:00 1INCHBTC 0.00011007 0.000116 0.00010718 0.00011023 862685.8 96.20224944 6533 1.60889E+12 12/25/2020 10:00 1INCHBTC 0.0001103 0.00012444 0.0001103 0.00011533 2052454.4 242.2437374 13444 1.60889E+12 12/25/2020 11:00 1INCHBTC 0.00011533 0.00011999 0.0001024 0.00010768 1410575.3 157.3953309 9802 1.6089E+12 12/25/2020 12:00 1INCHBTC 0.00010774 0.0001131 9.71E-05 9.89E-05 1265864.7 134.7208468 10978
- Assistant
The error message you're encountering, "None of ['Date'] are in the columns", occurs because when you load the data with , you skip the first row (which presumably contains the header names) using . This means pandas generates automatic column names (0, 1, 2, ...) for the DataFrame, and your subsequent code that refers to column names like 'Date', 'Open', etc., will fail because those named columns do not exist.