Skip to content

Commit 4e5f8ab

Browse files
Merge pull request #3705 from plotly/pydatacompat
Compatibility with evolving PyData libraries
2 parents a1ef556 + a208539 commit 4e5f8ab

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

Diff for: packages/python/plotly/_plotly_utils/utils.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,19 @@ def encode_as_sage(obj):
167167

168168
@staticmethod
169169
def encode_as_pandas(obj):
170-
"""Attempt to convert pandas.NaT"""
170+
"""Attempt to convert pandas.NaT / pandas.NA"""
171171
pandas = get_module("pandas", should_load=False)
172172
if not pandas:
173173
raise NotEncodable
174174

175175
if obj is pandas.NaT:
176176
return None
177-
else:
178-
raise NotEncodable
177+
178+
# pandas.NA was introduced in pandas 1.0
179+
if hasattr(pandas, "NA") and obj is pandas.NA:
180+
return None
181+
182+
raise NotEncodable
179183

180184
@staticmethod
181185
def encode_as_numpy(obj):

Diff for: packages/python/plotly/plotly/express/_imshow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def imshow(
351351
binary_string = img.ndim >= (3 + slice_dimensions) and not is_dataframe
352352

353353
# Cast bools to uint8 (also one byte)
354-
if img.dtype == np.bool:
354+
if img.dtype == bool:
355355
img = 255 * img.astype(np.uint8)
356356

357357
if range_color is not None:

Diff for: packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ def test_automatic_zmax_from_dtype():
4848
np.uint8: 2**8 - 1,
4949
np.uint16: 2**16 - 1,
5050
np.float: 1,
51-
np.bool: 255,
51+
bool: 255,
5252
}
5353
for key, val in dtypes_dict.items():
5454
img = np.array([0, 1], dtype=key)
5555
img = np.dstack((img,) * 3)
5656
fig = px.imshow(img, binary_string=False)
5757
# For uint8 in "infer" mode we don't pass zmin/zmax unless specified
58-
if key in [np.uint8, np.bool]:
58+
if key in [np.uint8, bool]:
5959
assert fig.data[0]["zmax"] is None
6060
else:
6161
assert fig.data[0]["zmax"] == (val, val, val, 255)

0 commit comments

Comments
 (0)