Created
May 14, 2025 13:07
-
-
Save michael021997/4f4f530eb3b207f5a6b8d7341edcf279 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from datetime import datetime | |
def calculate_monthly_yoy_fuel_burn(input_file, output_file=None): | |
""" | |
Calculate YoY monthly fuel burn from monthly historical data. | |
Args: | |
input_file: Path to the CSV file with monthly fuel burn data | |
output_file: Path to save the results (optional) | |
Returns: | |
DataFrame with the original data and YoY calculations | |
""" | |
# Read the input file | |
df = pd.read_csv(input_file) | |
# Convert Month column to datetime | |
df['Month'] = pd.to_datetime(df['Month']) | |
# Sort by Month | |
df = df.sort_values('Month') | |
# Extract year and month for grouping | |
df['Year'] = df['Month'].dt.year | |
df['MonthNum'] = df['Month'].dt.month | |
# Calculate YoY changes (percentage) directly comparing to same month last year | |
df['YoY_FuelBurn'] = df.groupby('MonthNum')['FuelBurn'].pct_change(periods=1) * 100 | |
df['YoY_Flights'] = df.groupby('MonthNum')['Flights'].pct_change(periods=1) * 100 | |
df['YoY_Hours'] = df.groupby('MonthNum')['Hours'].pct_change(periods=1) * 100 | |
# Calculate Fuel Efficiency metrics | |
df['FuelBurn_per_Flight'] = df['FuelBurn'] / df['Flights'] | |
df['FuelBurn_per_Hour'] = df['FuelBurn'] / df['Hours'] | |
# Calculate YoY changes in efficiency metrics | |
df['YoY_FuelBurn_per_Flight'] = df.groupby('MonthNum')['FuelBurn_per_Flight'].pct_change(periods=1) * 100 | |
df['YoY_FuelBurn_per_Hour'] = df.groupby('MonthNum')['FuelBurn_per_Hour'].pct_change(periods=1) * 100 | |
# Save to output file if specified | |
if output_file: | |
df.to_csv(output_file, index=False) | |
print(f"Results saved to {output_file}") | |
return df | |
def visualize_monthly_yoy_data(df): | |
""" | |
Create visualizations of the monthly YoY data. | |
Args: | |
df: DataFrame with YoY calculations | |
""" | |
# Filter out NaN values (first year won't have YoY data) | |
df_viz = df.dropna(subset=['YoY_FuelBurn']) | |
# Set up the figure | |
plt.figure(figsize=(12, 9)) | |
# Plot 1: YoY Fuel Burn | |
plt.subplot(2, 1, 1) | |
plt.plot(df_viz['Month'], df_viz['YoY_FuelBurn'], 'b-', label='YoY Fuel Burn (%)') | |
plt.title('Year-over-Year Monthly Fuel Burn') | |
plt.xlabel('Month') | |
plt.ylabel('Percent Change (%)') | |
plt.grid(True) | |
plt.legend() | |
# Plot 2: YoY Flights vs YoY Hours | |
plt.subplot(2, 2, 3) | |
plt.plot(df_viz['Month'], df_viz['YoY_Flights'], 'g-', label='YoY Flights (%)') | |
plt.plot(df_viz['Month'], df_viz['YoY_Hours'], 'r-', label='YoY Hours (%)') | |
plt.title('YoY Flights vs Hours') | |
plt.xlabel('Month') | |
plt.ylabel('Percent Change (%)') | |
plt.grid(True) | |
plt.legend() | |
# Plot 3: YoY Efficiency Metrics | |
plt.subplot(2, 2, 4) | |
plt.plot(df_viz['Month'], df_viz['YoY_FuelBurn_per_Flight'], 'c-', label='YoY Fuel per Flight (%)') | |
plt.plot(df_viz['Month'], df_viz['YoY_FuelBurn_per_Hour'], 'm-', label='YoY Fuel per Hour (%)') | |
plt.title('YoY Efficiency Metrics') | |
plt.xlabel('Month') | |
plt.ylabel('Percent Change (%)') | |
plt.grid(True) | |
plt.legend() | |
plt.tight_layout() | |
plt.savefig('monthly_yoy_fuel_burn_analysis.png') | |
print("Visualization saved to monthly_yoy_fuel_burn_analysis.png") | |
plt.show() | |
def main(): | |
"""Main function to run the YoY fuel burn calculator.""" | |
input_file = "argus_fuelburn_yrmo.csv" | |
output_file = "argus_fuelburn_monthly_yoy.csv" | |
print(f"Processing {input_file}...") | |
df = calculate_monthly_yoy_fuel_burn(input_file, output_file) | |
# Print sample of results | |
print("\nSample of calculated YoY data:") | |
# Get a sample of results that have YoY data (not NaN) | |
sample_df = df.dropna(subset=['YoY_FuelBurn']).head(5) | |
# Format the display for better readability | |
pd.set_option('display.float_format', '{:.2f}'.format) | |
display_cols = ['Month', 'FuelBurn', 'YoY_FuelBurn'] | |
print(sample_df[display_cols].to_string(index=False)) | |
# Print summary statistics | |
print("\nSummary statistics for YoY Fuel Burn (%):") | |
print(f"Mean: {df['YoY_FuelBurn'].mean():.2f}%") | |
print(f"Median: {df['YoY_FuelBurn'].median():.2f}%") | |
print(f"Min: {df['YoY_FuelBurn'].min():.2f}%") | |
print(f"Max: {df['YoY_FuelBurn'].max():.2f}%") | |
# Create visualizations | |
print("\nCreating visualizations...") | |
visualize_monthly_yoy_data(df) | |
# Return the DataFrame for further analysis if needed | |
return df | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment