{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyP4NJQtGdN2hKjuY6z95NLe", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Time Series Forecasting with Pandas\n", "## Learn how Python handles time-based data and lays the foundation for forecasting\n", "\n", "\n", "| ![space-1.jpg](https://github.com/Tanu-N-Prabhu/Python/blob/master/Img/Time%20Series%20Forecasting%20with%20Pandas.png?raw=true) |\n", "|:--:|\n", "| Image Generated Using Canva|\n", "\n", "\n", "### Why it’s so important\n", "Time series data is everywhere; stock prices, weather reports, sensor data, website traffic. If you can master how to work with time, you can unlock powerful insights and predict the future.\n", "\n", "Pandas makes it incredibly intuitive to handle time-indexed data, re-sample it, and prep it for machine learning models.\n", "\n", "In this article, let’s explore Pandas’s core time series operations, laying the groundwork for real-world forecasting models.\n", "\n", "---\n", "\n", "### Key Concepts Covered\n", "* How `DateTimeIndex` supercharges time-aware operations\n", "* Resampling vs. frequency conversion: `resample() vs. asfreq()`\n", "* Rolling windows and moving averages for smoothing\n", "* Time-shifting data for lag analysis\n", "* Handling missing time entries cleanly\n", "\n", "---\n", "\n", "### Fun Fact\n", "Pandas can automatically parse date columns and even infer frequencies, making it possible to clean and analyze time data with just a few lines of code.\n", "\n", "---\n", "\n", "### Implementation" ], "metadata": { "id": "GR67xFgeYmr2" } }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IUDHmM3_Yc4c", "outputId": "707fceb7-5a4f-471e-b665-3c3c0af76f35" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " value rolling_avg prev_day\n", "2023-01-01 173 NaN NaN\n", "2023-01-02 139 NaN 173.0\n", "2023-01-03 176 162.666667 139.0\n", "2023-01-04 111 142.000000 176.0\n", "2023-01-05 172 153.000000 111.0\n", "2023-01-06 164 149.000000 172.0\n", "2023-01-07 179 171.666667 164.0\n", "2023-01-08 149 164.000000 179.0\n", "2023-01-09 109 145.666667 149.0\n", "2023-01-10 107 121.666667 109.0\n", "Resampled:\n", " value\n", "2023-01-01 162.666667\n", "2023-01-04 149.000000\n", "2023-01-07 145.666667\n", "2023-01-10 107.000000\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# Create time series data\n", "dates = pd.date_range(start=\"2023-01-01\", periods=10, freq=\"D\")\n", "data = np.random.randint(100, 200, size=(10,))\n", "df = pd.DataFrame({'value': data}, index=dates)\n", "\n", "# Resample to every 3 days and get the mean\n", "resampled = df.resample('3D').mean()\n", "\n", "# Calculate rolling average\n", "df['rolling_avg'] = df['value'].rolling(window=3).mean()\n", "\n", "# Shift data for comparison (e.g., yesterday’s value)\n", "df['prev_day'] = df['value'].shift(1)\n", "\n", "print(df)\n", "print(\"Resampled:\\n\", resampled)" ] }, { "cell_type": "markdown", "source": [ "\n", "\n", "> ***This is the backbone of forecasting models. Whether you’re predicting sales or analyzing sensor patterns, these time-based manipulations are the first step toward insights!***\n", "\n", "---\n", "\n", "### Before you go\n", "* Be sure to Like and Connect Me\n", "* Follow Me : [Medium](https://medium.com/@tanunprabhu95) | [GitHub](https://github.com/Tanu-N-Prabhu) | [LinkedIn](https://ca.linkedin.com/in/tanu-nanda-prabhu-a15a091b5) | [Python Hub](https://github.com/Tanu-N-Prabhu/Python)\n", "* [Check out my latest articles on Programming](https://medium.com/@tanunprabhu95)\n", "* Check out my [GitHub](https://github.com/Tanu-N-Prabhu) for code and [Medium](https://medium.com/@tanunprabhu95) for deep dives!\n", "\n", "\n", "\n" ], "metadata": { "id": "FuZbSrr8ZCoY" } } ] }