SKY-P402 · Quality, structure and performance
pandas read_csv without a row bound
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
--qualityscan but not listed by default. It is listed, and counted towardmax_quality, with--all-adviceor when the rule is named in[tool.skylos.gate] rules. - Finding type
- advice
- Catalog name
- Repeated expensive operation
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
"""Nightly revenue job.""" import pandas as pd def total_revenue(path): orders = pd.read_csv(path) return orders["amount"].sum()What Skylos leaves alone
"""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 totalWhy 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
- Runs with
--quality, on Python files. - Finds calls that resolve to
pandas.read_csvthrough the file's imports (import pandas as pd,from pandas import read_csv). Aread_csvmethod of another object, or a project module namedpandas, does not count. - Leaves alone a call with a positive
chunksize, annrowsof zero or more, a size given as a variable, oriterator=True. - Reports LOW on the line of the call. Other pandas readers, such as
read_jsonorread_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.
"""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.
usecolsanddtypereduce 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.