Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

pandas - Calling python functions from *.py files from java and passing and returning

I want to call Python functions from *.py file from Java code and send arguments and receive return values from it transparently without requiring to modify given *.py file. I realised that Jython PythonInterpreter is the best option for this in comparison to py4j, java2python, ScriptEngine, JPype.

I am successfully able to pass / retrieve list with Jython PythonInterpreter. But not map yet. (I created stackoverflow question explaining exception I am getting while passing map argument and retrieving map return value.)

I just tried passing and retrieving panda series (I am to try panda data frame next) as follows:

py1.py

import numpy as np1
import pandas as pd

def getSeries():
    s = pd.Series(np1.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
    return s

JythonTest.groovy

import org.python.util.PythonInterpreter;
import org.python.core.*;
import org.python.util.*;

class JythonTest 
{
    static main(def args) 
    {
        PythonInterpreter() pi = new PythonInterpreter()
        pi.exec("from py1 import *")                       
        PyFunction pf1 = (PyFunction)pi.get("getSeries")
        PyObject obj = pf1.__call__()
        println obj
    }
}

It gave following error:

Exception in thread "main" Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "__pyclasspath__/py1.py", line 1, in <module>
ImportError: No module named numpy

After googling I realized that Jython does not support pandas and numpy. Also there are other webpages online which say the same. However they all are a bit old.

Q. I want to know if it is indeed impossible to send/receive Panda DataFrame to Jython or not?

Q. If it is not possible, is there any better options available?

I will also like to know why my sending / receiving map did not work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...