sspipe/sspipe

`TypeError` on conditional

sivabudh opened this issue · 3 comments

Is there a way to make the following working Pandas code more concise?

value = data | px[px.SOME_COLUMN == 10] # data: DataFrame
if not value.empty:
    print(value.ANOTHER_COLUMN.astype(str).values[0])

I tried this:

data | px[px.SOME_COLUMN == 40] | (px if not px.empty else exit()) | px.ANOTHER_COLUMN.astype(str).values[0] | p(print)

but I got a TypeError:

Traceback (most recent call last):
  File "/test_pandas.py", line 25, in <module>
    data | px[px.SOME_COLUMN == 40] | (px if not px.empty else exit()) | px.ANOTHER_COLUMN.astype(str).values[0] | p(print)
TypeError: 'Pipe' object cannot be interpreted as an integer

Essentially, I'm trying not to print if the DataFrame is empty.

Using px in ... if ...px... else ... is not supported. This error is because of the order python evaluates ifelse expressionss. The px.empty is evaluated before data | px[px.SOME_COLUMN == 40] is passed to it.

Alternatively, you can use p:

data | px[px.SOME_COLUMN == 40] | p(lambda x: x if not x.empty else exit()) | px.ANOTHER_COLUMN.astype(str).values[0] | p(print)
data | px[px.SOME_COLUMN == 40] | p(lambda x: x if not x.empty else exit()) | px.ANOTHER_COLUMN.astype(str).values[0] | p(print)

@mhsekhavat , exit() shuts down the Jupyter kernel in VScode, I believe it would be better to use None in this concise syntax, what do you think?

import pandas as pd
df = pd.DataFrame({'col1': [1,2], 'col2': [3,4]})
print(df)

# Conventional syntax
dfc = df[df.col1==2]
if not dfc.empty:
    print(dfc.col2.astype("str").values[0])

# sspipe syntax
from sspipe import p, px
( df | px[px.col1==2] 
     | p( lambda x: print(x.col2.astype("str").values[0])
                    if not x.empty 
                    else None ) )

@GitHunter0 you're right. Returning None makes more sense.