@@ -4081,9 +4081,43 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple
4081
4081
else :
4082
4082
return ret
4083
4083
4084
+ def replace_pd_NA_with_np_nan (data_structure ):
4085
+ """
4086
+ Recursively replace pd.NA with np.nan in a nested list or array.
4087
+
4088
+ This function traverses a nested structure (like lists or numpy arrays)
4089
+ and replaces occurrences of Pandas' NA values (pd.NA) with NumPy's NaN values (np.nan).
4090
+ It handles nested lists and arrays, applying the replacement recursively.
4091
+
4092
+ Parameters:
4093
+ data_structure (list, np.ndarray, or any): A nested list, numpy array, or any other object.
4094
+ If it's a list or numpy array, the function will
4095
+ process its elements recursively.
4096
+
4097
+ Returns:
4098
+ list, np.ndarray, or any: A new object with the same structure as `data_structure`,
4099
+ where all pd.NA values have been replaced with np.nan.
4100
+ The type of the returned object is the same as the input.
4101
+
4102
+ Example:
4103
+ >>> data = [1, pd.NA, [3, pd.NA, [pd.NA, 5]], pd.NA]
4104
+ >>> replace_pd_NA_with_np_nan(data)
4105
+ [1, nan, [3, nan, [nan, 5]], nan]
4106
+ """
4107
+ if isinstance (data_structure , list ):
4108
+ # Process each item in the list recursively
4109
+ return [replace_pd_NA_with_np_nan (element ) for element in data_structure ]
4110
+ elif isinstance (data_structure , np .ndarray ):
4111
+ # Convert numpy array elements recursively
4112
+ return np .array ([replace_pd_NA_with_np_nan (element ) for element in data_structure ])
4113
+ else :
4114
+ # Replace pd.NA with np.nan for individual elements
4115
+ return np .nan if pd .isna (data_structure ) else data_structure
4116
+
4084
4117
values = data .to_numpy ()
4085
- left = np .nanmin (values ) if vmin is None else vmin
4086
- right = np .nanmax (values ) if vmax is None else vmax
4118
+ np_values = replace_pd_NA_with_np_nan (values )
4119
+ left = np .nanmin (np_values ) if vmin is None else vmin
4120
+ right = np .nanmax (np_values ) if vmax is None else vmax
4087
4121
z : float = 0 # adjustment to translate data
4088
4122
4089
4123
if align == "mid" :
0 commit comments