Skip to content

Commit f959c20

Browse files
Add check and conversion for datetime (#12998)
1 parent e980540 commit f959c20

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/ert/config/design_matrix.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import polars as pl
1010
from polars.exceptions import InvalidOperationError
1111

12+
from ert.config.parsing.config_errors import ConfigWarning
13+
1214
from .distribution import RawSettings
1315
from .gen_kw_config import DataSource, GenKwConfig
1416
from .parsing import ConfigValidationError, ErrorInfo
@@ -239,6 +241,25 @@ def read_and_validate_design_matrix(
239241
)
240242
if design_matrix_df.is_empty():
241243
raise ValueError("Design sheet body is empty.")
244+
245+
# Design matrix does not support datetime columns,
246+
# so we convert them to strings and warn the user.
247+
datetime_cols = design_matrix_df.select(pl.col(pl.Date, pl.Datetime)).columns
248+
if len(datetime_cols) > 0:
249+
datetime_col_indices = [
250+
design_matrix_df.columns.index(col) for col in datetime_cols
251+
]
252+
affected_param_names = [param_names[i] for i in datetime_col_indices]
253+
ConfigWarning.warn(
254+
"The design matrix contains date/datetime columns which are not "
255+
"supported and will be converted to strings for internal use. "
256+
"The following columns will be converted to strings: "
257+
f"{', '.join(map(str, affected_param_names))}."
258+
)
259+
design_matrix_df = design_matrix_df.with_columns(
260+
pl.col(pl.Date, pl.Datetime).cast(pl.String)
261+
)
262+
242263
string_cols = [
243264
col for col, dtype in design_matrix_df.schema.items() if dtype == pl.String
244265
]

tests/ert/unit_tests/sensitivity_analysis/test_design_matrix.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from datetime import date, datetime
2+
13
import numpy as np
24
import polars as pl
35
import pytest
46
from xlsxwriter import Workbook
57

68
from ert.config import DataSource, DesignMatrix, GenKwConfig
79
from ert.config.design_matrix import DESIGN_MATRIX_GROUP
10+
from ert.config.parsing.config_errors import ConfigWarning
811
from tests.ert.conftest import _create_design_matrix
912

1013

@@ -545,3 +548,33 @@ def test_that_numeric_string_columns_are_converted(tmp_path):
545548
np.testing.assert_equal(df["a"], np.array([1, 2, 3]))
546549
np.testing.assert_equal(df["b"], np.array([0, 2.2, 0.1]))
547550
np.testing.assert_equal(df["c"], np.array(["10", "high", "medium"]))
551+
552+
553+
def test_that_excel_datetime_columns_are_converted_to_strings_with_warning(tmp_path):
554+
design_path = tmp_path / "design_matrix.xlsx"
555+
with Workbook(design_path) as wb:
556+
ws = wb.add_worksheet("DesignSheet")
557+
date_format = wb.add_format({"num_format": "yyyy-mm-dd"})
558+
559+
ws.write(0, 0, "REAL")
560+
ws.write(0, 1, "date_col")
561+
ws.write(0, 2, "value")
562+
563+
ws.write(1, 0, 0)
564+
ws.write_datetime(1, 1, date(2026, 3, 1), date_format) # type: ignore[arg-type]
565+
ws.write(1, 2, 1.5)
566+
567+
ws.write(2, 0, 1)
568+
ws.write_datetime(2, 1, datetime(2026, 3, 2, 10, 30), date_format)
569+
ws.write(2, 2, 2.5)
570+
571+
with pytest.warns(
572+
ConfigWarning,
573+
match="The design matrix contains date/datetime columns",
574+
):
575+
design_matrix = DesignMatrix(design_path, "DesignSheet", None)
576+
577+
df = design_matrix.design_matrix_df
578+
assert df.schema["date_col"] == pl.String
579+
assert "2026-03-01" in df["date_col"][0]
580+
assert "2026-03-02" in df["date_col"][1]

0 commit comments

Comments
 (0)