Skip to content

Commit aa5c9c2

Browse files
authored
fix pandas errors in notebooks (#1490)
1 parent bb8ffea commit aa5c9c2

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

introduction_to_amazon_algorithms/deepar_electricity/DeepAR-Electricity.ipynb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@
548548
" \n",
549549
" Return value: list of `pandas.DataFrame` objects, each containing the predictions\n",
550550
" \"\"\"\n",
551-
" prediction_time = ts.index[-1] + 1\n",
551+
" prediction_time = ts.index[-1] + ts.index.freq\n",
552552
" quantiles = [str(q) for q in quantiles]\n",
553553
" req = self.__encode_request(ts, cat, dynamic_feat, num_samples, return_samples, quantiles)\n",
554554
" res = super(DeepARPredictor, self).predict(req)\n",
@@ -575,7 +575,7 @@
575575
" # however, if possible one will pass multiple time series as predictions will then be faster\n",
576576
" predictions = json.loads(response.decode('utf-8'))['predictions'][0]\n",
577577
" prediction_length = len(next(iter(predictions['quantiles'].values())))\n",
578-
" prediction_index = pd.DatetimeIndex(start=prediction_time, freq=freq, periods=prediction_length) \n",
578+
" prediction_index = pd.date_range(start=prediction_time, freq=freq, periods=prediction_length)\n",
579579
" if return_samples:\n",
580580
" dict_of_samples = {'sample_' + str(i): s for i, s in enumerate(predictions['samples'])}\n",
581581
" else:\n",
@@ -669,6 +669,7 @@
669669
" plot_history=7 * 12,\n",
670670
" confidence=80\n",
671671
"):\n",
672+
" freq = target_ts.index.freq\n",
672673
" print(\"calling served model to generate predictions starting from {}\".format(str(forecast_date)))\n",
673674
" assert(confidence > 50 and confidence < 100)\n",
674675
" low_quantile = 0.5 - confidence * 0.005\n",
@@ -706,7 +707,7 @@
706707
" \n",
707708
" \n",
708709
" # plot the target\n",
709-
" target_section = target_ts[forecast_date-plot_history:forecast_date+prediction_length]\n",
710+
" target_section = target_ts[forecast_date - plot_history * freq:forecast_date + prediction_length * freq]\n",
710711
" target_section.plot(color=\"black\", label='target')\n",
711712
" \n",
712713
" # plot the confidence interval and the median predicted\n",
@@ -726,10 +727,10 @@
726727
" for i, f in enumerate(dynamic_feat, start=1):\n",
727728
" ax = plt.subplot(len(dynamic_feat) * 2, 1, len(dynamic_feat) + i, sharex=ax)\n",
728729
" feat_ts = pd.Series(\n",
729-
" index=pd.DatetimeIndex(start=target_ts.index[0], freq=target_ts.index.freq, periods=len(f)),\n",
730+
" index=pd.date_range(start=target_ts.index[0], freq=target_ts.index.freq, periods=len(f)),\n",
730731
" data=f\n",
731732
" )\n",
732-
" feat_ts[forecast_date-plot_history:forecast_date+prediction_length].plot(ax=ax, color='g')"
733+
" feat_ts[forecast_date - plot_history * freq:forecast_date + prediction_length * freq].plot(ax=ax, color='g')"
733734
]
734735
},
735736
{
@@ -908,8 +909,8 @@
908909
"test_data_new_features = [\n",
909910
" {\n",
910911
" \"start\": str(start_dataset),\n",
911-
" \"target\": encode_target(ts[start_dataset:end_training + 2*k*prediction_length]),\n",
912-
" \"dynamic_feat\": [special_day_features[i][start_dataset:end_training + 2*k*prediction_length].tolist()]\n",
912+
" \"target\": encode_target(ts[start_dataset:end_training + 2*k*prediction_length * ts.index.freq]),\n",
913+
" \"dynamic_feat\": [special_day_features[i][start_dataset:end_training + 2*k*prediction_length * ts.index.freq].tolist()]\n",
913914
" }\n",
914915
" for k in range(1, num_test_windows + 1) \n",
915916
" for i, ts in enumerate(timeseries_uplift)\n",
@@ -1070,9 +1071,11 @@
10701071
")\n",
10711072
"def plot_interact(customer_id, forecast_day, confidence, missing_ratio, show_samples): \n",
10721073
" forecast_date = end_training + datetime.timedelta(days=forecast_day)\n",
1073-
" target = time_series_processed[customer_id][start_dataset:forecast_date + prediction_length]\n",
1074+
" ts = time_series_processed[customer_id]\n",
1075+
" freq = ts.index.freq\n",
1076+
" target = ts[start_dataset:forecast_date + prediction_length * freq]\n",
10741077
" target = drop_at_random(target, missing_ratio)\n",
1075-
" dynamic_feat = [special_day_features[customer_id][start_dataset:forecast_date + prediction_length].tolist()]\n",
1078+
" dynamic_feat = [special_day_features[customer_id][start_dataset:forecast_date + prediction_length * freq].tolist()]\n",
10761079
" plot(\n",
10771080
" predictor_new_features,\n",
10781081
" target_ts=target, \n",

introduction_to_applying_machine_learning/deepar_chicago_traffic_violations/deepar_chicago_traffic_violations.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@
357357
" \n",
358358
" Return value: list of `pandas.DataFrame` objects, each containing the predictions\n",
359359
" \"\"\"\n",
360-
" prediction_times = [x.index[-1]+1 for x in ts]\n",
360+
" prediction_times = [x.index[-1] + x.index.freq for x in ts]\n",
361361
" req = self.__encode_request(ts, cat, encoding, num_samples, quantiles)\n",
362362
" res = super(DeepARPredictor, self).predict(req)\n",
363363
" return self.__decode_response(res, prediction_times, encoding)\n",
@@ -372,7 +372,7 @@
372372
" response_data = json.loads(response.decode(encoding))\n",
373373
" list_of_df = []\n",
374374
" for k in range(len(prediction_times)):\n",
375-
" prediction_index = pd.DatetimeIndex(start=prediction_times[k], freq=self.freq, periods=self.prediction_length)\n",
375+
" prediction_index = pd.date_range(start=prediction_times[k], freq=self.freq, periods=self.prediction_length)\n",
376376
" list_of_df.append(pd.DataFrame(data=response_data['predictions'][k]['quantiles'], index=prediction_index))\n",
377377
" return list_of_df\n",
378378
"\n",

0 commit comments

Comments
 (0)