Skip to content

Commit

Permalink
Fix ipc_read_schema so Path() and filename which start with "~/" work. (
Browse files Browse the repository at this point in the history
  • Loading branch information
ghuls committed May 4, 2022
1 parent 0bd1b57 commit 9a30669
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
3 changes: 3 additions & 0 deletions py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,9 @@ def read_ipc_schema(
-------
Dictionary mapping column names to datatypes
"""
if isinstance(file, (str, Path)):
file = format_path(file)

return _ipc_schema(file)


Expand Down
38 changes: 33 additions & 5 deletions py-polars/tests/io/test_ipc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# flake8: noqa: W191,E101
import io
import os
from typing import List
from pathlib import Path
from typing import List, Union

import pytest

Expand All @@ -25,14 +26,15 @@ def test_from_to_buffer(df: pl.DataFrame, compressions: List[str]) -> None:
def test_from_to_file(
io_test_dir: str, df: pl.DataFrame, compressions: List[str]
) -> None:
f = os.path.join(io_test_dir, "small.ipc")
f_ipc = os.path.join(io_test_dir, "small.ipc")

# does not yet work on windows because we hold an mmap?
if os.name != "nt":
for compression in compressions:
df.write_ipc(f, compression=compression) # type: ignore
df_read = pl.read_ipc(str(f))
assert df.frame_equal(df_read)
for f in (str(f_ipc), Path(f_ipc)):
df.write_ipc(f, compression=compression) # type: ignore
df_read = pl.read_ipc(f) # type: ignore
assert df.frame_equal(df_read)


def test_select_columns() -> None:
Expand Down Expand Up @@ -80,3 +82,29 @@ def test_ipc_schema(compressions: List[str]) -> None:
f.seek(0)

assert pl.read_ipc_schema(f) == {"a": pl.Int64, "b": pl.Utf8, "c": pl.Boolean}


def test_ipc_schema_from_file(
io_test_dir: str, df: pl.DataFrame, compressions: List[str]
) -> None:
f_ipc = os.path.join(io_test_dir, "small.ipc")

# does not yet work on windows because we hold an mmap?
if os.name != "nt":
for compression in compressions:
for f in (str(f_ipc), Path(f_ipc)):
df.write_ipc(f, compression=compression) # type: ignore
assert pl.read_ipc_schema(f) == { # type: ignore
"bools": pl.Boolean,
"bools_nulls": pl.Boolean,
"int": pl.Int64,
"int_nulls": pl.Int64,
"floats": pl.Float64,
"floats_nulls": pl.Float64,
"strings": pl.Utf8,
"strings_nulls": pl.Utf8,
"date": pl.Date,
"datetime": pl.Datetime,
"time": pl.Time,
"cat": pl.Categorical,
}

0 comments on commit 9a30669

Please sign in to comment.