Skip to content

Commit e6f0102

Browse files
committed
adjust tests for pandas 3
1 parent dca60fa commit e6f0102

File tree

1 file changed

+54
-15
lines changed

1 file changed

+54
-15
lines changed

tests/test_optional/test_utils/test_utils.py

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,24 @@ def test_encode_customdata_datetime_series(self):
311311
fig_json = _json.dumps(
312312
fig, cls=utils.PlotlyJSONEncoder, separators=(",", ":"), sort_keys=True
313313
)
314-
self.assertTrue(
315-
fig_json.startswith(
316-
'{"data":[{"customdata":["2010-01-01T00:00:00.000000000","2010-01-02T00:00:00.000000000"]'
314+
315+
fig_from_json = Figure(_json.loads(fig_json))
316+
317+
import pandas
318+
319+
if Version(pandas.__version__) >= Version("3.0.0"):
320+
# Starting in pandas 3, datetimes have ms precision by default
321+
# https://pandas.pydata.org/docs/whatsnew/v3.0.0.html#datetime-timedelta-resolution-inference
322+
assert fig_from_json.data[0].customdata == (
323+
"2010-01-01T00:00:00.000000",
324+
"2010-01-02T00:00:00.000000",
325+
)
326+
else:
327+
# Before pandas 3, datetimes have ns precision by default
328+
assert fig_from_json.data[0].customdata == (
329+
"2010-01-01T00:00:00.000000000",
330+
"2010-01-02T00:00:00.000000000",
317331
)
318-
)
319332

320333
def test_encode_customdata_datetime_homogeneous_dataframe(self):
321334
df = pd.DataFrame(
@@ -332,13 +345,24 @@ def test_encode_customdata_datetime_homogeneous_dataframe(self):
332345
fig_json = _json.dumps(
333346
fig, cls=utils.PlotlyJSONEncoder, separators=(",", ":"), sort_keys=True
334347
)
335-
self.assertTrue(
336-
fig_json.startswith(
337-
'{"data":[{"customdata":'
338-
'[["2010-01-01T00:00:00.000000000","2011-01-01T00:00:00.000000000"],'
339-
'["2010-01-02T00:00:00.000000000","2011-01-02T00:00:00.000000000"]'
348+
349+
fig_from_json = Figure(_json.loads(fig_json))
350+
351+
import pandas
352+
353+
if Version(pandas.__version__) >= Version("3.0.0"):
354+
# Starting in pandas 3, datetimes have ms precision by default
355+
# https://pandas.pydata.org/docs/whatsnew/v3.0.0.html#datetime-timedelta-resolution-inference
356+
assert fig_from_json.data[0].customdata == (
357+
["2010-01-01T00:00:00.000000", "2011-01-01T00:00:00.000000"],
358+
["2010-01-02T00:00:00.000000", "2011-01-02T00:00:00.000000"],
359+
)
360+
else:
361+
# Before pandas 3, datetimes have ns precision by default
362+
assert fig_from_json.data[0].customdata == (
363+
["2010-01-01T00:00:00.000000000", "2011-01-01T00:00:00.000000000"],
364+
["2010-01-02T00:00:00.000000000", "2011-01-02T00:00:00.000000000"],
340365
)
341-
)
342366

343367
def test_encode_customdata_datetime_inhomogeneous_dataframe(self):
344368
df = pd.DataFrame(
@@ -384,11 +408,26 @@ def test_datetime_dot_date(self):
384408
def test_numpy_datetime64(self):
385409
a = pd.date_range("2011-07-11", "2011-07-13", freq="D").values
386410
j1 = _json.dumps(a, cls=utils.PlotlyJSONEncoder)
387-
assert (
388-
j1 == '["2011-07-11T00:00:00.000000000", '
389-
'"2011-07-12T00:00:00.000000000", '
390-
'"2011-07-13T00:00:00.000000000"]'
391-
)
411+
412+
from_json = _json.loads(j1)
413+
414+
import pandas
415+
416+
if Version(pandas.__version__) >= Version("3.0.0"):
417+
# Starting in pandas 3, datetimes have ms precision by default
418+
# https://pandas.pydata.org/docs/whatsnew/v3.0.0.html#datetime-timedelta-resolution-inference
419+
assert from_json == [
420+
"2011-07-11T00:00:00.000000",
421+
"2011-07-12T00:00:00.000000",
422+
"2011-07-13T00:00:00.000000",
423+
]
424+
else:
425+
# Before pandas 3, datetimes have ns precision by default
426+
assert from_json == [
427+
"2011-07-11T00:00:00.000000000",
428+
"2011-07-12T00:00:00.000000000",
429+
"2011-07-13T00:00:00.000000000",
430+
]
392431

393432
def test_pil_image_encoding(self):
394433
img_path = os.path.join(

0 commit comments

Comments
 (0)