Skip to content

Commit

Permalink
feat(python): improved support for use of file-like objects with `Dat…
Browse files Browse the repository at this point in the history
…aFrame` "write" methods (#12113)
  • Loading branch information
alexander-beedie authored Oct 30, 2023
1 parent baf8086 commit 1c34710
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
24 changes: 12 additions & 12 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2358,8 +2358,8 @@ def write_json(
Parameters
----------
file
File path to which the result should be written. If set to `None`
(default), the output is returned as a string instead.
File path or writeable file-like object to which the result will be written.
If set to `None` (default), the output is returned as a string instead.
pretty
Pretty serialize json.
row_oriented
Expand Down Expand Up @@ -2415,8 +2415,8 @@ def write_ndjson(self, file: IOBase | str | Path | None = None) -> str | None:
Parameters
----------
file
File path to which the result should be written. If set to `None`
(default), the output is returned as a string instead.
File path or writeable file-like object to which the result will be written.
If set to `None` (default), the output is returned as a string instead.
Examples
--------
Expand Down Expand Up @@ -2508,8 +2508,8 @@ def write_csv(
Parameters
----------
file
File path to which the result should be written. If set to `None`
(default), the output is returned as a string instead.
File path or writeable file-like object to which the result will be written.
If set to `None` (default), the output is returned as a string instead.
has_header
Whether to include header in the CSV output.
separator
Expand Down Expand Up @@ -2616,7 +2616,7 @@ def write_avro(
Parameters
----------
file
File path to which the file should be written.
File path or writeable file-like object to which the data will be written.
compression : {'uncompressed', 'snappy', 'deflate'}
Compression method. Defaults to "uncompressed".
Expand Down Expand Up @@ -3178,8 +3178,8 @@ def write_ipc(
Parameters
----------
file
Path to which the IPC data should be written. If set to
`None`, the output is returned as a BytesIO object.
Path or writeable file-like object to which the IPC data will be
written. If set to `None`, the output is returned as a BytesIO object.
compression : {'uncompressed', 'lz4', 'zstd'}
Compression method. Defaults to "uncompressed".
Expand Down Expand Up @@ -3239,8 +3239,8 @@ def write_ipc_stream(
Parameters
----------
file
Path to which the IPC record batch data should be written. If set to
`None`, the output is returned as a BytesIO object.
Path or writeable file-like object to which the IPC record batch data will
be written. If set to `None`, the output is returned as a BytesIO object.
compression : {'uncompressed', 'lz4', 'zstd'}
Compression method. Defaults to "uncompressed".
Expand Down Expand Up @@ -3288,7 +3288,7 @@ def write_parquet(
Parameters
----------
file
File path to which the file should be written.
File path or writeable file-like object to which the result will be written.
compression : {'lz4', 'uncompressed', 'snappy', 'gzip', 'lzo', 'brotli', 'zstd'}
Choose "zstd" for good compression performance.
Choose "lz4" for fast compression/decompression.
Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub fn get_either_file(py_f: PyObject, truncate: bool) -> PyResult<EitherRustPyt
};
Ok(EitherRustPythonFile::Rust(f))
} else {
let f = PyFileLikeObject::with_requirements(py_f, true, true, true)?;
let f = PyFileLikeObject::with_requirements(py_f, !truncate, truncate, !truncate)?;
Ok(EitherRustPythonFile::Py(f))
}
})
Expand Down
19 changes: 19 additions & 0 deletions py-polars/tests/unit/io/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,3 +1603,22 @@ def test_provide_schema() -> None:
"B": [None, "ragged", None],
"C": [None, None, None],
}


def test_custom_writeable_object() -> None:
df = pl.DataFrame({"a": [10, 20, 30], "b": ["x", "y", "z"]})

class CustomBuffer:
writes: list[bytes]

def __init__(self) -> None:
self.writes = []

def write(self, data: bytes) -> int:
self.writes.append(data)
return len(data)

buf = CustomBuffer()
df.write_csv(buf) # type: ignore[call-overload]

assert b"".join(buf.writes) == b"a,b\n10,x\n20,y\n30,z\n"

0 comments on commit 1c34710

Please sign in to comment.