pola-rs/polars · error · NoDataError
empty data from
Error message
empty data from {context}{hint} What it means
Polars' _check_empty raises NoDataError when the bytes read from the source are empty. For StringIO/BytesIO inputs whose read position is not 0, it appends a hint suggesting seek(0), because the buffer looks empty only because it was already consumed.
Solutions
- Call buffer.seek(0) before passing it to polars.
- Verify the producer actually wrote data (check buffer.getbuffer().nbytes or file size).
- Create a fresh buffer from the raw bytes for each read call.
- If the empty source is expected, guard upstream and skip the read.
Example fix
// before buf = io.BytesIO(data) other = pl.read_csv(buf) # position already at end // after buf = io.BytesIO(data) buf.seek(0) other = pl.read_csv(buf)
Defensive patterns
Strategy: validation
Validate before calling
if isinstance(src, (io.BytesIO, io.StringIO)):
pos = src.tell()
src.seek(0, io.SEEK_END)
if src.tell() == 0:
raise RuntimeError('source buffer is empty')
src.seek(pos) Type guard
def has_data(buf: io.BytesIO) -> bool:
pos = buf.tell()
buf.seek(0, io.SEEK_END)
size = buf.tell()
buf.seek(pos)
return size > 0 Try / catch
try:
df = pl.read_csv(buf)
except pl.exceptions.NoDataError as e:
buf.seek(0)
df = pl.read_csv(buf) # rewind once and retry Prevention
- Always seek(0) on in-memory buffers right before handing them to polars.
- Never reuse a consumed buffer; construct a fresh one from the raw bytes.
- Assert file size / buffer length > 0 in tests that build inputs programmatically.
When it happens
Trigger: Passing an in-memory buffer to pl.read_csv/read_json etc. after it was already read (position > 0), or genuinely passing an empty file/buffer/empty downloaded response.
Common situations: Reusing a BytesIO across two read calls without rewinding; reading a 0-byte file from disk; an HTTP request returning an empty body; a writer that never flushed to the buffer.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- `columns` arg should only have unique values, got
- could not read from input
- Expecting to be able to downcast into bytes from read…
- `fsspec` is required for `storage_options` argument
- the `columns` argument should contain a list of all…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/7a748f8ace325190.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/io/_utils.py:310
BytesIO(f.read().encode("utf8")),
context=f"{file!r}",
raise_if_empty=raise_if_empty,
)
return managed_file(file)
def _check_empty(
b: BytesIO, *, context: str, raise_if_empty: bool, read_position: int | None = None
) -> BytesIO:
if raise_if_empty and b.getbuffer().nbytes == 0:
hint = (
f" (buffer position = {read_position}; try seek(0) before reading?)"
if context in ("StringIO", "BytesIO") and read_position
else ""
)
msg = f"empty data from {context}{hint}"
raise NoDataError(msg)
return b
def looks_like_url(path: str) -> bool:
return re.match(r"^(ht|f)tps?://", path, re.IGNORECASE) is not None
def process_file_url(path: str, encoding: str | None = None) -> BytesIO:
from urllib.request import urlopen
with urlopen(path) as f:
if not encoding or encoding in {"utf8", "utf8-lossy"}:
return BytesIO(f.read())
else:
return BytesIO(f.read().decode(encoding).encode("utf8"))
def is_glob_pattern(file: str) -> bool:View on GitHub (pinned to fe841f959e)