Skip to content

Commit df42901

Browse files
georgesittasizeigerman
authored andcommitted
Fix: convert NaN/NaT (np.nan) values to None (#2774)
1 parent e1a390e commit df42901

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

sqlmesh/core/model/definition.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pathlib import Path
1111

1212
import pandas as pd
13+
import numpy as np
1314
from astor import to_source
1415
from pydantic import Field
1516
from sqlglot import diff, exp
@@ -1172,6 +1173,7 @@ def render_seed(self) -> t.Iterator[QueryOrDF]:
11721173
datetime_columns = []
11731174
bool_columns = []
11741175
string_columns = []
1176+
11751177
for name, tpe in (self.columns_to_types_ or {}).items():
11761178
if tpe.this in (exp.DataType.Type.DATE, exp.DataType.Type.DATE32):
11771179
date_columns.append(name)
@@ -1196,7 +1198,7 @@ def render_seed(self) -> t.Iterator[QueryOrDF]:
11961198
cond=lambda x: x.notna(), # type: ignore
11971199
other=df[string_columns].astype(str), # type: ignore
11981200
)
1199-
yield df
1201+
yield df.replace({np.nan: None})
12001202

12011203
@property
12021204
def columns_to_types(self) -> t.Optional[t.Dict[str, exp.DataType]]:

tests/core/test_model.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,8 @@ def test_seed_model_custom_types(tmp_path):
851851

852852
with open(model_csv_path, "w", encoding="utf-8") as fd:
853853
fd.write(
854-
"""key,ds_date,ds_timestamp,b_a,b_b,i,i_str
855-
123,2022-01-01,2022-01-01,false,0,321,321
854+
"""key,ds_date,ds_timestamp,b_a,b_b,i,i_str,empty_date
855+
123,2022-01-01,2022-01-01,false,0,321,321,
856856
"""
857857
)
858858

@@ -867,6 +867,7 @@ def test_seed_model_custom_types(tmp_path):
867867
"b_b": "boolean",
868868
"i": "int",
869869
"i_str": "text",
870+
"empty_date": "date",
870871
},
871872
)
872873

@@ -893,6 +894,9 @@ def test_seed_model_custom_types(tmp_path):
893894
assert df["i_str"].dtype == "object"
894895
assert df["i_str"].iloc[0] == "321"
895896

897+
assert df["empty_date"].dtype == "object"
898+
assert df["empty_date"].iloc[0] is None
899+
896900

897901
def test_seed_with_special_characters_in_column(tmp_path, assert_exp_eq):
898902
config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))

0 commit comments

Comments
 (0)