CryptoMarketParser/utils/converty_csv_to_day.py
2025-05-13 16:13:52 +08:00

49 lines
1.6 KiB
Python

import pandas as pd
from datetime import datetime
def aggregate_to_daily(input_csv, output_csv):
"""
Load a CSV file with Bitcoin price data, aggregate to daily values, and save to a new CSV.
Args:
input_csv (str): Path to the input CSV file
output_csv (str): Path to save the output CSV file
"""
try:
# Read the CSV file
print(f"Loading data from {input_csv}...")
df = pd.read_csv(input_csv)
# Convert timestamp to datetime
df['Timestamp'] = pd.to_datetime(df['Timestamp'], unit='s')
df.set_index('Timestamp', inplace=True)
# Aggregate to daily values
print("Aggregating data to daily values...")
daily_df = df.resample('D').agg({
'Open': 'first',
'High': 'max',
'Low': 'min',
'Close': 'last',
'Volume': 'sum'
})
# Reset index to make Timestamp a column
daily_df.reset_index(inplace=True)
# Save to new CSV
print(f"Saving daily data to {output_csv}...")
daily_df.to_csv(output_csv, index=False)
print(f"Successfully processed {len(df)} records into {len(daily_df)} daily records")
except Exception as e:
print(f"Error processing data: {str(e)}")
if __name__ == "__main__":
# Example usage
input_file = "../data/btcusd_1-min_data.csv" # Update this path to your input file
output_file = "../data/btcusd_daily_data.csv" # Update this path to your desired output file
aggregate_to_daily(input_file, output_file)