NumPy – Time Series Analysis
Time series analysis is the process of analyzing data points that are collected over time.
Examples include:
- Stock prices 📈
- Weather data 🌦
- Website traffic 🌐
- Sensor readings 📡
- Sales data 💰
In Python, NumPy is one of the core libraries used for numerical time-based data processing.
What is Time Series Data?
Time series data is:
A sequence of values recorded at different points in time.
Example:
| Time | Value |
|---|---|
| 10:00 | 20 |
| 10:01 | 25 |
| 10:02 | 18 |
Import NumPy
import numpy as np
1. Creating Time Series Data in NumPy
import numpy as np
data = np.array([100, 120, 130, 125, 140, 150])
print(data)
Output:
[100 120 130 125 140 150]
2. Creating Time Index (Simulated Time)
NumPy doesn’t directly handle dates, but we can simulate time:
import numpy as np
time = np.arange(6) # represents time steps
data = np.array([100, 120, 130, 125, 140, 150])
print(time)
print(data)
Output:
[0 1 2 3 4 5]
[100 120 130 125 140 150]
3. Calculating Differences (Trend Analysis)
We use np.diff() to find changes over time.
import numpy as np
data = np.array([100, 120, 130, 125, 140, 150])
changes = np.diff(data)
print(changes)
Output:
[ 20 10 -5 15 10]
Meaning:
- Shows how values change between time steps
4. Cumulative Sum (Running Total)
import numpy as np
data = np.array([10, 20, 30, 40])
cumulative = np.cumsum(data)
print(cumulative)
Output:
[10 30 60 100]
Use Case:
- Revenue tracking
- Growth analysis
5. Moving Average (Smoothing Data)
Moving average helps reduce noise in time series data.
import numpy as np
data = np.array([10, 20, 30, 40, 50])
window = 3
moving_avg = np.convolve(data, np.ones(window)/window, mode='valid')
print(moving_avg)
Output:
[20. 30. 40.]
6. Detecting Trends (Simple Comparison)
import numpy as np
data = np.array([100, 110, 120, 130, 125, 140])
trend = np.where(data > np.mean(data), "High", "Low")
print(trend)
Output:
['Low' 'Low' 'High' 'High' 'High' 'High']
7. Normalizing Time Series Data
Normalization helps scale data between 0 and 1.
import numpy as np
data = np.array([100, 200, 300, 400])
normalized = (data - np.min(data)) / (np.max(data) - np.min(data))
print(normalized)
Output:
[0. 0.33 0.66 1. ]
8. Reshaping Time Series Data
import numpy as np
data = np.arange(12)
reshaped = data.reshape(3, 4)
print(reshaped)
Output:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
9. Handling Missing Values (NaN)
import numpy as np
data = np.array([100, np.nan, 130, 140])
cleaned = np.nan_to_num(data, nan=0)
print(cleaned)
Output:
[100. 0. 130. 140.]
Real-World Applications
Time series analysis is used in:
📈 Finance
- Stock price prediction
- Market trend analysis
🌦 Weather Forecasting
- Temperature prediction
- Rainfall trends
🌐 Web Analytics
- User traffic tracking
- Engagement monitoring
📦 Business Sales
- Revenue forecasting
- Demand prediction
Advantages of Using NumPy
- Fast numerical computation
- Efficient array handling
- Supports large datasets
- Easy mathematical operations
- Foundation for data science tools
Limitations
- No built-in date/time handling
- Limited statistical modeling
- Often combined with Pandas for real projects
NumPy vs Pandas for Time Series
| Feature | NumPy | Pandas |
|---|---|---|
| Speed | Very fast | Fast |
| Date handling | Limited | Excellent |
| Ease of use | Medium | Easy |
| Best for | Math operations | Real-world time series |
Summary
NumPy helps in:
- Creating time series data
- Analyzing trends
- Performing mathematical operations
- Cleaning and transforming data
It is the foundation of time-based data processing in NumPy and works closely with Python for data science applications.
Conclusion
Time series analysis is essential for modern data-driven applications. With NumPy, you can efficiently process, transform, and analyze time-based data with simple and powerful operations.
If you're working in data science, AI, or analytics using Python, mastering NumPy time series tools is a must.


0 Comments