|
| 1 | +from datetime import date, datetime |
| 2 | + |
1 | 3 | import numpy as np |
2 | 4 | import polars as pl |
3 | 5 | import pytest |
4 | 6 | from xlsxwriter import Workbook |
5 | 7 |
|
6 | 8 | from ert.config import DataSource, DesignMatrix, GenKwConfig |
7 | 9 | from ert.config.design_matrix import DESIGN_MATRIX_GROUP |
| 10 | +from ert.config.parsing.config_errors import ConfigWarning |
8 | 11 | from tests.ert.conftest import _create_design_matrix |
9 | 12 |
|
10 | 13 |
|
@@ -545,3 +548,33 @@ def test_that_numeric_string_columns_are_converted(tmp_path): |
545 | 548 | np.testing.assert_equal(df["a"], np.array([1, 2, 3])) |
546 | 549 | np.testing.assert_equal(df["b"], np.array([0, 2.2, 0.1])) |
547 | 550 | 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