Skip to content

Commit e84cb98

Browse files
authored
Merge branch 'main' into compliant-group-by
2 parents 810fa20 + 319c9d1 commit e84cb98

File tree

16 files changed

+81
-1
lines changed

16 files changed

+81
-1
lines changed

docs/api-reference/dtypes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- Unknown
3737
- UnsignedIntegerType
3838
- Time
39+
- Binary
3940
show_root_heading: false
4041
show_source: false
4142
show_bases: false

narwhals/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from narwhals.dataframe import DataFrame
99
from narwhals.dataframe import LazyFrame
1010
from narwhals.dtypes import Array
11+
from narwhals.dtypes import Binary
1112
from narwhals.dtypes import Boolean
1213
from narwhals.dtypes import Categorical
1314
from narwhals.dtypes import Date
@@ -85,6 +86,7 @@
8586

8687
__all__ = [
8788
"Array",
89+
"Binary",
8890
"Boolean",
8991
"Categorical",
9092
"DataFrame",

narwhals/_arrow/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def native_to_narwhals_dtype(dtype: pa.DataType, version: Version) -> DType:
156156
return dtypes.Decimal()
157157
if pa.types.is_time32(dtype) or pa.types.is_time64(dtype):
158158
return dtypes.Time()
159+
if pa.types.is_binary(dtype):
160+
return dtypes.Binary()
159161
return dtypes.Unknown() # pragma: no cover
160162

161163

@@ -211,6 +213,8 @@ def narwhals_to_native_dtype(dtype: DType | type[DType], version: Version) -> pa
211213
return pa.list_(inner, list_size=list_size)
212214
if isinstance_or_issubclass(dtype, dtypes.Time):
213215
return pa.time64("ns")
216+
if isinstance_or_issubclass(dtype, dtypes.Binary):
217+
return pa.binary()
214218

215219
msg = f"Unknown dtype: {dtype}" # pragma: no cover
216220
raise AssertionError(msg)

narwhals/_dask/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def narwhals_to_native_dtype(dtype: DType | type[DType], version: Version) -> An
147147
if isinstance_or_issubclass(dtype, dtypes.Time): # pragma: no cover
148148
msg = "Converting to Time dtype is not supported yet"
149149
return NotImplementedError(msg)
150+
if isinstance_or_issubclass(dtype, dtypes.Binary): # pragma: no cover
151+
msg = "Converting to Binary dtype is not supported yet"
152+
return NotImplementedError(msg)
150153

151154
msg = f"Unknown dtype: {dtype}" # pragma: no cover
152155
raise AssertionError(msg)

narwhals/_duckdb/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ def native_to_narwhals_dtype(duckdb_dtype: str, version: Version) -> DType:
116116
return dtypes.Decimal()
117117
if duckdb_dtype == "TIME":
118118
return dtypes.Time()
119+
if duckdb_dtype == "BLOB":
120+
return dtypes.Binary()
119121
return dtypes.Unknown() # pragma: no cover
120122

121123

@@ -154,6 +156,8 @@ def narwhals_to_native_dtype(dtype: DType | type[DType], version: Version) -> st
154156
return "BOOLEAN"
155157
if isinstance_or_issubclass(dtype, dtypes.Time):
156158
return "TIME"
159+
if isinstance_or_issubclass(dtype, dtypes.Binary):
160+
return "BLOB"
157161
if isinstance_or_issubclass(dtype, dtypes.Categorical):
158162
msg = "Categorical not supported by DuckDB"
159163
raise NotImplementedError(msg)

narwhals/_ibis/dataframe.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def native_to_narwhals_dtype(ibis_dtype: Any, version: Version) -> DType:
7171
return dtypes.Decimal()
7272
if ibis_dtype.is_time():
7373
return dtypes.Time()
74+
if ibis_dtype.is_binary():
75+
return dtypes.Binary()
7476
return dtypes.Unknown() # pragma: no cover
7577

7678

narwhals/_pandas_like/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ def non_object_native_to_narwhals_dtype(dtype: str, version: Version) -> DType:
394394
return dtypes.Decimal()
395395
if dtype.startswith("time") and dtype.endswith("[pyarrow]"):
396396
return dtypes.Time()
397+
if dtype.startswith("binary") and dtype.endswith("[pyarrow]"):
398+
return dtypes.Binary()
397399
return dtypes.Unknown() # pragma: no cover
398400

399401

@@ -593,7 +595,7 @@ def narwhals_to_native_dtype( # noqa: PLR0915
593595
msg = "Converting to Enum is not (yet) supported"
594596
raise NotImplementedError(msg)
595597
if isinstance_or_issubclass(
596-
dtype, (dtypes.Struct, dtypes.Array, dtypes.List, dtypes.Time)
598+
dtype, (dtypes.Struct, dtypes.Array, dtypes.List, dtypes.Time, dtypes.Binary)
597599
):
598600
if implementation is Implementation.PANDAS and backend_version >= (2, 2):
599601
try:

narwhals/_polars/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ def native_to_narwhals_dtype(
147147
return dtypes.Decimal()
148148
if dtype == pl.Time:
149149
return dtypes.Time()
150+
if dtype == pl.Binary:
151+
return dtypes.Binary()
150152
return dtypes.Unknown()
151153

152154

@@ -192,6 +194,8 @@ def narwhals_to_native_dtype(
192194
return pl.Date()
193195
if dtype == dtypes.Time:
194196
return pl.Time()
197+
if dtype == dtypes.Binary:
198+
return pl.Binary()
195199
if dtype == dtypes.Decimal:
196200
msg = "Casting to Decimal is not supported yet."
197201
raise NotImplementedError(msg)

narwhals/_spark_like/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def native_to_narwhals_dtype(
7979
for field in dtype
8080
]
8181
)
82+
if isinstance(dtype, native.BinaryType):
83+
return dtypes.Binary()
8284
return dtypes.Unknown()
8385

8486

@@ -135,6 +137,8 @@ def narwhals_to_native_dtype(
135137
for field in dtype.fields
136138
]
137139
)
140+
if isinstance_or_issubclass(dtype, dtypes.Binary):
141+
return native.BinaryType()
138142

139143
if isinstance_or_issubclass(
140144
dtype,

narwhals/dtypes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,27 @@ class Time(TemporalType):
696696
>>> nw.from_native(rel).schema["t"]
697697
Time
698698
"""
699+
700+
701+
class Binary(DType):
702+
"""Binary type.
703+
704+
Examples:
705+
>>> import polars as pl
706+
>>> import narwhals as nw
707+
>>> import pyarrow as pa
708+
>>> import duckdb
709+
>>> data = [b"test1", b"test2"]
710+
>>> ser_pl = pl.Series(data, dtype=pl.Binary)
711+
>>> ser_pa = pa.chunked_array([pa.array(data, type=pa.binary())])
712+
>>> rel = duckdb.sql(
713+
... "SELECT * FROM (VALUES (BLOB 'test1'), (BLOB 'test2')) AS df(t)"
714+
... )
715+
716+
>>> nw.from_native(ser_pl, series_only=True).dtype
717+
Binary
718+
>>> nw.from_native(ser_pa, series_only=True).dtype
719+
Binary
720+
>>> nw.from_native(rel).schema["t"]
721+
Binary
722+
"""

0 commit comments

Comments
 (0)