Skip to content

Commit

Permalink
nicer error message
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaskruchten committed Jun 22, 2020
1 parent 216331e commit e823437
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,21 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds
x_is_date = True
elif x.dtype.type == np.object_:
x = x.astype(np.float64)

try:
x = x.astype(np.float64)
except ValueError:
raise ValueError(
"Could not convert value of 'x' ('%s') into a numeric type. "
"If 'x' contains stringified dates, please convert to a datetime column."
% args["x"]
)
if y.dtype.type == np.object_:
y = y.astype(np.float64)
try:
y = y.astype(np.float64)
except ValueError:
raise ValueError(
"Could not convert value of 'y' into a numeric type."
)

if attr_value == "lowess":
# missing ='drop' is the default value for lowess but not for OLS (None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def test_no_slope_ols_trendline():
@pytest.mark.parametrize("mode", ["ols", "lowess"])
def test_trendline_on_timeseries(mode):
df = px.data.stocks()

with pytest.raises(ValueError) as err_msg:
px.scatter(df, x="date", y="GOOG", trendline=mode)
assert "Could not convert value of 'x' ('date') into a numeric type." in str(
err_msg.value
)

df["date"] = pd.to_datetime(df["date"])
fig = px.scatter(df, x="date", y="GOOG", trendline=mode)
assert len(fig.data) == 2
Expand Down

0 comments on commit e823437

Please sign in to comment.