Home > other >  can someone explain why i'm getting an error on a sympy.Set()
can someone explain why i'm getting an error on a sympy.Set()

Time:12-07

works:

    import sympy as sp
    import pandas as pd
    
    test = pd.DataFrame( {'greeting':['hey','hi','hello','howdy']} )
    test1['greeting'].map(lambda x: sp.Set(*x).is_proper_subset( sp.Set(*x) ))

doesn't work:

    import sympy as sp
    import pandas as pd
    
    test = pd.DataFrame( {'greeting':['hey','hi','hello','howdy']} )
    test1['greeting'].map(lambda x: sp.Set('hi',).is_proper_subset( sp.Set(*x) ))```

i get:

AttributeError: 'str' object has no attribute 'args'

i've also tried with numbers on another dataframe:

test['triplets'].map(lambda x: sp.Set(*(813,)).is_proper_subset( sp.Set(*x) ))

and i get the same result.

https://docs.sympy.org/latest/modules/sets.html

class sympy.sets.sets.Set(*args)[source]¶
The base class for any kind of set.

ok...why is it working for each Series value when i pass it through lambda, but not when i write it manually

note: my end goal is to have this inside a for loop where the the iteration is passed into where i have 'hi'

CodePudding user response:

From sympy's Set documentation:

class sympy.sets.sets.Set(*args)[source]¶ This is not meant to be used directly as a container of items. It does not behave like the builtin set; see FiniteSet for that.

The main problem is that is_proper_subset is meant for Interval type of "sets". It doesn't seem to handle simpler sets well, but gives a rather cryptic error message. When debugging, it often helps to reduce the problem as much as possible:


import sympy as sp

sp.Set(1, 2).is_proper_subset(sp.Set(1, 2, 3))

This raises the error AttributeError: 'int' object has no attribute 'args'.

Similarly:

sp.Set('a', 'b').is_proper_subset(sp.Set('a', 'b', 'c'))

Leads to AttributeError: 'str' object has no attribute 'args'.

The best solution to the original problem, is to use standard python functions.

CodePudding user response:

i don't know what the deal is, i'm hoping there's an answer - but until then, i've resorted to this:

test1['greeting'].map(lambda x: set('hi').issubset(set(x)) and set('hi')!=set(x))

  • Related