Skip to content

Commit 38facba

Browse files
committed
Apply copy-on-write context only when pandas < 3.0
1 parent f355a46 commit 38facba

5 files changed

Lines changed: 26 additions & 14 deletions

File tree

doc/changelog.qmd

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
title: Changelog
33
---
44

5-
## v0.15.1
5+
## v0.15.3
6+
(2025-01-28)
7+
8+
### Enhancements
9+
10+
- Removed warnings about copy-on-write for pandas >= 3.0.0
11+
12+
## v0.15.2
613
(2025-12-12)
714

815
### Bug Fixes
9-
1016
- Fixed random failures for when using plotnine in quarto == 1.8.26. {{< issue 1017 >}}
1117

18+
## v0.15.1
19+
(2025-09-30)
20+
1221
### New
1322

1423
- Added [](:class:`~plotnine.scale_stroke_identity`) which was conspicuously missing!

plotnine/_utils/context.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
from typing import TYPE_CHECKING
55

66
import pandas as pd
7+
from packaging.version import Version
78

89
if TYPE_CHECKING:
910
from typing_extensions import Self
1011

1112
from plotnine import ggplot
1213
from plotnine.composition import Compose
1314

15+
PANDAS_LT_3 = Version(pd.__version__) < Version("3.0")
16+
1417

1518
def reopen(fig):
1619
"""
@@ -55,20 +58,20 @@ def __init__(self, plot: ggplot, show: bool = False):
5558

5659
# Contexts
5760
self.rc_context = mpl.rc_context(plot.theme.rcParams)
58-
# TODO: Remove this context when copy-on-write is permanent, i.e.
59-
# pandas >= 3.0
60-
self.pd_option_context = pd.option_context(
61-
"mode.copy_on_write",
62-
True,
63-
)
61+
if PANDAS_LT_3:
62+
self.pd_option_context = pd.option_context(
63+
"mode.copy_on_write",
64+
True,
65+
)
6466

6567
def __enter__(self) -> Self:
6668
"""
6769
Enclose in matplolib & pandas environments
6870
"""
6971

7072
self.rc_context.__enter__()
71-
self.pd_option_context.__enter__()
73+
if PANDAS_LT_3:
74+
self.pd_option_context.__enter__()
7275

7376
return self
7477

@@ -89,7 +92,8 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
8992
plt.close(self.plot.figure)
9093

9194
self.rc_context.__exit__(exc_type, exc_value, exc_traceback)
92-
self.pd_option_context.__exit__(exc_type, exc_value, exc_traceback)
95+
if PANDAS_LT_3:
96+
self.pd_option_context.__exit__(exc_type, exc_value, exc_traceback)
9397

9498

9599
@dataclass

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies = [
2828
"mizani~=0.14.0",
2929
"numpy>=1.23.5",
3030
"scipy>=1.8.0",
31-
"statsmodels>=0.14.5",
31+
"statsmodels>=0.14.6",
3232
]
3333
requires-python = ">=3.10"
3434

440 Bytes
Loading

tests/test_scale_internals.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,12 +633,11 @@ def test_missing_data_discrete_scale():
633633
def test_missing_data_discrete_position_scale():
634634
data = pd.DataFrame({"a": [1, 2, 3], "b": ["a", "b", None]})
635635

636+
# The missing data is not removed
636637
p = ggplot(data, aes("a", "b")) + geom_point(
637638
aes(fill="b"), stroke=0, size=10
638639
)
639-
640-
with pytest.warns(PlotnineWarning):
641-
assert p == "missing_data_discrete_position_scale"
640+
assert p == "missing_data_discrete_position_scale"
642641

643642

644643
data = pd.DataFrame(

0 commit comments

Comments
 (0)