Skip to content

Commit df761da

Browse files
committed
Adopted another tricky way to fix the bug
1 parent 130c322 commit df761da

File tree

2 files changed

+11
-41
lines changed

2 files changed

+11
-41
lines changed

pandas/io/formats/style.py

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4081,43 +4081,10 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple
40814081
else:
40824082
return ret
40834083

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-
41174084
values = data.to_numpy()
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
4085+
# A tricky way to address the issue where np.nanmin/np.nanmax fail to handle pd.NA.
4086+
left = np.nanmin(data.min(skipna=True)) if vmin is None else vmin
4087+
right = np.nanmax(data.max(skipna=True)) if vmax is None else vmax
41214088
z: float = 0 # adjustment to translate data
41224089

41234090
if align == "mid":

pandas/tests/io/formats/style/test_bar.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,11 @@ def test_bar_invalid_color_type_error_raises():
330330
df.style.bar(color=['#d65f5f', '#5fba7d', '#abcdef']).to_html()
331331

332332

333-
def test_styler_bar_with_missing_values():
334-
df = DataFrame({"A": [1, 2, NA, 4]})
335-
expected_substring = "linear-gradient"
336-
html_output = df.style.bar(subset='A').to_html()
337-
assert expected_substring in html_output
333+
def test_styler_bar_with_NA_values():
334+
df1 = DataFrame({"A": [1, 2, NA, 4]})
335+
df2 = DataFrame([[NA, NA], [NA, NA]])
336+
expected_substring = "style type="
337+
html_output1 = df1.style.bar(subset='A').to_html()
338+
html_output2 = df2.style.bar(align="left", axis=None).to_html()
339+
assert expected_substring in html_output1
340+
assert expected_substring in html_output2

0 commit comments

Comments
 (0)