Skip to content

Commit 9772ec1

Browse files
authored
Merge pull request #89 from isabelizimm/date-monitor
coerce date var to datetime
2 parents a25fb07 + 35e5cf7 commit 9772ec1

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

vetiver/monitor.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import plotly.express as px
22
import pandas as pd
3+
import numpy as np
34
from datetime import timedelta
45

56

@@ -41,7 +42,12 @@ def compute_metrics(
4142
4243
"""
4344

44-
df = data[[truth, estimate, date_var]].set_index(date_var).sort_index()
45+
df = data[[truth, estimate, date_var]].copy()
46+
47+
if not np.issubdtype(df[date_var], np.datetime64):
48+
df[date_var] = pd.to_datetime(df[date_var])
49+
50+
df = df.set_index(date_var).sort_index()
4551
lst = [_ for _ in _rolling_df(df=df, td=period)]
4652

4753
rows = []
@@ -169,7 +175,7 @@ def plot_metrics(
169175
color=metric,
170176
facet_row=metric,
171177
markers=dict(size=n),
172-
hover_data={"n": ':'},
178+
hover_data={"n": ":"},
173179
**kw,
174180
)
175181

vetiver/tests/test_monitor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ def test_compute():
3535
)
3636

3737

38+
def test_compute_coerce_datetime():
39+
40+
df_metrics = pd.DataFrame(
41+
{
42+
"index": ["2021-01-01", "2021-01-02", "2021-01-03"],
43+
"truth": [200, 201, 199],
44+
"pred": [198, 200, 199],
45+
}
46+
)
47+
td = timedelta(days=1)
48+
m = vetiver.compute_metrics(
49+
df_metrics, "index", td, metric_set=metric_set, truth="truth", estimate="pred"
50+
)
51+
assert isinstance(m, pd.DataFrame)
52+
assert m.shape == (4, 4)
53+
numpy.testing.assert_array_equal(
54+
m.metric.unique(),
55+
numpy.array(["mean_squared_error", "mean_absolute_error"], dtype=object),
56+
)
57+
58+
3859
def test_monitor(snapshot):
3960
snapshot.snapshot_dir = "./vetiver/tests/snapshots"
4061
m = vetiver.compute_metrics(

0 commit comments

Comments
 (0)