diff --git a/doc/changelog.qmd b/doc/changelog.qmd index 3af3941e69..e05a82e16f 100644 --- a/doc/changelog.qmd +++ b/doc/changelog.qmd @@ -25,6 +25,14 @@ title: Changelog - Fixed rendering of SVGs in jupyter notebooks. +## v0.15.3 +(2025-01-28) + +### Enhancements + +- Removed warnings about copy-on-write for pandas >= 3.0.0 + + ## v0.15.2 (2025-12-12) diff --git a/plotnine/_utils/context.py b/plotnine/_utils/context.py index 6deda3bf1e..872106934c 100644 --- a/plotnine/_utils/context.py +++ b/plotnine/_utils/context.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING import pandas as pd +from packaging.version import Version if TYPE_CHECKING: from typing_extensions import Self @@ -11,6 +12,8 @@ from plotnine import ggplot from plotnine.composition import Compose +PANDAS_LT_3 = Version(pd.__version__) < Version("3.0") + def reopen(fig): """ @@ -55,12 +58,11 @@ def __init__(self, plot: ggplot, show: bool = False): # Contexts self.rc_context = mpl.rc_context(plot.theme.rcParams) - # TODO: Remove this context when copy-on-write is permanent, i.e. - # pandas >= 3.0 - self.pd_option_context = pd.option_context( - "mode.copy_on_write", - True, - ) + if PANDAS_LT_3: + self.pd_option_context = pd.option_context( + "mode.copy_on_write", + True, + ) def __enter__(self) -> Self: """ @@ -68,7 +70,8 @@ def __enter__(self) -> Self: """ self.rc_context.__enter__() - self.pd_option_context.__enter__() + if PANDAS_LT_3: + self.pd_option_context.__enter__() return self @@ -89,7 +92,8 @@ def __exit__(self, exc_type, exc_value, exc_traceback): plt.close(self.plot.figure) self.rc_context.__exit__(exc_type, exc_value, exc_traceback) - self.pd_option_context.__exit__(exc_type, exc_value, exc_traceback) + if PANDAS_LT_3: + self.pd_option_context.__exit__(exc_type, exc_value, exc_traceback) @dataclass diff --git a/pyproject.toml b/pyproject.toml index 3a45a69f56..544c0510ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,7 @@ dependencies = [ "mizani~=0.14.0", "numpy>=1.23.5", "scipy>=1.8.0", - "statsmodels>=0.14.5", + "statsmodels>=0.14.6", ] requires-python = ">=3.10" diff --git a/tests/baseline_images/test_scale_internals/missing_data_discrete_position_scale.png b/tests/baseline_images/test_scale_internals/missing_data_discrete_position_scale.png index 42fb33c9e0..f18e6e5eda 100644 Binary files a/tests/baseline_images/test_scale_internals/missing_data_discrete_position_scale.png and b/tests/baseline_images/test_scale_internals/missing_data_discrete_position_scale.png differ diff --git a/tests/test_scale_internals.py b/tests/test_scale_internals.py index f2716e50fa..bd74049657 100644 --- a/tests/test_scale_internals.py +++ b/tests/test_scale_internals.py @@ -5,6 +5,7 @@ import numpy.testing as npt import pandas as pd import pytest +from packaging.version import Version import plotnine as p9 from plotnine import ( @@ -57,6 +58,8 @@ ) from plotnine.scales.scales import make_scale +PANDAS_LT_3 = Version(pd.__version__) < Version("3.0") + # test palettes def test_discrete_color_palettes(): @@ -630,15 +633,15 @@ def test_missing_data_discrete_scale(): assert p == "missing_data_discrete_scale" +@pytest.mark.skipif(PANDAS_LT_3, reason="Fails on pandas<3") def test_missing_data_discrete_position_scale(): data = pd.DataFrame({"a": [1, 2, 3], "b": ["a", "b", None]}) + # The missing data is not removed p = ggplot(data, aes("a", "b")) + geom_point( aes(fill="b"), stroke=0, size=10 ) - - with pytest.warns(PlotnineWarning): - assert p == "missing_data_discrete_position_scale" + assert p == "missing_data_discrete_position_scale" data = pd.DataFrame(