Skip to content

Commit d7bfd95

Browse files
authored
Merge pull request #46 from pylint-dev/45-add-pandas-dataframe-column-selection-checker
Add pandas dataframe column selection checker
2 parents de9c090 + e0fbf98 commit d7bfd95

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed under the MIT: https://mit-license.org/
2+
# For details: https://github.com/pylint-dev/pylint-ml/LICENSE
3+
# Copyright (c) https://github.com/pylint-dev/pylint-ml/CONTRIBUTORS.txt
4+
5+
"""Check for using dictionary-like column selection over property-like selection in pandas DataFrames."""
6+
7+
from __future__ import annotations
8+
9+
from astroid import nodes
10+
from pylint.checkers import BaseChecker
11+
from pylint.checkers.utils import only_required_for_messages
12+
from pylint.interfaces import HIGH
13+
14+
15+
class PandasColumnSelectionChecker(BaseChecker):
16+
name = "pandas-column-selection"
17+
msgs = {
18+
"W8118": (
19+
"Use dictionary-like column selection (df['column']) instead of property-like selection (df.column).",
20+
"pandas-column-selection",
21+
"Ensure that pandas DataFrame columns are selected using dictionary-like syntax for clarity and safety.",
22+
),
23+
}
24+
25+
@only_required_for_messages("pandas-column-selection")
26+
def visit_attribute(self, node: nodes.Attribute) -> None:
27+
"""Check for attribute access that might be a column selection."""
28+
if isinstance(node.expr, nodes.Name) and node.expr.name.startswith("df_"):
29+
# Issue a warning for property-like access
30+
self.add_message("pandas-column-selection", node=node, confidence=HIGH)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import astroid
2+
import pylint.testutils
3+
from pylint.interfaces import HIGH
4+
5+
from pylint_ml.checkers.pandas.pandas_dataframe_column_selection import PandasColumnSelectionChecker
6+
7+
8+
class TestPandasColumnSelectionChecker(pylint.testutils.CheckerTestCase):
9+
CHECKER_CLASS = PandasColumnSelectionChecker
10+
11+
def test_incorrect_column_selection(self):
12+
node = astroid.extract_node(
13+
"""
14+
import pandas as pd
15+
df_sales = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
16+
value = df_sales.A # [pandas-column-selection]
17+
"""
18+
)
19+
20+
column_attribute = node.value
21+
22+
with self.assertAddsMessages(
23+
pylint.testutils.MessageTest(
24+
msg_id="pandas-column-selection",
25+
confidence=HIGH,
26+
node=column_attribute,
27+
),
28+
ignore_position=True,
29+
):
30+
self.checker.visit_attribute(column_attribute)

0 commit comments

Comments
 (0)