{"cells":[{"cell_type":"markdown","source":"The oil dataframe contains important information about the economic health of Ecuador during the training period. One of the preprocessing challenges is to take this data, fill in holes and extract a useful feature from it. \n\nIn this notebook, we construct a \"complete\" timseries of the data and show how to smooth it using a rolling mean.","metadata":{}},{"cell_type":"code","execution_count":null,"source":"import pandas as pd\nimport numpy as np\n\ndef create_oil_df(window_size=7, input_path='.'):\n    '''Reads in oil df, fills in mising timepoints and applies rolling mean.'''\n    \n    df_oil = pd.read_csv('{}/oil.csv'.format(input_path), \n                         parse_dates=['date'], \n                         dtype={'dcoilwtico':np.float16})\n    \n    # Resample oil timeseries to daily resolution\n    oil_ts = df_oil.set_index('date').dcoilwtico.resample('1D').mean()\n    oil_ts = oil_ts.interpolate(method='spline', order=3)\n    \n    # Apply a rolling mean of size `window_size`\n    df_oil = (oil_ts.rolling(window=window_size)\n                  .mean()\n                  .fillna(oil_ts).fillna(93)\n                  .reset_index() )\n    \n    # Future: consider binarizing prices, smoothing more, taking differential (i.e. are prices going up / down?)    \n    return df_oil.set_index('date')","outputs":[],"metadata":{"collapsed":true}},{"cell_type":"code","execution_count":null,"source":"df_oil = create_oil_df(window_size=7, input_path='../input')","outputs":[],"metadata":{"collapsed":true}},{"cell_type":"code","execution_count":null,"source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\nfig, ax = plt.subplots(1,1, figsize=(8,4), dpi=150)\n\ndf_oil.plot(ax=ax)\n\n# Plot 7-day change in oil price, smoothed by two months\nbaseline = 70\ndx = 10 * df_oil.diff(periods=7) + baseline\ndx.rolling(window=60).median().plot(ax=ax)\n\n# Add graph features\nax.hlines(baseline, dx.index.min(), dx.index.max(), \n          linestyle='--', color='xkcd:light gray')\nhandles, labels = ax.get_legend_handles_labels()\nax.legend(handles, ['Actual Price', 'Smoothed 7-Day Change'])\nax.set_title('The actual price of oil during the training period\\nWith the daily changes in price.')\n\nplt.show()","outputs":[],"metadata":{}},{"cell_type":"code","execution_count":null,"source":"","outputs":[],"metadata":{"collapsed":true}}],"nbformat_minor":1,"nbformat":4,"metadata":{"language_info":{"name":"python","version":"3.6.3","codemirror_mode":{"name":"ipython","version":3},"mimetype":"text/x-python","nbconvert_exporter":"python","pygments_lexer":"ipython3","file_extension":".py"},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"}}}