pwwang / datar

A Grammar of Data Manipulation in python

Home Page:https://pwwang.github.io/datar/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] if_else() with logical operators 'and' / 'or' produces incorrect results

GitHunter0 opened this issue · comments

datar version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of datar and its backends.

Issue Description

import pandas as pd
import datar.all as d
from datar.all import f
df = pd.DataFrame({'x': [0, 1, 2], 'y': [1,2,3]})
(
    df
    >> d.mutate(
        and_issue = d.if_else(
            f.x > 0 and 2+2==4,
            'x greater than zero',
            'x smaller or equal to zero'
        ),
        or_issue = d.if_else(
            f.x > 0 or 2+2==4,
            'True (should be always True)',
            'False (should be True)'
        ),            
    )
)
#>         x       y            and_issue                      or_issue
  <int64> <int64>             <object>                      <object>
0       0       1  x greater than zero        False (should be True)
1       1       2  x greater than zero  True (should be always True)
2       2       3  x greater than zero  True (should be always True)

Expected Behavior

The first row of and_issue should be x smaller or equal to zero, and all rows of or_issue should be True.

Installed Versions

python : 3.10.8 | packaged by conda-forge | (main, Nov 24 2022, 14:07:00) [MSC v.1916 64 bit (AMD64)]
datar : 0.11.1
simplug : 0.2.2
executing : 1.2.0
pipda : 0.11.0
datar-numpy : 0.1.0
numpy : 1.23.5
datar-pandas: 0.2.0
pandas : 1.5.2

I forgot that, the trick is to use &, | and parenthesis like: (f.x > 0) & (2+2==4) and (f.x > 0) | (2+2==4).