From a7b6d1720e2cf594366234ee6df60f7577e4708a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 1 Aug 2023 12:10:17 +0200 Subject: [PATCH 1/3] Example register_matplotlib_converters --- ci/code_checks.sh | 1 - pandas/plotting/_misc.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 7678e1e975d1a..e4df7c0d164a4 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -66,7 +66,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.NaT \ pandas.io.stata.StataWriter.write_file \ pandas.plotting.deregister_matplotlib_converters \ - pandas.plotting.register_matplotlib_converters \ pandas.api.extensions.ExtensionArray \ RET=$(($RET + $?)) ; echo $MSG "DONE" diff --git a/pandas/plotting/_misc.py b/pandas/plotting/_misc.py index f862e49071cae..33409b696603d 100644 --- a/pandas/plotting/_misc.py +++ b/pandas/plotting/_misc.py @@ -81,6 +81,24 @@ def register() -> None: See Also -------- deregister_matplotlib_converters : Remove pandas formatters and converters. + + Examples + -------- + .. plot:: + :context: close-figs + + >>> pd.plotting.register_matplotlib_converters() + >>> df = pd.DataFrame({'ts': pd.period_range('2020', periods=2, freq='M'), + ... 'y': [1, 2] + ... }) + >>> plot = df.plot.line(x='ts', y='y') + + With the following, an error will be raised: + + >>> pd.set_option("plotting.matplotlib.register_converters", + ... False + ... ) # doctest: +SKIP + >>> df.plot.line(x='ts', y='y') # doctest: +SKIP """ plot_backend = _get_plot_backend("matplotlib") plot_backend.register() From ce19c70ece1ef4d0c0cd269bc753b12376cd848d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 1 Aug 2023 13:34:07 +0200 Subject: [PATCH 2/3] Changed/added explanation to example --- pandas/plotting/_misc.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_misc.py b/pandas/plotting/_misc.py index 33409b696603d..7b0bc39b88fe9 100644 --- a/pandas/plotting/_misc.py +++ b/pandas/plotting/_misc.py @@ -87,18 +87,24 @@ def register() -> None: .. plot:: :context: close-figs + The following line is done automatically by pandas so + the plot can be rendered: + >>> pd.plotting.register_matplotlib_converters() + >>> df = pd.DataFrame({'ts': pd.period_range('2020', periods=2, freq='M'), ... 'y': [1, 2] ... }) >>> plot = df.plot.line(x='ts', y='y') - With the following, an error will be raised: + Unsetting the register manually an error will be raised: >>> pd.set_option("plotting.matplotlib.register_converters", ... False ... ) # doctest: +SKIP >>> df.plot.line(x='ts', y='y') # doctest: +SKIP + Traceback (most recent call last): + TypeError: float() argument must be a string or a real number, not 'Period' """ plot_backend = _get_plot_backend("matplotlib") plot_backend.register() From af9f78cad213c6fdc011c04afed15bcdb5d28940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 1 Aug 2023 16:45:28 +0200 Subject: [PATCH 3/3] Fixing plot --- pandas/plotting/_misc.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pandas/plotting/_misc.py b/pandas/plotting/_misc.py index 7b0bc39b88fe9..1f46fdacce4e9 100644 --- a/pandas/plotting/_misc.py +++ b/pandas/plotting/_misc.py @@ -97,14 +97,13 @@ def register() -> None: ... }) >>> plot = df.plot.line(x='ts', y='y') - Unsetting the register manually an error will be raised: - - >>> pd.set_option("plotting.matplotlib.register_converters", - ... False - ... ) # doctest: +SKIP - >>> df.plot.line(x='ts', y='y') # doctest: +SKIP - Traceback (most recent call last): - TypeError: float() argument must be a string or a real number, not 'Period' + Unsetting the register manually an error will be raised: + + >>> pd.set_option("plotting.matplotlib.register_converters", + ... False) # doctest: +SKIP + >>> df.plot.line(x='ts', y='y') # doctest: +SKIP + Traceback (most recent call last): + TypeError: float() argument must be a string or a real number, not 'Period' """ plot_backend = _get_plot_backend("matplotlib") plot_backend.register()