<jupyter_start><jupyter_text>Python - Time Series Data with Pandas<jupyter_code>from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pytz

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Generate Time Series Data<jupyter_code># Generate data with time
ts = pd.Series(np.random.rand(100), index=pd.date_range(datetime.now(), periods=100))
ts
# Generate data without time (normalize = True)
ts = pd.Series(np.random.rand(100), index=pd.date_range(datetime.now(), periods=100, normalize=True))
ts
# You can generate data using different frequency offset alias
# https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases

# BM - business month end
pd.date_range(start=datetime.now(), end=datetime.now()+timedelta(days=365), freq='BM')
# Every 90 minutes
pd.date_range(start=datetime.now(), end=datetime.now()+timedelta(days=3), freq='1H30T')<jupyter_output><empty_output><jupyter_text>Handling Time Zone<jupyter_code># View common time zones
# pytz.common_timezones
# Check time series time zone zone
print(ts.index.tz)
ts_utc = ts.tz_localize('UTC')
print(ts_utc.index.tz)
ts_sgt = ts.tz_localize('Asia/Singapore')
print(ts_sgt.index.tz)
ts_london = ts.tz_localize('Europe/London')
print(ts_london.index.tz)
# Convert between timezone
utc_time = pd.Timestamp('2020-12-01 06:00', tz='utc')
print(f"UTC time {utc_time}")

shanghai_time = utc_time.tz_convert('Asia/Shanghai')
print(f"Shanghai time: {shanghai_time}")<jupyter_output>UTC time 2020-12-01 06:00:00+00:00
Shanghai time: 2020-12-01 14:00:00+08:00<jupyter_text>Resampling<jupyter_code>dt_ranges = pd.date_range(datetime.now(), periods=365, freq='D', normalize=True)
ts = pd.Series(np.random.randn(len(dt_ranges)), index=dt_ranges)
ts
# Roll up to month
ts.resample('M').sum()
# Roll up to quarter
ts.resample('Q').sum()<jupyter_output><empty_output><jupyter_text>Downsampling<jupyter_code>dt_ranges = pd.date_range(datetime.now(), periods=100, freq='T', normalize=True)
ts = pd.Series(np.random.randn(len(dt_ranges)), index=dt_ranges)
ts
ts.resample('5min', closed='right', label='right').sum()
# OHLC resampling
ts.resample('5min').ohlc()<jupyter_output><empty_output><jupyter_text>Upsampling<jupyter_code>dt_ranges = pd.date_range(datetime.now(), periods=12, freq='M', normalize=True)
ts = pd.Series(np.random.randn(len(dt_ranges)), index=dt_ranges)
ts
ts.resample('D').ffill()<jupyter_output><empty_output><jupyter_text>Moving Average<jupyter_code>dt_ranges = pd.date_range(datetime.now(), periods=30, freq='D', normalize=True)
ts = pd.Series(np.random.randint(150,200, len(dt_ranges)), index=dt_ranges)
ts
ts.plot()
ts.rolling(5).mean().plot()
ts.ewm(5).mean().plot();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.155877034358047, 'fraction_numerical': 0.03218806509945751, 'mean_word_length': 8.408163265306122, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '514792', 'license': 'MIT', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1060, 'n_words': 172, 'path': 'nbs/01_pandas_time_series.ipynb', 'repo_name': 'deedree/alpha2phi-jupyter-notebooks', 'size': '2765'}