Search code examples
pandaspylintpep8

How can I make filtering a Pandas Dataframe with a condition conform to PEP8?


It seems quite standard to filter a Pandas Dataframe with a condition like this

filter_ = (df == True)
df = df[filter_]

but according to PyCharm, this does not conform to PEP 8, and I get this message using Pylint

C0121: Comparison to True should be just 'expr' (singleton-comparison)

Is there a PEP 8 friendly way to do this?


Solution

  • Is that a case of the == operator being overloaded to change its effect? Looks like it is. If yes, then obviously the linting tools would have a hard time recognizing this and offer meaningful advice.

    Personally I am not a fan of such operator overloading that change the meaning (semantics). I would avoid them, since they change the way one should read the code and are confusing or at least ambiguous.

    Maybe I would recommend, either keeping the same ambiguous notation and disable the one specific linting error for that instruction:

    • filter_ = (df == True) # pylint: disable=singleton-comparison [doc]

    or using a more explicit notation (I am not familiar with pandas, so I do not guarantee the following code, but at least the idea should be somewhat applicable in any context, not only pandas):

    • filter_ = df.eq(True) [doc]
    • filter_ = df.bool (really not sure about this one) [doc]