Skip to content

Commit

Permalink
Fix init of DataFrame with empty dataset (eg:"[]") and column/schema …
Browse files Browse the repository at this point in the history
…typedefs (#3353)
  • Loading branch information
alexander-beedie committed May 10, 2022
1 parent 96cc3d9 commit 575bc4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions py-polars/polars/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def sequence_to_pydf(
data_series: List["PySeries"]

if len(data) == 0:
data_series = []
return dict_to_pydf({}, columns=columns)

elif isinstance(data[0], pli.Series):
series_names = [s.name for s in data]
Expand Down Expand Up @@ -506,7 +506,7 @@ def sequence_to_pydf(
columns, dtypes = _unpack_columns(columns, n_expected=1)
data_series = [pli.Series(columns[0], data, dtypes.get(columns[0])).inner()]

data_series = _handle_columns_arg(data_series, columns=columns) # type: ignore[arg-type]
data_series = _handle_columns_arg(data_series, columns=columns)
return PyDataFrame(data_series)


Expand Down
33 changes: 17 additions & 16 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# flake8: noqa: W191,E101
import io
import sys
import typing
from builtins import range
Expand All @@ -15,8 +14,6 @@

import polars as pl
from polars import testing
from polars.datatypes import List
from polars.internals.frame import DataFrame


def test_version() -> None:
Expand All @@ -36,19 +33,23 @@ def test_init_only_columns() -> None:
assert df.frame_equal(truth, null_equal=True)
assert df.dtypes == [pl.Float32, pl.Float32, pl.Float32]

df = pl.DataFrame(
columns=[("a", pl.Date), ("b", pl.UInt64), ("c", pl.datatypes.Int8)]
)
truth = pl.DataFrame({"a": [], "b": [], "c": []}).with_columns(
[
pl.col("a").cast(pl.Date),
pl.col("b").cast(pl.UInt64),
pl.col("c").cast(pl.Int8),
]
)
assert df.shape == (0, 3)
assert df.frame_equal(truth, null_equal=True)
assert df.dtypes == [pl.Date, pl.UInt64, pl.Int8]
# Validate construction with various flavours of no/empty data
no_data: Any
for no_data in (None, {}, []):
df = pl.DataFrame(
data=no_data,
columns=[("a", pl.Date), ("b", pl.UInt64), ("c", pl.datatypes.Int8)],
)
truth = pl.DataFrame({"a": [], "b": [], "c": []}).with_columns(
[
pl.col("a").cast(pl.Date),
pl.col("b").cast(pl.UInt64),
pl.col("c").cast(pl.Int8),
]
)
assert df.shape == (0, 3)
assert df.frame_equal(truth, null_equal=True)
assert df.dtypes == [pl.Date, pl.UInt64, pl.Int8]


def test_init_dict() -> None:
Expand Down

0 comments on commit 575bc4c

Please sign in to comment.