<jupyter_start><jupyter_text>Resampling Oil Price Timeseries Data This week we are going to read in oil price data that was sampled at a daily interval. Next we will take this timeseries and resample it to bi-weekly and half-yearly intervals. We could also downsample or upsample to any interval we are interested in<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Read in the excel workbook and read in the second sheet<jupyter_code>oil_price = pd.read_excel(
    "eia_prices.xls",
    sheet_name="Data 1",
    header=2,
    index_col=[0],
    parse_dates=True,
).dropna()  # read EIA workbook, open sheet named Data 1 and index with dates
oil_price.head()  # quick look to make sure the data is ok, we also dropped the NaN values<jupyter_output><empty_output><jupyter_text>Now let's make a new `DataFrame` with an easier to call name for the WTI oil price<jupyter_code>WTI = pd.DataFrame()
WTI['price'] = oil_price['Cushing, OK WTI Spot Price FOB (Dollars per Barrel)']<jupyter_output><empty_output><jupyter_text>To resample the WTI oil price we call the WTI `DataFrame` with the `rolling` method, and specify the interval (here `14d` is 14 days) and then we need to specify how to aggregate the values. Here we use the `mean`, `min`, and `max`.<jupyter_code>biweekly_mean = WTI.rolling('14d').mean()
biweekly_min = WTI.rolling('14d').min()
biweekly_max = WTI.rolling('14d').max()<jupyter_output><empty_output><jupyter_text>To sample at the half-year scale, we call `6M` for six months. Let's call the median price for the 6 month period with `median`<jupyter_code>half_year = WTI.resample('6M').median()<jupyter_output><empty_output><jupyter_text>Let's do a quick visual, to see how the half-yearly resample compares to the bi-weekly mean, min, and max<jupyter_code>plt.plot(biweekly_mean, label='Bi-weekly mean')
plt.plot(biweekly_min, label='Bi-weekly min')
plt.plot(biweekly_max, label='Bi-weekly max')


plt.plot(half_year, label='Half-yearly')
plt.legend()
plt.xlim('2010-01-01','2018-07-01')<jupyter_output><empty_output><jupyter_text>And a quick sanity check to see the structure of the data and we are good to go! We can now combine this with other datasets that have similar sample rates<jupyter_code>half_year.tail()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0963265306122449, 'fraction_numerical': 0.013469387755102041, 'mean_word_length': 6.083815028901734, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '531986', 'license': 'Unlicense', 'n_tokens_mistral': 858, 'n_tokens_neox': 816, 'n_words': 272, 'path': 'Oil Prices/Price Timeseries Resample.ipynb', 'repo_name': 'jessepisel/5minutesofpython', 'size': '2450'}