33import math
44import posixpath
55import re
6+ from functools import cache , wraps
7+
8+ import pandas as pd
9+
10+ from ..utils import in_context
11+
12+
13+ def _pandas_3_0 ():
14+ """Silence pandas warnings and opt in to future behavior.
15+
16+ This sets pandas behavior to 3.0+ defaults.
17+ Prior to pandas 3.0, the fillna() and replace() methods would convert
18+ object columns to float64 if the resulting series was all float64.
19+ In 3.0+, you need to use infer_objects() to do this.
20+
21+ This also opts-in to copy-on-write, which previously required `copy=False`
22+ to be set.
23+ """
24+ if args := _pandas_3_0_options ():
25+ return pd .option_context (* args )
26+
27+ import contextlib
28+
29+ return contextlib .nullcontext ()
30+
31+
32+ @cache
33+ def _pandas_3_0_options ():
34+ options = [
35+ ("future.no_silent_downcasting" , True ),
36+ ("mode.copy_on_write" , True ),
37+ ]
38+
39+ args = []
40+ for option in options :
41+ try :
42+ pd .get_option (option [0 ])
43+ except KeyError :
44+ continue
45+ args .extend (option )
46+ return args
647
748
849def _link_with_html (string , html_path = None , heading = None , pdf_format = False ):
@@ -113,6 +154,7 @@ def combine_extensions(lst, html_path=None, heading_lst=None, pdf_format=True):
113154 return new_lst
114155
115156
157+ @in_context (_pandas_3_0 ())
116158def drop_unused_entities (df ):
117159 """Remove columns from a dataframe where all values in the column are NaNs.
118160
@@ -130,8 +172,7 @@ def drop_unused_entities(df):
130172 df : pandas.DataFrame
131173 DataFrame with columns associated with unused entities removed.
132174 """
133- df = df .replace ("" , math .nan ).dropna (axis = 1 , how = "all" ).fillna ("" )
134- return df
175+ return df .replace ("" , math .nan ).dropna (axis = 1 , how = "all" ).fillna ("" )
135176
136177
137178def flatten_multiindexed_columns (df ):
@@ -412,3 +453,18 @@ def num2words(integer, to="ordinal"):
412453 return mapper [integer ]
413454 except KeyError :
414455 raise ValueError (f"Input { integer } is not supported." )
456+
457+
458+ def propagate_fence_exception (func ):
459+ """Decorator to prevent superfences from swallowing exceptions."""
460+
461+ @wraps (func )
462+ def wrapper (* args , ** kwargs ):
463+ try :
464+ return func (* args , ** kwargs )
465+ except Exception as e :
466+ from pymdownx .superfences import SuperFencesException
467+
468+ raise SuperFencesException from e
469+
470+ return wrapper
0 commit comments