Skip to content

Commit

Permalink
feat(python): Minor type-inference update for read_database (pola-r…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie authored Apr 21, 2024
1 parent e3d0ef2 commit 53c8ec9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion py-polars/polars/io/database/_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Int32,
Int64,
List,
Null,
String,
Time,
UInt8,
Expand Down Expand Up @@ -164,6 +165,10 @@ def _infer_dtype_from_database_typename(
elif value.startswith("BOOL"):
dtype = Boolean

# null dtype; odd, but valid
elif value == "NULL":
dtype = Null

# temporal dtypes
elif value.startswith(("DATETIME", "TIMESTAMP")) and not (value.endswith("[D]")):
if any((tz in value.replace(" ", "")) for tz in ("TZ", "TIMEZONE")):
Expand All @@ -173,7 +178,7 @@ def _infer_dtype_from_database_typename(
dtype = Datetime(time_unit=(unit or "us")) # type: ignore[arg-type]
else:
value = re.sub(r"\d", "", value)
if value in ("INTERVAL", "TIMEDELTA"):
if value in ("INTERVAL", "TIMEDELTA", "DURATION"):
dtype = Duration
elif value == "DATE":
dtype = Date
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/unit/io/database/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@
("timestamp(5)", pl.Datetime("us")),
("timestamp(7)", pl.Datetime("ns")),
("datetime without tz", pl.Datetime("us")),
("duration(2)", pl.Duration("ms")),
("interval", pl.Duration("us")),
("date", pl.Date),
("time", pl.Time),
("date32", pl.Date),
("time64", pl.Time),
# binary types
("BYTEA", pl.Binary),
("BLOB", pl.Binary),
# miscellaneous
("NULL", pl.Null),
],
)
def test_dtype_inference_from_string(
Expand Down

0 comments on commit 53c8ec9

Please sign in to comment.