I am using python 3.X. I am trying to use the eval()dataframe method including custom functions like this
import pandas as pd
import numpy as np
df = pd.DataFrame({
'T': [0, 10, 0, 10, 10, 30],
'P': [0, 0, 1000, 1000, 0, 0],
'S': [25, 25, 25, 25, 40, 40]
})
def custom(A, B, C):
# some operations
aux = pd.DataFrame({
'A': [0, 10, 0, 10, 10, 30],
})
return aux.A # here is where I want to return the numpy array or list, or dataframe column
eq = 'RES = T + @custom(S, T, P) + 2'
df.eval(eq, engine='numexpr', inplace=True)
But I can only return one float or integer value in the function, a simple value.
So I would like to return a numpy array or a list of values because I want to use the result to operate with the rest of the equation variables. I get this error:
TypeError: unhashable type: 'numpy.ndarray'
Another example:
import pandas as pd
import numpy as np
import seawater as sw
from seawater.library import T90conv
df = pd.DataFrame({
'T': T90conv([0, 10, 0, 10, 10, 30]),
'P': [0, 0, 1000, 1000, 0, 0],
'S': [25, 25, 25, 25, 40, 40]
})
cndr = sw.cndr # it returns a numpy array
eq = 'NEW = @cndr(S, T, P)'
df.eval(eq, engine='numexpr', inplace=True)
Is that possible? What kind of types can I return? Is there another way to achieve this?
sw.cndr(df['T'], df['P'], df['S'])instead of using the dot notation @maxunumexpr. Usingengine='python'- it works properly...