From f6730269ef2b364a7089d2a6384a4d45cfafb2f6 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 2 May 2022 09:22:44 -0400 Subject: [PATCH 1/3] handle pandas.NA --- packages/python/plotly/_plotly_utils/utils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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): From 121b876ab930cfca60ae290b651b70e574029586 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 2 May 2022 09:22:47 -0400 Subject: [PATCH 2/3] stop relying on np.bool --- .../python/plotly/plotly/express/_imshow.py | 4 ++-- .../test_optional/test_px/test_imshow.py | 24 ++++++------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/packages/python/plotly/plotly/express/_imshow.py b/packages/python/plotly/plotly/express/_imshow.py index 75c15d44caf..14ff5fcd10f 100644 --- a/packages/python/plotly/plotly/express/_imshow.py +++ b/packages/python/plotly/plotly/express/_imshow.py @@ -50,7 +50,7 @@ def _infer_zmax_from_type(img): elif im_max <= 65535 * rtol: return 65535 else: - return 2**32 + return 2 ** 32 def imshow( @@ -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..8d89d8390ac 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 @@ -45,17 +45,17 @@ def test_zmax(): def test_automatic_zmax_from_dtype(): dtypes_dict = { - np.uint8: 2**8 - 1, - np.uint16: 2**16 - 1, + 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) @@ -195,9 +195,7 @@ def test_imshow_xarray_slicethrough(): def test_imshow_labels_and_ranges(): - fig = px.imshow( - [[1, 2], [3, 4], [5, 6]], - ) + fig = px.imshow([[1, 2], [3, 4], [5, 6]],) assert fig.layout.xaxis.title.text is None assert fig.layout.yaxis.title.text is None assert fig.layout.coloraxis.colorbar.title.text is None @@ -386,11 +384,7 @@ def test_facet_col(facet_col, binary_string): @pytest.mark.parametrize("binary_string", [False, True]) def test_animation_frame_grayscale(animation_frame, binary_string): img = np.random.randint(255, size=(10, 9, 8)).astype(np.uint8) - fig = px.imshow( - img, - animation_frame=animation_frame, - binary_string=binary_string, - ) + fig = px.imshow(img, animation_frame=animation_frame, binary_string=binary_string,) nslices = img.shape[animation_frame] assert len(fig.frames) == nslices @@ -399,11 +393,7 @@ def test_animation_frame_grayscale(animation_frame, binary_string): @pytest.mark.parametrize("binary_string", [False, True]) def test_animation_frame_rgb(animation_frame, binary_string): img = np.random.randint(255, size=(10, 9, 8, 3)).astype(np.uint8) - fig = px.imshow( - img, - animation_frame=animation_frame, - binary_string=binary_string, - ) + fig = px.imshow(img, animation_frame=animation_frame, binary_string=binary_string,) nslices = img.shape[animation_frame] assert len(fig.frames) == nslices From a208539c3019771c639e2430a68c815221bb63da Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 2 May 2022 09:43:37 -0400 Subject: [PATCH 3/3] blacken --- .../python/plotly/plotly/express/_imshow.py | 2 +- .../test_optional/test_px/test_imshow.py | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/python/plotly/plotly/express/_imshow.py b/packages/python/plotly/plotly/express/_imshow.py index 14ff5fcd10f..6b10b4fcc78 100644 --- a/packages/python/plotly/plotly/express/_imshow.py +++ b/packages/python/plotly/plotly/express/_imshow.py @@ -50,7 +50,7 @@ def _infer_zmax_from_type(img): elif im_max <= 65535 * rtol: return 65535 else: - return 2 ** 32 + return 2**32 def imshow( 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 8d89d8390ac..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 @@ -45,8 +45,8 @@ def test_zmax(): def test_automatic_zmax_from_dtype(): dtypes_dict = { - np.uint8: 2 ** 8 - 1, - np.uint16: 2 ** 16 - 1, + np.uint8: 2**8 - 1, + np.uint16: 2**16 - 1, np.float: 1, bool: 255, } @@ -195,7 +195,9 @@ def test_imshow_xarray_slicethrough(): def test_imshow_labels_and_ranges(): - fig = px.imshow([[1, 2], [3, 4], [5, 6]],) + fig = px.imshow( + [[1, 2], [3, 4], [5, 6]], + ) assert fig.layout.xaxis.title.text is None assert fig.layout.yaxis.title.text is None assert fig.layout.coloraxis.colorbar.title.text is None @@ -384,7 +386,11 @@ def test_facet_col(facet_col, binary_string): @pytest.mark.parametrize("binary_string", [False, True]) def test_animation_frame_grayscale(animation_frame, binary_string): img = np.random.randint(255, size=(10, 9, 8)).astype(np.uint8) - fig = px.imshow(img, animation_frame=animation_frame, binary_string=binary_string,) + fig = px.imshow( + img, + animation_frame=animation_frame, + binary_string=binary_string, + ) nslices = img.shape[animation_frame] assert len(fig.frames) == nslices @@ -393,7 +399,11 @@ def test_animation_frame_grayscale(animation_frame, binary_string): @pytest.mark.parametrize("binary_string", [False, True]) def test_animation_frame_rgb(animation_frame, binary_string): img = np.random.randint(255, size=(10, 9, 8, 3)).astype(np.uint8) - fig = px.imshow(img, animation_frame=animation_frame, binary_string=binary_string,) + fig = px.imshow( + img, + animation_frame=animation_frame, + binary_string=binary_string, + ) nslices = img.shape[animation_frame] assert len(fig.frames) == nslices