Skip to content

Commit

Permalink
Merge pull request #4379 from ned2/suppress-futurewarning
Browse files Browse the repository at this point in the history
handle deprecation of pandas.Series.dt.to_pydatetime() calls and suppress their FutureWarnings
  • Loading branch information
alexcjohnson authored Oct 12, 2023
2 parents e19dec8 + ede7343 commit 57e4d1d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 13 additions & 2 deletions packages/python/plotly/_plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io
import re
import sys
import warnings

from _plotly_utils.optional_imports import get_module

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion packages/python/plotly/plotly/io/_json.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import datetime
import re
import sys
import warnings
from pytz import timezone
from _plotly_utils.optional_imports import get_module

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 57e4d1d

Please sign in to comment.