I'm using sympy to create an expression that is then displayed as latex with sympy.init_printing()
. The expression is used in calculations after being lambdified to a function named f
.
However, when using an array or a Series
object as an argument to f
I get a TypeError, if the lambdified expression contains a sympy object (e.g. sympy.sqrt()
). If I had used **.5
instead of the sqrt
I would get no error (but it wouldn't display the root in IPython).
Question:
How can I use arrays or Series
on a function I created through sympy.lambdify()
?
The following code is a (simplified) demo of the problem:
import sympy
import numpy
sympy.init_printing()
x = sympy.symbols('x')
_f = lambda x: sympy.sqrt(x)
f = sympy.lambdify(x, _f(x), (sympy, numpy))
f(x)
This results in a pretty root:
Then, trying to use
import pandas
df = pandas.DataFrame([1,2,3], columns=['a'])
f(df['a'])
I get:
TypeError Traceback (most recent call last)
/home/gold/venvs/venv_python3.5/lib/python3.5/site-packages/sympy/core/cache.py in wrapper(*args, **kwargs)
92 try:
---> 93 retval = cfunc(*args, **kwargs)
94 except TypeError:
/home/gold/venvs/venv_python3.5/lib/python3.5/site-packages/pandas/core/generic.py in __hash__(self)
830 raise TypeError('{0!r} objects are mutable, thus they cannot be'
--> 831 ' hashed'.format(self.__class__.__name__))
832
TypeError: 'Series' objects are mutable, thus they cannot be hashed
During handling of the above exception, another exception occurred:
SympifyError Traceback (most recent call last)
<ipython-input-25-d0ba59fbbc02> in <module>()
2 df = pandas.DataFrame([1,2,3], columns=['a'])
3
----> 4 f(df['a'])
/home/gold/venvs/venv_python3.5/lib/python3.5/site-packages/sympy/__init__.py in <lambda>(_Dummy_21)
/home/gold/venvs/venv_python3.5/lib/python3.5/site-packages/sympy/functions/elementary/miscellaneous.py in sqrt(arg)
113 """
114 # arg = sympify(arg) is handled by Pow
--> 115 return Pow(arg, S.Half)
116
117
/home/gold/venvs/venv_python3.5/lib/python3.5/site-packages/sympy/core/cache.py in wrapper(*args, **kwargs)
93 retval = cfunc(*args, **kwargs)
94 except TypeError:
---> 95 retval = func(*args, **kwargs)
96 return retval
97
/home/gold/venvs/venv_python3.5/lib/python3.5/site-packages/sympy/core/power.py in __new__(cls, b, e, evaluate)
168 from sympy.functions.elementary.exponential import exp_polar
169
--> 170 b = _sympify(b)
171 e = _sympify(e)
172 if evaluate:
/home/gold/venvs/venv_python3.5/lib/python3.5/site-packages/sympy/core/sympify.py in _sympify(a)
353
354 """
--> 355 return sympify(a, strict=True)
356
357
/home/gold/venvs/venv_python3.5/lib/python3.5/site-packages/sympy/core/sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
275
276 if strict:
--> 277 raise SympifyError(a)
278
279 if iterable(a):
SympifyError: SympifyError: 0 1
1 2
2 3
Name: a, dtype: int64
With
"numpy"
, this works:but the sympy substitute does not:
I haven't studied the
lambdify
docs enough to know if I can make both work in one function or not.handles the
sympy
but not thenumpy
arguments.It looks like not specifying
modules
should be the same asmodules = ["math", "mpmath", "sympy", "numpy"]
.An expression with operators works nicely, even combining symbols and arrays:
I probably haven't discovered anything that you haven't already.