Skip to content

Commit

Permalink
ignore index column in empty pandas dataframe (#2712)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 20, 2022
1 parent 71c256a commit f796322
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,14 @@ def _from_pandas(
"""
# path for table without rows that keeps datatype
if data.shape[0] == 0:
return DataFrame([pli.Series(name, data[name]) for name in data.columns])
# We do a loop and materialize the series.
# There can be series of len != null due to pandas indexes.
series = []
for name in data.columns:
col = pli.Series(name, data[name])
if len(col) == 0:
series.append(pli.Series(name, col))
return DataFrame(series)

return cls._from_pydf(
pandas_to_pydf(
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,10 @@ def test_from_empty_arrow() -> None:
df = pl.from_arrow(pa.table(pd.DataFrame({"a": [], "b": []})))
assert df.columns == ["a", "b"] # type: ignore
assert df.dtypes == [pl.Float64, pl.Float64] # type: ignore

# 2705
df1 = pd.DataFrame(columns=["b"], dtype=float)
tbl = pa.Table.from_pandas(df1)
out = pl.from_arrow(tbl)
assert out.columns == ["b"] # type: ignore
assert out.dtypes == [pl.Float64] # type: ignore

0 comments on commit f796322

Please sign in to comment.