avoid-inplace-true

Try in Playground
python-pandasBest PracticeInformational

0

pandas

No CWE or CVE

You should avoid using inplace=True for the following methods: fillna, replace, rename, query, drop_duplicates, reset_index.

More on the following blog post.

Ast Rule: function call


avoid-inplace-true

How to write a rule
function visit(node, filename, code) {
  FUNCTION_TO_CHECK = ["replace", "fillna", "sort_values", "query", "drop_duplicates", "reset_index"]

  console.log(node.functionName.astType);
  if (node.functionName.astType === "string" && FUNCTION_TO_CHECK.includes(node.functionName.value)) {
    const arguments = node.arguments && node.arguments.values;
    if (!arguments) {
      return;
    }
    const inplaceArguments = arguments.filter(a => a.name && a.name.value === "inplace" && a.value && a.value.value === "True");
    console.log(inplaceArguments);
    if (inplaceArguments && inplaceArguments.length > 0) {
      console.log("bla");
      const arg = inplaceArguments[0];
      const error = buildError(arg.start.line, arg.start.col, arg.end.line, arg.end.col,
        "inplace=True should not be used", "INFO", "BEST_PRACTICE");
      const edit = buildEditRemove(arg.start.line, arg.start.col, arg.end.line, arg.end.col);
      const fix = buildFix("remove argument", [edit]);
      addError(error.addFix(fix));
    }
  }
}

other-function.py

Expected test result: no error

ice_cream.foobar({"frozen": "melted"}, inplace=True)

use-inplace.py

Expected test result: has error

ice_cream.replace({"frozen": "melted"}, inplace=True)

inplace-false.py

Expected test result: no error

ice_cream.replace({"frozen": "melted"}, inplace=False)

no-inplace.py

Expected test result: no error

Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.