Skip to content

Commit e823437

Browse files
nicer error message
1 parent 216331e commit e823437

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
@@ -291,10 +291,21 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
291291
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds
292292
x_is_date = True
293293
elif x.dtype.type == np.object_:
294-
x = x.astype(np.float64)
295-
294+
try:
295+
x = x.astype(np.float64)
296+
except ValueError:
297+
raise ValueError(
298+
"Could not convert value of 'x' ('%s') into a numeric type. "
299+
"If 'x' contains stringified dates, please convert to a datetime column."
300+
% args["x"]
301+
)
296302
if y.dtype.type == np.object_:
297-
y = y.astype(np.float64)
303+
try:
304+
y = y.astype(np.float64)
305+
except ValueError:
306+
raise ValueError(
307+
"Could not convert value of 'y' into a numeric type."
308+
)
298309

299310
if attr_value == "lowess":
300311
# 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)