How to return a numpy array or list in a custom function using the eval dataframe method?

谁都会走 提交于 2019-12-11 07:30:10

问题


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?


回答1:


I couldn't make it work using numexpr engine - it always gives me TypeError: unhashable type: 'numpy.ndarray'.

I also tried to convert that numpy.ndarray into list or tuple - it still says: TypeError: unsupported expression type: <class 'tuple'> or TypeError: unhashable type: 'list'

But it does work with python engine:

In [47]: df.eval("NEW = @sw.cndr(S, T, P)", engine='python', inplace=True)

In [48]: df
Out[48]:
      P   S          T       NEW
0     0  25   0.000000  0.498008
1     0  25   9.997601  0.654990
2  1000  25   0.000000  0.506244
3  1000  25   9.997601  0.662975
4     0  40   9.997601  1.000073
5     0  40  29.992802  1.529967

What about the following workaround?

In [77]: df = df.assign(RES=sw.cndr(df['S'], df['T'], df['P'])) \
                .eval("RES = T + RES + 2", inplace=False)

In [78]: df
Out[78]:
      P   S          T        RES
0     0  25   0.000000   2.498008
1     0  25   9.997601  12.652591
2  1000  25   0.000000   2.506244
3  1000  25   9.997601  12.660576
4     0  40   9.997601  12.997674
5     0  40  29.992802  33.522769


来源:https://stackoverflow.com/questions/47163401/how-to-return-a-numpy-array-or-list-in-a-custom-function-using-the-eval-datafram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!