diff --git a/packages/python/plotly/_plotly_utils/utils.py b/packages/python/plotly/_plotly_utils/utils.py index 9254b7f23d2..787ad5077a8 100644 --- a/packages/python/plotly/_plotly_utils/utils.py +++ b/packages/python/plotly/_plotly_utils/utils.py @@ -167,15 +167,19 @@ def encode_as_sage(obj): @staticmethod def encode_as_pandas(obj): - """Attempt to convert pandas.NaT""" + """Attempt to convert pandas.NaT / pandas.NA""" pandas = get_module("pandas", should_load=False) if not pandas: raise NotEncodable if obj is pandas.NaT: return None - else: - raise NotEncodable + + # pandas.NA was introduced in pandas 1.0 + if hasattr(pandas, "NA") and obj is pandas.NA: + return None + + raise NotEncodable @staticmethod def encode_as_numpy(obj): diff --git a/packages/python/plotly/plotly/express/_imshow.py b/packages/python/plotly/plotly/express/_imshow.py index 75c15d44caf..6b10b4fcc78 100644 --- a/packages/python/plotly/plotly/express/_imshow.py +++ b/packages/python/plotly/plotly/express/_imshow.py @@ -351,7 +351,7 @@ def imshow( binary_string = img.ndim >= (3 + slice_dimensions) and not is_dataframe # Cast bools to uint8 (also one byte) - if img.dtype == np.bool: + if img.dtype == bool: img = 255 * img.astype(np.uint8) if range_color is not None: diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py index c9b39cb201c..d4b33db2e40 100644 --- a/packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py @@ -48,14 +48,14 @@ def test_automatic_zmax_from_dtype(): np.uint8: 2**8 - 1, np.uint16: 2**16 - 1, np.float: 1, - np.bool: 255, + bool: 255, } for key, val in dtypes_dict.items(): img = np.array([0, 1], dtype=key) img = np.dstack((img,) * 3) fig = px.imshow(img, binary_string=False) # For uint8 in "infer" mode we don't pass zmin/zmax unless specified - if key in [np.uint8, np.bool]: + if key in [np.uint8, bool]: assert fig.data[0]["zmax"] is None else: assert fig.data[0]["zmax"] == (val, val, val, 255)