{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5a1ddc14-91c8-46c9-a379-6ed8b6426723",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File not found. Please check the file path.\n"
     ]
    }
   ],
   "source": [
    "#my example\n",
    "# Import necessary libraries\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# Load the car selling dataset\n",
    "# Note: Since you didn't specify the file path, I'll assume it's a CSV file\n",
    "# Replace 'car_sales_data.csv' with your actual file path\n",
    "try:\n",
    "    # Try to load the dataset\n",
    "    df = pd.read_csv('car_sales_data.csv')\n",
    "    \n",
    "    # Display the first few rows to understand the structure\n",
    "    print(\"Dataset Preview:\")\n",
    "    print(df.head())\n",
    "    print(\"\\nDataset Information:\")\n",
    "    print(df.info())\n",
    "    \n",
    "    # Assuming the dataset has 'sale_date' and 'price' columns\n",
    "    # Convert date column to datetime if it exists\n",
    "    if 'sale_date' in df.columns:\n",
    "        df['sale_date'] = pd.to_datetime(df['sale_date'])\n",
    "        \n",
    "        # Calculate the start date for \"last year\" (365 days ago from today)\n",
    "        today = datetime.now()\n",
    "        one_year_ago = today - timedelta(days=365)\n",
    "        \n",
    "        # Filter data for last year\n",
    "        last_year_sales = df[df['sale_date'] >= one_year_ago]\n",
    "        \n",
    "        # Calculate total revenue\n",
    "        if 'price' in df.columns:\n",
    "            total_revenue = last_year_sales['price'].sum()\n",
    "            print(f\"\\nTotal Revenue for Last Year: ${total_revenue:,.2f}\")\n",
    "            \n",
    "            # Create a monthly revenue chart\n",
    "            monthly_revenue = last_year_sales.groupby(pd.Grouper(key='sale_date', freq='M'))['price'].sum()\n",
    "            \n",
    "            plt.figure(figsize=(12, 6))\n",
    "            monthly_revenue.plot(kind='bar')\n",
    "            plt.title('Monthly Revenue - Last Year')\n",
    "            plt.xlabel('Month')\n",
    "            plt.ylabel('Revenue ($)')\n",
    "            plt.xticks(rotation=45)\n",
    "            plt.tight_layout()\n",
    "            plt.show()\n",
    "        else:\n",
    "            print(\"Error: 'price' column not found in the dataset\")\n",
    "    else:\n",
    "        print(\"Error: 'sale_date' column not found in the dataset\")\n",
    "        \n",
    "        # If date column has a different name, suggest checking column names\n",
    "        print(\"Available columns:\", df.columns.tolist())\n",
    "        print(\"Please adjust the code to use the correct date and price column names\")\n",
    "        \n",
    "except FileNotFoundError:\n",
    "    print(\"File not found. Please check the file path.\")\n",
    "except Exception as e:\n",
    "    print(f\"An error occurred: {e}\")\n",
    "    print(\"If you're having trouble loading the dataset, make sure to:\")\n",
    "    print(\"1. Check the file path\")\n",
    "    print(\"2. Verify the file format (CSV, Excel, etc.)\")\n",
    "    print(\"3. Ensure the file is not corrupted\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb5b67f2-8658-42b0-9c4e-b42e8266a3d9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "anaconda-2024.02-py310",
   "language": "python",
   "name": "conda-env-anaconda-2024.02-py310-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
