Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 7ebf726

Browse files
timeseries trendlines (closes #4)
1 parent 66f60bf commit 7ebf726

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

plotly_express/_core.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,22 @@ def make_trace_kwargs(args, trace_spec, g, mapping_labels, sizeref, color_range)
144144
elif k == "trendline":
145145
if v in ["ols", "lowess"] and args["x"] and args["y"] and len(g) > 1:
146146
import statsmodels.api as sm
147+
import numpy as np
148+
149+
# sorting is bad but trace_specs with "trendline" have no other attrs
150+
g2 = g.sort_values(by=args["x"])
151+
y = g2[args["y"]]
152+
x = g2[args["x"]]
153+
result["x"] = x
154+
155+
if x.dtype.type == np.datetime64:
156+
x = x.astype(int) / 10 ** 9 # convert to unix epoch seconds
147157

148158
if v == "lowess":
149-
trendline = sm.nonparametric.lowess(g[args["y"]], g[args["x"]])
150-
result["x"] = trendline[:, 0]
159+
trendline = sm.nonparametric.lowess(y, x)
151160
result["y"] = trendline[:, 1]
152161
hover_header = "<b>LOWESS trendline</b><br><br>"
153162
elif v == "ols":
154-
# sorting is bad but trace_specs with "trendline" have no other attrs
155-
g2 = g.sort_values(by=args["x"])
156-
y = g2[args["y"]]
157-
x = g2[args["x"]]
158-
result["x"] = x
159163
fitted = sm.OLS(y, sm.add_constant(x)).fit()
160164
result["y"] = fitted.predict()
161165
hover_header = "<b>OLS trendline</b><br>"

0 commit comments

Comments
 (0)