diff --git a/CHANGELOG.md b/CHANGELOG.md index 895f7e7c72f..c2a32064c24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Repair crash on Matplotlib 3.8 related to get_offset_position [[#4372](https://github.com/plotly/plotly.py/pull/4372)], +- Handle deprecation of `pandas.Series.dt.to_pydatetime()` calls and suppress the `FutureWarning` they currently emit. [[#4379](https://github.com/plotly/plotly.py/pull/4379)] ## [5.17.0] - 2023-09-15 diff --git a/packages/python/plotly/_plotly_utils/basevalidators.py b/packages/python/plotly/_plotly_utils/basevalidators.py index 35dec09dd1e..71c01b41168 100644 --- a/packages/python/plotly/_plotly_utils/basevalidators.py +++ b/packages/python/plotly/_plotly_utils/basevalidators.py @@ -7,6 +7,7 @@ import io import re import sys +import warnings from _plotly_utils.optional_imports import get_module @@ -100,7 +101,11 @@ def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False): elif v.dtype.kind == "M": # Convert datetime Series/Index to numpy array of datetimes if isinstance(v, pd.Series): - v = v.dt.to_pydatetime() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", FutureWarning) + # Series.dt.to_pydatetime will return Index[object] + # https://github.com/pandas-dev/pandas/pull/52459 + v = np.array(v.dt.to_pydatetime()) else: # DatetimeIndex v = v.to_pydatetime() @@ -109,7 +114,13 @@ def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False): if dtype.kind in numeric_kinds: v = v.values elif dtype.kind == "M": - v = [row.dt.to_pydatetime().tolist() for i, row in v.iterrows()] + with warnings.catch_warnings(): + warnings.simplefilter("ignore", FutureWarning) + # Series.dt.to_pydatetime will return Index[object] + # https://github.com/pandas-dev/pandas/pull/52459 + v = [ + np.array(row.dt.to_pydatetime()).tolist() for i, row in v.iterrows() + ] if not isinstance(v, np.ndarray): # v has its own logic on how to convert itself into a numpy array diff --git a/packages/python/plotly/plotly/io/_json.py b/packages/python/plotly/plotly/io/_json.py index b7df0c2e5c5..15375632c7a 100644 --- a/packages/python/plotly/plotly/io/_json.py +++ b/packages/python/plotly/plotly/io/_json.py @@ -1,6 +1,7 @@ import json import decimal import datetime +import warnings from pathlib import Path from plotly.io._utils import validate_coerce_fig_to_dict, validate_coerce_output_type @@ -535,7 +536,11 @@ def clean_to_json_compatible(obj, **kwargs): return np.ascontiguousarray(obj.values) elif obj.dtype.kind == "M": if isinstance(obj, pd.Series): - dt_values = obj.dt.to_pydatetime().tolist() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", FutureWarning) + # Series.dt.to_pydatetime will return Index[object] + # https://github.com/pandas-dev/pandas/pull/52459 + dt_values = np.array(obj.dt.to_pydatetime()).tolist() else: # DatetimeIndex dt_values = obj.to_pydatetime().tolist() diff --git a/packages/python/plotly/plotly/tests/test_io/test_to_from_plotly_json.py b/packages/python/plotly/plotly/tests/test_io/test_to_from_plotly_json.py index cdcd78a5a41..ff8ad213c84 100644 --- a/packages/python/plotly/plotly/tests/test_io/test_to_from_plotly_json.py +++ b/packages/python/plotly/plotly/tests/test_io/test_to_from_plotly_json.py @@ -8,6 +8,7 @@ import datetime import re import sys +import warnings from pytz import timezone from _plotly_utils.optional_imports import get_module @@ -194,7 +195,13 @@ def to_str(v): if isinstance(datetime_array, list): dt_values = [to_str(d) for d in datetime_array] elif isinstance(datetime_array, pd.Series): - dt_values = [to_str(d) for d in datetime_array.dt.to_pydatetime().tolist()] + with warnings.catch_warnings(): + warnings.simplefilter("ignore", FutureWarning) + # Series.dt.to_pydatetime will return Index[object] + # https://github.com/pandas-dev/pandas/pull/52459 + dt_values = [ + to_str(d) for d in np.array(datetime_array.dt.to_pydatetime()).tolist() + ] elif isinstance(datetime_array, pd.DatetimeIndex): dt_values = [to_str(d) for d in datetime_array.to_pydatetime().tolist()] else: # numpy datetime64 array