Timeseries
In [4]:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
dates = pd.date_range('2023-01-01', periods=100)
data = np.random.randn(100).cumsum()
df = pd.DataFrame({'Date': dates, 'Value': data})
plt.figure(figsize=(10, 6))
plt.plot(df['Date'], df['Value'], label='Time Series Data')
plt.title('Time Series Chart')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
In [5]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dates = pd.date_range(start='2022-01-01', end='2023-12-31', freq='B') # Business days
np.random.seed(0)
prices = np.random.normal(loc=100, scale=10, size=len(dates))
df = pd.DataFrame({'price': prices}, index=dates)
start_date = '2022-06-01'
end_date = '2022-10-05'
df.loc[start_date:end_date, 'price'] = np.nan
df_original = df
df_ffill = df_original.copy()
df_ffill = df_ffill.ffill()
df_bfill = df_original.copy()
df_bfill = df_bfill.bfill()
df_interp = df_original.copy()
df_interp = df_interp.interpolate(method='spline', order=1)
df_drop = df_original.copy()
df_drop = df_drop.fillna(20)
In [6]:
fig, axes = plt.subplots(nrows=5, ncols=1, figsize=(14, 16), sharex=True)
axes[0].plot(df_original.index, df_original['price'], label='Original', color='blue', linestyle='--')
axes[0].set_title('Original Data')
axes[0].legend()
axes[1].plot(df_ffill.index, df_ffill['price'], label='Forward Fill', color='green')
axes[1].set_title('Forward Fill')
axes[1].legend()
axes[2].plot(df_bfill.index, df_bfill['price'], label='Backward Fill', color='red')
axes[2].set_title('Backward Fill')
axes[2].legend()
axes[3].plot(df_interp.index, df_interp['price'], label='Interpolation', color='purple')
axes[3].set_title('Interpolation')
axes[3].legend()
axes[4].plot(df_drop.index, df_drop['price'], label='fillna', color='purple')
axes[4].set_title('fillna')
axes[4].legend()
plt.xlabel('Date')
plt.ylabel('Price')
plt.tight_layout()
plt.show()
