|
16 | 16 | ) |
17 | 17 | from polars._dependencies import numpy as np |
18 | 18 | from polars._utils.wrap import wrap_expr |
19 | | -from polars.datatypes import BaseExtension, Date, Datetime, Duration |
| 19 | +from polars.datatypes import BaseExtension, Date, Datetime, Duration, Object |
20 | 20 | from polars.datatypes.convert import DataTypeMappings |
21 | 21 |
|
22 | 22 | with contextlib.suppress(ImportError): # Module not available when building docs |
23 | 23 | import polars._plr as plr |
24 | 24 |
|
25 | | - |
26 | 25 | if TYPE_CHECKING: |
27 | 26 | from polars import Expr |
28 | 27 | from polars._typing import PolarsDataType, TimeUnit |
@@ -84,7 +83,11 @@ def lit( |
84 | 83 | elif isinstance(dtype, type) and issubclass(dtype, BaseExtension): |
85 | 84 | msg = f"dtype '{dtype}' is a BaseExtension class, it should be an instance" |
86 | 85 | raise TypeError(msg) |
87 | | - elif isinstance(value, datetime): |
| 86 | + elif dtype == Object: |
| 87 | + value_s = pl.Series("literal", [value], dtype=dtype) |
| 88 | + return wrap_expr(plr.lit(value_s._s, allow_object, is_scalar=True)) |
| 89 | + |
| 90 | + if isinstance(value, datetime): |
88 | 91 | if dtype == Date: |
89 | 92 | return wrap_expr(plr.lit(value.date(), allow_object=False, is_scalar=True)) |
90 | 93 |
|
@@ -185,11 +188,7 @@ def lit( |
185 | 188 | return lit(value.value, dtype=dtype) |
186 | 189 |
|
187 | 190 | if dtype: |
188 | | - value_s = ( |
189 | | - pl.Series("literal", [value], dtype=dtype) |
190 | | - if dtype.is_object() |
191 | | - else pl.Series("literal", [value]).cast(dtype) |
192 | | - ) |
| 191 | + value_s = pl.Series("literal", [value]).cast(dtype) |
193 | 192 | return wrap_expr(plr.lit(value_s._s, allow_object, is_scalar=True)) |
194 | 193 |
|
195 | 194 | if _check_for_numpy(value) and isinstance(value, np.generic): |
|
0 commit comments