Skip to main content

SKY-P402 · Quality, structure and performance

pandas read_csv without a row bound

Low

The catalog gives no severity. Every finding is reported LOW.

pandas.read_csv(path) builds the whole table in memory before it returns. SKY-P402 reports a call to pandas' read_csv that has neither chunksize, nrows nor iterator=True. The catalog lists the rule as "Repeated expensive operation"; this is what the code checks.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans). Advice that is measured on every --quality scan but not listed by default. It is listed, and counted toward max_quality, with --all-advice or when the rule is named in [tool.skylos.gate] rules.
Finding type
advice
Catalog name
Repeated expensive operation
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A pandas DataFrame often takes several times the size of the CSV in memory. A nightly job that worked on last year's export runs out of memory once the file grows, and fails without a useful error.

What Skylos flags

jobs/revenue.py · PythonReported on line 7
"""Nightly revenue job.""" import pandas as pd  def total_revenue(path):    orders = pd.read_csv(path)    return orders["amount"].sum()

What Skylos leaves alone

jobs/revenue.py · Python
"""Nightly revenue job.""" import pandas as pd  def total_revenue(path):    total = 0    for chunk in pd.read_csv(path, chunksize=100_000):        total += chunk["amount"].sum()    return total

Why Skylos leaves it alone

With chunksize, read_csv returns an iterator that yields 100,000 rows at a time, so memory use stays the same however large the file becomes.

How Skylos decides

  1. Runs with --quality, on Python files.
  2. Finds calls that resolve to pandas.read_csv through the file's imports (import pandas as pd, from pandas import read_csv). A read_csv method of another object, or a project module named pandas, does not count.
  3. Leaves alone a call with a positive chunksize, an nrows of zero or more, a size given as a variable, or iterator=True.
  4. Reports LOW on the line of the call. Other pandas readers, such as read_json or read_parquet, are not checked.

How to fix it

Read the file in chunks and combine the partial results, or load only the columns you need with usecols and pass chunksize.

jobs/revenue.py · Python · the fix, against the flagged file
"""Nightly revenue job.""" import pandas as pd  def total_revenue(path):Removed:     orders = pd.read_csv(path)Removed:     return orders["amount"].sum()Added:     chunks = pd.read_csv(path, usecols=["amount"], chunksize=100_000)Added:     return sum(chunk["amount"].sum() for chunk in chunks)

When it can be wrong

  • A CSV that is small by design, such as a lookup table shipped with the code, is reported.
  • usecols and dtype reduce memory but do not count as a bound, so a call that already reads only one small column is still reported.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • On the reported line:

    Python
    orders = pd.read_csv(path)  # skylos: ignore[SKY-P402] one day of orders
  • [tool.skylos] ignore = ["SKY-P402"] turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.