Skip to content

Commit 29c796a

Browse files
authored
fix(memtable): ensure that constructing an empty memtable from a dataframe works (#10945)
Closes #10940.
1 parent 3eb76f8 commit 29c796a

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

ibis/backends/tests/test_generic.py

+15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
np = pytest.importorskip("numpy")
4141
pd = pytest.importorskip("pandas")
4242
tm = pytest.importorskip("pandas.testing")
43+
pa = pytest.importorskip("pyarrow")
4344

4445
NULL_BACKEND_TYPES = {
4546
"bigquery": "NULL",
@@ -2516,3 +2517,17 @@ def test_table_describe_with_multiple_decimal_columns(con):
25162517
expr = t.describe()
25172518
result = con.to_pyarrow(expr)
25182519
assert len(result) == 2
2520+
2521+
2522+
@pytest.mark.parametrize(
2523+
"input",
2524+
[[], pa.table([[]], pa.schema({"x": pa.int64()}))],
2525+
ids=["list", "pyarrow-table"],
2526+
)
2527+
@pytest.mark.notyet(["druid"], raises=PyDruidProgrammingError)
2528+
@pytest.mark.notyet(
2529+
["flink"], raises=ValueError, reason="flink doesn't support empty tables"
2530+
)
2531+
def test_empty_memtable(con, input):
2532+
t = ibis.memtable(input, schema={"x": "int64"})
2533+
assert not len(con.to_pyarrow(t))

ibis/expr/api.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -429,15 +429,24 @@ def _memtable(
429429
schema: SchemaLike | None = None,
430430
name: str | None = None,
431431
) -> Table:
432+
import ibis
433+
432434
if hasattr(data, "__arrow_c_stream__"):
433435
# Support objects exposing arrow's PyCapsule interface
434436
import pyarrow as pa
435437

436-
data = pa.table(data)
438+
data = pa.table(
439+
data,
440+
schema=ibis.schema(schema).to_pyarrow() if schema is not None else None,
441+
)
437442
else:
438443
import pandas as pd
439444

440-
data = pd.DataFrame(data, columns=columns)
445+
data = pd.DataFrame(
446+
data,
447+
columns=columns
448+
or (ibis.schema(schema).names if schema is not None else None),
449+
)
441450
return _memtable(data, columns=columns, schema=schema, name=name)
442451

443452

0 commit comments

Comments
 (0)