irongit
time-series-cookbook/ch1/1.04_dealing_with_missing_values.py
52 lines1.4 KBPython
1import os
2import pandas as pd
3import numpy as np
4import matplotlib.pyplot as plt
5import seaborn as sns
6
7# Load solar data
8data = pd.read_csv(
9 "../assets/datasets/time_series_solar.csv",
10 parse_dates=["Datetime"],
11 index_col="Datetime",
12)
13series = data["Incoming Solar"]
14
15# Resample to daily
16series_daily = series.resample("D").sum()
17
18# Create random nan values in first 2 years, remove 60% of values
19sample_with_nan = series_daily.head(365 * 2).copy()
20size_na = int(0.6 * len(sample_with_nan))
21
22idx = np.random.choice(a=range(len(sample_with_nan)), size=size_na, replace=False)
23
24sample_with_nan[idx] = np.nan
25
26# Gather imputations
27average_value = sample_with_nan.mean()
28imp_mean = sample_with_nan.fillna(average_value) # Fill na with average
29imp_ffill = sample_with_nan.ffill() # Fill na with previous value
30imp_bfill = sample_with_nan.bfill() # Fill na with next value
31
32# Plot all on one chart
33plt.rcParams['figure.figsize'] = [12, 6]
34
35sns.set_theme(style='darkgrid')
36
37fig, (ax0, ax1, ax2, ax3) = plt.subplots(4, sharex=True)
38fig.suptitle('Time series imputation methods')
39
40ax0.plot(sample_with_nan)
41ax0.set_title('Original series with missing data')
42ax1.plot(imp_mean)
43ax1.set_title('Series with mean imputation')
44ax2.plot(imp_ffill)
45ax2.set_title('Series with ffill imputation (Most common)')
46ax3.plot(imp_bfill)
47ax3.set_title('Series with bfill imputation')
48
49plt.tight_layout()
50
51os.makedirs("assets", exist_ok=True)
52plt.savefig("assets/missing_data_plot.png")