Skip to content

Commit c7191a7

Browse files
authored
Fix: Ignore datetime column parsing erros when rendering seeds (#5199)
1 parent f73cdfe commit c7191a7

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

sqlmesh/core/model/definition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ def render_seed(self) -> t.Iterator[QueryOrDF]:
16101610
for column in [*date_columns, *datetime_columns]:
16111611
import pandas as pd
16121612

1613-
df[column] = pd.to_datetime(df[column])
1613+
df[column] = pd.to_datetime(df[column], infer_datetime_format=True, errors="ignore") # type: ignore
16141614

16151615
# extract datetime.date from pandas timestamp for DATE columns
16161616
for column in date_columns:

tests/core/test_model.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9972,6 +9972,31 @@ def test_seed_dont_coerce_na_into_null(tmp_path):
99729972
assert next(model.render(context=None)).to_dict() == {"code": {0: "NA"}}
99739973

99749974

9975+
def test_seed_coerce_datetime(tmp_path):
9976+
model_csv_path = (tmp_path / "model.csv").absolute()
9977+
9978+
with open(model_csv_path, "w", encoding="utf-8") as fd:
9979+
fd.write("bad_datetime\n9999-12-31 23:59:59")
9980+
9981+
expressions = d.parse(
9982+
f"""
9983+
MODEL (
9984+
name db.seed,
9985+
kind SEED (
9986+
path '{str(model_csv_path)}',
9987+
),
9988+
columns (
9989+
bad_datetime datetime,
9990+
),
9991+
);
9992+
"""
9993+
)
9994+
9995+
model = load_sql_based_model(expressions, path=Path("./examples/sushi/models/test_model.sql"))
9996+
df = next(model.render(context=None))
9997+
assert df["bad_datetime"].iloc[0] == "9999-12-31 23:59:59"
9998+
9999+
997510000
def test_missing_column_data_in_columns_key():
997610001
expressions = d.parse(
997710002
"""

0 commit comments

Comments
 (0)