Repository: alteryx/evalml
Pull request #3792 (2022-10-27 19:14:03)

Fix get_time_index bug
We were doing some unnecessary conversion of a DatetimeIndex into a Series and then back, which threw some errors with certain datetime data. This fixes both those issues!


Patch:
diff --git a/docs/source/release_notes.rst b/docs/source/release_notes.rst
index d6350ce3..363d3a8a 100644
--- a/docs/source/release_notes.rst
+++ b/docs/source/release_notes.rst
@@ -4,6 +4,7 @@ Release Notes
 **Future Releases**
     * Enhancements
     * Fixes
+        * Fixed bug with datetime conversion in ``get_time_index`` :pr:`3792`
         * Fixed bug where invalid anchored or offset frequencies were including the ``STLDecomposer`` in pipelines :pr:`3794`
     * Changes
     * Documentation Changes
diff --git a/evalml/utils/gen_utils.py b/evalml/utils/gen_utils.py
index 03866a0e..5a93d0cd 100644
--- a/evalml/utils/gen_utils.py
+++ b/evalml/utils/gen_utils.py
@@ -672,9 +672,9 @@ def get_time_index(X: pd.DataFrame, y: pd.Series, time_index_name: str):
         # Use the feature data's index, preferentially
         num_datetime_features = X.ww.select("Datetime").shape[1]
         if isinstance(X.index, pd.DatetimeIndex):
-            dt_col = pd.Series(X.index)
+            dt_col = X.index
         elif isinstance(y.index, pd.DatetimeIndex):
-            dt_col = pd.Series(y.index)
+            dt_col = y.index
         elif num_datetime_features == 0:
             raise ValueError(
                 "There are no Datetime features in the feature data and neither the feature nor the target data have a DateTime index.",
@@ -693,9 +693,9 @@ def get_time_index(X: pd.DataFrame, y: pd.Series, time_index_name: str):
                     f"Too many Datetime features provided in data and provided time_index column {time_index_name} not present in data.",
                 )
 
-    time_index = pd.DatetimeIndex(dt_col, freq=pd.infer_freq(dt_col)).rename(
-        y.index.name,
-    )
+    if not isinstance(dt_col, pd.DatetimeIndex):
+        dt_col = pd.DatetimeIndex(dt_col, freq="infer")
+    time_index = dt_col.rename(y.index.name)
     return time_index
 
 


Test patch:
diff --git a/evalml/tests/utils_tests/test_gen_utils.py b/evalml/tests/utils_tests/test_gen_utils.py
index 1bcf5f6c..525de471 100644
--- a/evalml/tests/utils_tests/test_gen_utils.py
+++ b/evalml/tests/utils_tests/test_gen_utils.py
@@ -973,3 +973,37 @@ def test_get_time_index(
     else:
         idx = get_time_index(X, y, time_index)
         assert isinstance(idx, pd.DatetimeIndex)
+
+
+def test_get_time_index_maintains_freq():
+    idx = pd.DatetimeIndex(
+        [
+            "1992-01-01T00:00:00Z",
+            "1992-02-01T00:00:00Z",
+            "1992-03-01T00:00:00Z",
+            "1992-04-01T00:00:00Z",
+            "1992-05-01T00:00:00Z",
+            "1992-06-01T00:00:00Z",
+            "1992-07-01T00:00:00Z",
+            "1992-08-01T00:00:00Z",
+            "1992-09-01T00:00:00Z",
+            "1992-10-01T00:00:00Z",
+            "1992-11-01T00:00:00Z",
+            "1992-12-01T00:00:00Z",
+            "1993-01-01T00:00:00Z",
+            "1993-02-01T00:00:00Z",
+        ],
+    )
+    X = pd.DataFrame(index=idx)
+    y = pd.Series(range(len(idx)), idx)
+    X.ww.init()
+    y.ww.init()
+    time_idx = get_time_index(X, y, None)
+    assert X.index.equals(time_idx)
+    assert y.index.equals(time_idx)
+
+    X = pd.DataFrame({"date": idx})
+    y = pd.Series(range(len(idx)))
+    X.ww.init()
+    y.ww.init()
+    time_idx = get_time_index(X, y, None)