Skip to content

Commit 416eeeb

Browse files
authored
Update rule metadata (#1601)
1 parent b29355f commit 416eeeb

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S6742.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<p>This rule raises an issue when 5 or more commands are applied on a data frame.</p>
1+
<p>This rule raises an issue when 7 or more commands are applied on a data frame.</p>
22
<h2>Why is this an issue?</h2>
33
<p>The pandas library provides many ways to filter, select, reshape and modify a data frame. Pandas supports as well method chaining, which means that
44
many <code>DataFrame</code> methods return a modified <code>DataFrame</code>. This allows the user to chain multiple operations together, making it
55
effortless perform several of them in one line of code:</p>
66
<pre>
77
import pandas as pd
88

9-
joe = pd.read_csv("data.csv", dtype={'user_id':'str', 'name':'str'}).set_index("name").filter(like='jo', axis=0).head()
9+
schema = {'name':str, 'domain': str, 'revenue': 'Int64'}
10+
joe = pd.read_csv("data.csv", dtype=schema).set_index('name').filter(like='joe', axis=0).groupby('domain').mean().round().sample()
1011
</pre>
1112
<p>While this code is correct and concise, it can be challenging to follow its logic and flow, making it harder to debug or modify in the future.</p>
1213
<p>To improve code readability, debugging, and maintainability, it is recommended to break down long chains of pandas instructions into smaller, more
@@ -21,20 +22,20 @@ <h4>Noncompliant code example</h4>
2122
import pandas as pd
2223

2324
def foo(df: pd.DataFrame):
24-
return df.set_index("name").filter(like='joe', axis=0).groupby("team")["salary"].mean().head() # Noncompliant: too many operations happen on this data frame.
25+
return df.set_index('name').filter(like='joe', axis=0).groupby('team').mean().round().sort_values('salary').take([0]) # Noncompliant: too many operations happen on this data frame.
2526
</pre>
2627
<h4>Compliant solution</h4>
2728
<pre data-diff-id="1" data-diff-type="compliant">
2829
import pandas as pd
2930

3031
def select_joes(df):
31-
return df.set_index("name").filter(like='joe', axis=0)
32+
return df.set_index('name').filter(like='joe', axis=0)
3233

3334
def compute_mean_salary_per_team(df):
34-
return df.groupby("team")["salary"].mean()
35+
return df.groupby('team').mean().round()
3536

3637
def foo(df: pd.DataFrame):
37-
return df.pipe(select_joes).pipe(compute_mean_salary_per_team).head() # Compliant
38+
return df.pipe(select_joes).pipe(compute_mean_salary_per_team).sort_values('salary').take([0]) # Compliant
3839
</pre>
3940
<h2>Resources</h2>
4041
<h3>Documentation</h3>

sonarpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"languages": [
44
"PY"
55
],
6-
"latest-update": "2023-10-06T11:02:04.798788Z",
6+
"latest-update": "2023-10-09T13:33:42.838821515Z",
77
"options": {
88
"no-language-in-filenames": true,
99
"preserve-filenames": true

0 commit comments

Comments
 (0)