pivot-table

Try in Playground
python-pandasBest PracticeInformational

0

pandas

No CWE or CVE

pivot_table is prefered to pivot:

  • pivot_table is a generalization of pivot that can handle duplicate values for one pivoted index/column pair.
  • pivot_table also supports using multiple columns for the index and column of the pivoted table.

Reference

  • pivot
  • pivot_table

Ast Rule: function call


pivot-table

How to write a rule
function visit(node, filename, code) {
  const usePandas = node.context.imports.filter(i => i.astType === "importstatement" && i.packages.filter(p => p.name && p.name.value && p.name.value === "pandas").length > 0).length > 0;

  if (!usePandas) {
    return;
  }
  if (node.functionName && node.functionName.value && (node.functionName.value === "pivot" || node.functionName.value === "unstack")) {
    if (node.moduleOrObject) {
      const error = buildError(node.functionName.start.line, node.functionName.start.col, node.functionName.end.line, node.functionName.end.col,
        `do not use ${node.functionName.value}, prefer pivot_table`, "INFO", "BEST_PRACTICE");
      const edit = buildEditUpdate(node.functionName.start.line, node.functionName.start.col,
        node.functionName.end.line, node.functionName.end.col,
        "pivot_table");
      const fix = buildFix("use pivot_table", [edit]);
      addError(error.addFix(fix));
    }
  }
}

use-pivot-table.py

Expected test result: no error

import pandas as pd

stocks.pivot(index='symbol', columns='date', values='volume')
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.