Time Series Data
Python tutorial · PySpark.in
What is Time Series Data?
Time Series Data is data collected over time at regular intervals, where each observation is associated with a date or time.
Why Time Series is Important?
✔ Trend analysis
✔ Seasonality detection
✔ Forecasting
✔ Financial analysis
Sample Time Series Dataset (Used Everywhere)
```
import pandas as pd
data = {
"Date": ["2024-01-01", "2024-01-02", "2024-01-03",
"2024-01-04", "2024-01-05"],
"Sales": [200, 220, 210, 250, 300]
}
df = pd.DataFrame(data)
print(df)
```
Date & Time Handling
Date & Time handling means working with dates, extracting parts, and performing time-based operations.
Practical (Problem)
Currently Date is string (object).
```
print(df.dtypes)
```
to_datetime(): converts string/object date into datetime format.It is mandatory for time series operations.
Practical
```
df["Date"] = pd.to_datetime(df["Date"])
print(df.dtypes)
```
Extract Date Components
```
df["Year"] = df["Date"].dt.year
df["Month"] = df["Date"].dt.month
df["Day"] = df["Date"].dt.day
print(df)
```
Date Indexing
Setting date column as index for time-based operations.It is required for resampling
Practical
```
df.set_index("Date", inplace=True)
print(df)
```
Select by Date
```
print(df.loc["2024-01-03"])
```
Resampling
Resampling means changing the frequency of time series data.It is similar to groupby but time-based
Daily → Monthly
```
Resample=df.resample("M").sum()
print(Resample)
```
Weekly Average
```
Wa=df.resample("W").mean()
print(Wa)
```
Common Resample Codes
Code | Meaning |
|---|---|
D | Daily |
W | Weekly |
M | Monthly |
Y | Yearly |
Time Shifting (shift())
shift() moves data forward or backward in time. It is used for lag features
Practical
```
df["Prev_Day_Sales"] = df["Sales"].shift(1)
print(df)
```
Forward Shift
```
df["Next_Day_Sales"] = df["Sales"].shift(-1)
print(df)
```
Rolling Window Functions
Rolling window applies calculations over a moving window. It is used in stock analysis, smoothing
Practical: Rolling Mean (Window = 3)
```
df["Rolling_Avg"] = df["Sales"].rolling(window=3).mean()
print(df)
```
Rolling Sum
```
df["Rolling_Sum"] = df["Sales"].rolling(2).sum()
print(df)
```
Difference: Resample vs Rolling
Resample | Rolling |
|---|---|
Change frequency | Moving window |
Time-based grouping | Smoothing |
Monthly, yearly | Trends |
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges