pandas-import

Try in Playground
python-pandasBest PracticeInformational

0

pandas

No CWE or CVE

When importing the pandas library, make sure you import it as follow:

1import pandas as pd

Is ensures all imports are consistent in your codebase.

Ast Rule: import


pandas-import

How to write a rule
function visit(node, filename, code) {
  if (node.astType === "importstatement") {
    node.packages.forEach(statement => {
      if (statement.name && statement.name.value === "pandas") {
        if (!statement.as) {
          const error = buildError(statement.start.line, statement.start.col, statement.end.line, statement.end.col,
            "Must import as pd", "INFO", "BEST_PRACTICE");
          const edit = buildEditAdd(statement.end.line, statement.end.col, " as pd");
          const fix = buildFix("import as pd", [edit]);
          addError(error.addFix(fix));
        }

        if (statement.as && statement.as.value !== "pd") {
          const error = buildError(statement.start.line, statement.start.col, statement.end.line, statement.end.col,
            "Must import as pd", "INFO", "BEST_PRACTICE");
          const edit = buildEditUpdate(statement.as.start.line, statement.as.start.col,
            statement.as.end.line, statement.as.end.col, "pd");
          const fix = buildFix(`import as pd`, [edit]);
          addError(error.addFix(fix));
        }
      }
    })

  }
}

correct.py

Expected test result: no error

import pandas as pd

error1.py

Expected test result: no error

import pandas as foo

error2.py

Expected test result: no error

import pandas
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
Legal
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.