Skip to content

Commit 42cc93a

Browse files
committed
convert to polars if is list
1 parent 4a11db5 commit 42cc93a

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

great_tables/_formats_vals.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,17 @@ def _make_one_col_table(vals: X) -> GT:
3838
# anticipating a tuple may be too defensive
3939
vals = list(vals)
4040

41+
if isinstance(vals, list):
42+
try:
43+
import polars as pl
44+
except ImportError:
45+
pass
46+
else:
47+
vals = pl.Series(vals)
48+
4149
# TODO: remove pandas. if vals is not a SeriesLike, then we currently
4250
# convert them to a pandas Series for backwards compatibility.
51+
4352
df = to_frame(vals, name="x")
4453

4554
# Convert the list to a Pandas DataFrame and then to a GTData object

tests/test__formats_vals.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
import pytest
44
from great_tables._formats_vals import _make_one_col_table
55
from great_tables._tbl_data import to_list
6+
import sys
67

78

89
@pytest.mark.parametrize("src", [1, [1], (1,), pd.Series([1]), pl.Series([1])])
910
def test_roundtrip(src):
1011
gt = _make_one_col_table(src)
1112

1213
assert to_list(gt._tbl_data["x"]) == [1]
14+
15+
16+
def test_one_column_table_no_lib() -> None:
17+
sys.modules["pandas"] = None
18+
19+
with pytest.raises(ModuleNotFoundError):
20+
import pandas
21+
22+
vals = [1, 2, 3]
23+
24+
_make_one_col_table(vals=vals)
25+
raise
26+
27+
28+
test_one_column_table_no_lib()

0 commit comments

Comments
 (0)