44from typing import TYPE_CHECKING
55
66import pandas as pd
7+ from packaging .version import Version
78
89if 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
1518def 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
0 commit comments