|
| 1 | +This rule is specific to Python because it's related to the Pandas library, which is widely used for data manipulation and analysis in Python. |
| 2 | + |
| 3 | +Reading CSV files without explicitly specifying which columns to load leads to unnecessary data loading and increases memory and energy consumption. This guidance is specific to the use of the Pandas library in Python, but it aligns with the more general GCI74: Avoid SELECT * from table in SQL. To ensure low environmental impact and optimal performance, always use the usecols parameter in pandas.read_csv() to select only the required columns. |
| 4 | + |
| 5 | +== Non Compliant Code Example |
| 6 | + |
| 7 | +[source,python] |
| 8 | +---- |
| 9 | +file_path = 'data.csv' |
| 10 | +df = pd.read_csv(file_path) |
| 11 | +# or |
| 12 | +df = pd.read_csv('data.csv') |
| 13 | +---- |
| 14 | + |
| 15 | +In this case, **all columns** are read into memory, even if only one or two are needed. |
| 16 | + |
| 17 | +== Compliant Solution |
| 18 | + |
| 19 | +[source,python] |
| 20 | +---- |
| 21 | +file_path = 'data.csv' |
| 22 | +df = pd.read_csv(file_path, usecols=['A', 'B']) # Only read needed columns |
| 23 | +---- |
| 24 | + |
| 25 | +This ensures only the necessary data is loaded, reducing memory usage and energy consumption. |
| 26 | + |
| 27 | +== Relevance Analysis |
| 28 | + |
| 29 | +Local experiments were conducted to assess the environmental impact of reading CSV files with and without column selection. |
| 30 | + |
| 31 | +=== Configuration |
| 32 | + |
| 33 | +* Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MHz, 12 cores, 14 logical processors |
| 34 | +* RAM: 16 GB |
| 35 | +* CO₂ Emissions Measurement: https://mlco2.github.io/codecarbon/[CodeCarbon] |
| 36 | + |
| 37 | +=== Context |
| 38 | + |
| 39 | +We generated CSV files with the following row sizes: |
| 40 | +* 1,000 |
| 41 | +* 10,000 |
| 42 | +* 100,000 |
| 43 | +* 1,000,000 |
| 44 | + |
| 45 | +Each file contains 5 columns (`A`, `B`, `C`, `D`, `E`). We measured the carbon emissions required to read: |
| 46 | +* 1 column |
| 47 | +* 2 columns |
| 48 | +* 3 columns |
| 49 | +* 4 columns |
| 50 | +* all 5 columns |
| 51 | + |
| 52 | +=== Impact Analysis |
| 53 | + |
| 54 | +The graph below illustrates the emissions generated as a function of file size and number of columns read. |
| 55 | + |
| 56 | +*Carbon emissions during reading (kgCO₂eq):* |
| 57 | + |
| 58 | +image::image.png[] |
| 59 | + |
| 60 | +The results show a **increase in emissions** as more columns are read, and a strong correlation between file size and emissions. |
| 61 | + |
| 62 | +== Conclusion |
| 63 | + |
| 64 | +The rule is relevant. Explicitly specifying columns: |
| 65 | +- Reduces carbon emissions |
| 66 | +- Decreases memory and CPU usage |
| 67 | +- Improves data loading time |
| 68 | + |
| 69 | +This is especially critical when working with large datasets or in environments where sustainability and performance matter (e.g., cloud computing, machine learning pipelines). |
| 70 | + |
| 71 | +== References |
| 72 | +https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html |
| 73 | +https://medium.com/@amit25173/what-is-usecols-in-pandas-7a6a43885f4b |
| 74 | + |
0 commit comments