Use fixed seeds for reproducible pseudorandomness
authorDebian Science Team <debian-science-maintainers@lists.alioth.debian.org>
Sun, 18 Feb 2024 20:31:18 +0000 (20:31 +0000)
committerRebecca N. Palmer <rebecca_palmer@zoho.com>
Sun, 18 Feb 2024 20:31:18 +0000 (20:31 +0000)
Author: Rebecca N. Palmer <rebecca_palmer@zoho.com>
Forwarded: no

Gbp-Pq: Name fix_random_seeds.patch

doc/source/getting_started/comparison/comparison_with_r.rst
doc/source/user_guide/advanced.rst
doc/source/user_guide/style.ipynb
doc/source/user_guide/visualization.rst
pandas/plotting/_core.py
pandas/plotting/_misc.py

index a6cfcd46149846d34897dfe11d0d40c3db823d49..836e3669c6881b9b8d6b155103e77804ff8a5347 100644 (file)
@@ -237,6 +237,7 @@ In pandas we may use :meth:`~pandas.pivot_table` method to handle this:
 
    import random
    import string
+   random.seed(123456) # for reproducibility
 
    baseball = pd.DataFrame(
        {
index 682fa4c9b4fcce120a17cc735772e7c2ea231b97..e92dbdb26e407d31c36c43c5dfc34088eb9e3317 100644 (file)
@@ -590,6 +590,7 @@ they need to be sorted. As with any index, you can use :meth:`~DataFrame.sort_in
 
    import random
 
+   random.seed(123456) # for reproducibility
    random.shuffle(tuples)
    s = pd.Series(np.random.randn(8), index=pd.MultiIndex.from_tuples(tuples))
    s
index f22a506499cf4647c716b6a416ee61ca198d7c80..46c671c5331f17b48bdf563d3459283cb90226b9 100644 (file)
    "source": [
     "import pandas as pd\n",
     "import numpy as np\n",
-    "import matplotlib as mpl\n",
-    "\n",
+    "import matplotlib as mpl\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "nbsphinx": "hidden"
+   },
+   "outputs": [],
+   "source": [
+    "# For reproducibility - this doesn't respect uuid_len or positionally-passed uuid but the places here that use that coincidentally bypass this anyway\n",
+    "from pandas.io.formats.style import Styler\n",
+    "next_uuid = 1000\n",
+    "class StylerReproducible(Styler):\n",
+    "    def __init__(self, *args, uuid=None, **kwargs):\n",
+    "        global next_uuid\n",
+    "        if uuid is None:\n",
+    "            uuid = str(next_uuid)\n",
+    "            next_uuid = next_uuid + 1\n",
+    "        super().__init__(*args, uuid=uuid, **kwargs)\n",
+    "Styler = StylerReproducible\n",
+    "pd.DataFrame.style = property(lambda self: StylerReproducible(self))\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
     "df = pd.DataFrame({\n",
     "    \"strings\": [\"Adam\", \"Mike\"],\n",
     "    \"ints\": [1, 3],\n",
    "metadata": {},
    "outputs": [],
    "source": [
+    "np.random.seed(25)  # for reproducibility\n",
     "weather_df = pd.DataFrame(np.random.rand(10,2)*5, \n",
     "                          index=pd.date_range(start=\"2021-01-01\", periods=10),\n",
     "                          columns=[\"Tokyo\", \"Beijing\"])\n",
    "outputs": [],
    "source": [
     "# Hide the construction of the display chart from the user\n",
-    "import pandas as pd\n",
     "from IPython.display import HTML\n",
     "\n",
     "# Test series\n",
     "from pandas.io.formats.style import Styler"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "nbsphinx": "hidden"
+   },
+   "outputs": [],
+   "source": [
+    "# For reproducibility\n",
+    "Styler = StylerReproducible\n"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
    "version": "3.9.5"
-  }
+  },
+  "record_timing": false
  },
  "nbformat": 4,
  "nbformat_minor": 1
index 9081d13ef2cf13c0b09b4097090b7653f813c7b1..b786b181dab31a23eec20911bd36770b3481c503 100644 (file)
@@ -1086,6 +1086,7 @@ are what constitutes the bootstrap plot.
    :suppress:
 
    np.random.seed(123456)
+   random.seed(123456) # for reproducibility - bootstrap_plot uses random.sample
 
 .. ipython:: python
 
index 07c77ec4f3e0a183711d53fa5d787699605dfbda..8336a246ae4c550ee91aa00c467396debb580c39 100644 (file)
@@ -600,6 +600,7 @@ def boxplot_frame_groupby(
     .. plot::
         :context: close-figs
 
+        >>> np.random.seed(1234)
         >>> import itertools
         >>> tuples = [t for t in itertools.product(range(1000), range(4))]
         >>> index = pd.MultiIndex.from_tuples(tuples, names=['lvl0', 'lvl1'])
@@ -1329,6 +1330,7 @@ class PlotAccessor(PandasObject):
         .. plot::
             :context: close-figs
 
+            >>> np.random.seed(1234)
             >>> data = np.random.randn(25, 4)
             >>> df = pd.DataFrame(data, columns=list('ABCD'))
             >>> ax = df.plot.box()
@@ -1393,6 +1395,7 @@ class PlotAccessor(PandasObject):
         .. plot::
             :context: close-figs
 
+            >>> np.random.seed(1234)
             >>> df = pd.DataFrame(
             ...     np.random.randint(1, 7, 6000),
             ...     columns = ['one'])
@@ -1814,6 +1817,7 @@ class PlotAccessor(PandasObject):
         .. plot::
             :context: close-figs
 
+            >>> np.random.seed(1234)
             >>> n = 10000
             >>> df = pd.DataFrame({'x': np.random.randn(n),
             ...                    'y': np.random.randn(n)})
index 625780ac9fc670b9cbc215ec5ffda61a05196eba..b7ec4ca520c4c138327ff7c9af0d32b47286032e 100644 (file)
@@ -438,6 +438,8 @@ def bootstrap_plot(
     .. plot::
         :context: close-figs
 
+        >>> np.random.seed(1234)
+        >>> random.seed(1234)  # for reproducibility
         >>> s = pd.Series(np.random.uniform(size=100))
         >>> pd.plotting.bootstrap_plot(s)
         <Figure size 640x480 with 6 Axes>
@@ -597,6 +599,7 @@ def autocorrelation_plot(series: Series, ax: Axes | None = None, **kwargs) -> Ax
     .. plot::
         :context: close-figs
 
+        >>> np.random.seed(1234)
         >>> spacing = np.linspace(-9 * np.pi, 9 * np.pi, num=1000)
         >>> s = pd.Series(0.7 * np.random.rand(1000) + 0.3 * np.sin(spacing))
         >>> pd.plotting.autocorrelation_plot(s)  # doctest: +SKIP