Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle deprecation of pandas.Series.dt.to_pydatetime() calls and suppress their FutureWarnings #4379

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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