<jupyter_start><jupyter_text>Read the Data<jupyter_code>def parser(s):
    return datetime.strptime(s, '%Y-%m')
ice_cream_heater_df = pd.read_csv('ice_cream_vs_heater.csv', parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
ice_cream_heater_df = ice_cream_heater_df.asfreq(pd.infer_freq(ice_cream_heater_df.index))
heater_series = ice_cream_heater_df.heater
heater_series
def plot_series(series):
    plt.figure(figsize=(12,6))
    plt.plot(heater_series, color='red')
    plt.ylabel('Search Frequency for "Heater"', fontsize=16)

    for year in range(2004, 2021):
        plt.axvline(datetime(year,1,1), linestyle='--', color='k', alpha=0.5)
plot_series(heater_series)<jupyter_output><empty_output><jupyter_text>Normalize<jupyter_code>avg, dev = heater_series.mean(), heater_series.std()
heater_series = (heater_series - avg) / dev
plot_series(heater_series)
plt.axhline(0, linestyle='--', color='k', alpha=0.3)<jupyter_output><empty_output><jupyter_text>Take First Difference to Remove Trend<jupyter_code>heater_series = heater_series.diff().dropna()
plot_series(heater_series)
plt.axhline(0, linestyle='--', color='k', alpha=0.3)<jupyter_output><empty_output><jupyter_text>Remove Increasing Volatility<jupyter_code>annual_volatility = heater_series.groupby(heater_series.index.year).std()
annual_volatility
heater_annual_vol = heater_series.index.map(lambda d: annual_volatility.loc[d.year])
heater_annual_vol
heater_series = heater_series / heater_annual_vol
plot_series(heater_series)
plt.axhline(0, linestyle='--', color='k', alpha=0.3)<jupyter_output><empty_output><jupyter_text>Remove Seasonality<jupyter_code>month_avgs = heater_series.groupby(heater_series.index.month).mean()
month_avgs
heater_month_avg = heater_series.index.map(lambda d: month_avgs.loc[d.month])
heater_month_avg
heater_series = heater_series - heater_month_avg
plot_series(heater_series)
plt.axhline(0, linestyle='--', color='k', alpha=0.3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13638676844783715, 'fraction_numerical': 0.015776081424936386, 'mean_word_length': 9.1340206185567, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '215226', 'license': 'MIT', 'n_tokens_mistral': 786, 'n_tokens_neox': 747, 'n_words': 94, 'path': 'time-series-analysis-anomalies/Time Series Data Preprocessing.ipynb', 'repo_name': 'suhasdevmane/AnomalyDetection', 'size': '1965'}