irongit
time-series-cookbook/ch1/1.03_resampling_a_time_series.py
20 lines545 BPython
1import pandas as pd
2import matplotlib.pyplot as plt
3import numpy as np
4
5
6# Create fake sales data
7n_sales = 1000
8start = pd.Timestamp('2023-01-01 09:00')
9end = pd.Timestamp('2023-04-01')
10n_days = (end-start).days + 1 # pyright: ignore[reportOperatorIssue]
11
12# random times from start
13irregular_series = pd.to_timedelta(np.random.rand(n_sales) * n_days, unit='D')+start
14
15# fake sales all = 0, resampled to daily
16ts_sales = pd.Series(0, index=irregular_series)
17tot_sales = ts_sales.resample('D').count() # daily sales
18
19tot_sales.plot()
20plt.show()