Skip to content

Commit

Permalink
take shallow copy on dataframe processing and fix get_cmap deprecation (
Browse files Browse the repository at this point in the history
#617)

* take shallow copy on dataframe processing and fix get_cmap deprecation

* check mpl version
  • Loading branch information
jmoralez authored Aug 23, 2023
1 parent 6e071ec commit 2cc53b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
19 changes: 14 additions & 5 deletions nbs/src/core/core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@
"import pkg_resources\n",
"\n",
"from fugue.execution.factory import make_execution_engine\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.colors as cm \n",
"import matplotlib.colors as cm\n",
"import numpy as np\n",
"import pandas as pd\n",
"import polars as pl\n",
Expand Down Expand Up @@ -936,6 +937,7 @@
"\n",
" # Datetime check\n",
" dt_arr = self.dataframe['ds'].values\n",
" self.dataframe = self.dataframe.copy(deep=False)\n",
" self.dataframe['ds'] = self._check_datetime(dt_arr)\n",
"\n",
" self.dataframe = self.dataframe.set_index('ds', append=True)\n",
Expand Down Expand Up @@ -1956,7 +1958,10 @@
" df_uid = df_uid.iloc[-max_insample_length:]\n",
" plot_anomalies = 'y' in df_uid and plot_anomalies\n",
" df_uid = _parse_ds_type(df_uid)\n",
" colors = plt.cm.get_cmap('tab20b', len(models))\n",
" if pkg_resources.parse_version(mpl.__version__) < pkg_resources.parse_version('3.6'):\n",
" colors = plt.cm.get_cmap('tab20b', len(models))\n",
" else:\n",
" colors = mpl.colormaps['tab20b'].resampled(len(models)) \n",
" colors = ['#1f77b4'] + [cm.to_hex(colors(i)) for i in range(len(models))]\n",
" for col, color in zip(models, colors):\n",
" if col in df_uid:\n",
Expand Down Expand Up @@ -2071,7 +2076,11 @@
" color='black', \n",
" label='First ds Forecast', \n",
" linestyle='--')\n",
" colors = plt.cm.get_cmap('tab20b', len(models))\n",
"\n",
" if pkg_resources.parse_version(mpl.__version__) < pkg_resources.parse_version('3.6'):\n",
" colors = plt.cm.get_cmap('tab20b', len(models))\n",
" else:\n",
" colors = mpl.colormaps['tab20b'].resampled(len(models))\n",
" colors = ['blue'] + [colors(i) for i in range(len(models))]\n",
" for col, color in zip(models, colors):\n",
" if col in test_uid:\n",
Expand Down Expand Up @@ -2390,7 +2399,7 @@
"source": [
"#| hide\n",
"fig = StatsForecast.plot(panel_df, max_insample_length=10)\n",
"fig.show()"
"fig"
]
},
{
Expand Down Expand Up @@ -2568,7 +2577,7 @@
"source": [
"#| hide\n",
"fig = fcst.plot(panel_df, fcsts_df, unique_ids=[0, 1, 2, 3, 4], models=['AutoARIMA', 'Naive'], level=[90])\n",
"fig.show()"
"fig"
]
},
{
Expand Down
17 changes: 15 additions & 2 deletions statsforecast/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import pkg_resources

from fugue.execution.factory import make_execution_engine
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors as cm
import numpy as np
Expand Down Expand Up @@ -615,6 +616,7 @@ def _to_np_and_engine(self):

# Datetime check
dt_arr = self.dataframe["ds"].values
self.dataframe = self.dataframe.copy(deep=False)
self.dataframe["ds"] = self._check_datetime(dt_arr)

self.dataframe = self.dataframe.set_index("ds", append=True)
Expand Down Expand Up @@ -1595,7 +1597,12 @@ def plotly(
df_uid = df_uid.iloc[-max_insample_length:]
plot_anomalies = "y" in df_uid and plot_anomalies
df_uid = _parse_ds_type(df_uid)
colors = plt.cm.get_cmap("tab20b", len(models))
if pkg_resources.parse_version(
mpl.__version__
) < pkg_resources.parse_version("3.6"):
colors = plt.cm.get_cmap("tab20b", len(models))
else:
colors = mpl.colormaps["tab20b"].resampled(len(models))
colors = ["#1f77b4"] + [
cm.to_hex(colors(i)) for i in range(len(models))
]
Expand Down Expand Up @@ -1751,7 +1758,13 @@ def plotly(
label="First ds Forecast",
linestyle="--",
)
colors = plt.cm.get_cmap("tab20b", len(models))

if pkg_resources.parse_version(
mpl.__version__
) < pkg_resources.parse_version("3.6"):
colors = plt.cm.get_cmap("tab20b", len(models))
else:
colors = mpl.colormaps["tab20b"].resampled(len(models))
colors = ["blue"] + [colors(i) for i in range(len(models))]
for col, color in zip(models, colors):
if col in test_uid:
Expand Down

0 comments on commit 2cc53b6

Please sign in to comment.