Skip to content

Commit 594286e

Browse files
nicer error message
1 parent af038f2 commit 594286e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,21 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
279279
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds
280280
x_is_date = True
281281
elif x.dtype.type == np.object_:
282-
x = x.astype(np.float64)
283-
282+
try:
283+
x = x.astype(np.float64)
284+
except ValueError:
285+
raise ValueError(
286+
"Could not convert value of 'x' ('%s') into a numeric type. "
287+
"If 'x' contains stringified dates, please convert to a datetime column."
288+
% args["x"]
289+
)
284290
if y.dtype.type == np.object_:
285-
y = y.astype(np.float64)
291+
try:
292+
y = y.astype(np.float64)
293+
except ValueError:
294+
raise ValueError(
295+
"Could not convert value of 'y' into a numeric type."
296+
)
286297

287298
if attr_value == "lowess":
288299
# missing ='drop' is the default value for lowess but not for OLS (None)

Diff for: packages/python/plotly/plotly/tests/test_core/test_px/test_trendline.py

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ def test_no_slope_ols_trendline():
9494
@pytest.mark.parametrize("mode", ["ols", "lowess"])
9595
def test_trendline_on_timeseries(mode):
9696
df = px.data.stocks()
97+
98+
with pytest.raises(ValueError) as err_msg:
99+
px.scatter(df, x="date", y="GOOG", trendline=mode)
100+
assert "Could not convert value of 'x' ('date') into a numeric type." in str(
101+
err_msg.value
102+
)
103+
97104
df["date"] = pd.to_datetime(df["date"])
98105
fig = px.scatter(df, x="date", y="GOOG", trendline=mode)
99106
assert len(fig.data) == 2

0 commit comments

Comments
 (0)