0

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.

6
  • stackoverflow.com/questions/36213908/… Commented Sep 13, 2016 at 12:37
  • stackoverflow.com/questions/19455100/… Commented Sep 13, 2016 at 12:37
  • (It's still on the wishlist for JyNi, so I think the answer to the question is "you can't") Commented Sep 13, 2016 at 12:38
  • ohhh thanks but then what next best option can you suggest for my requirements: (1) 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. (2) use pandas and numpy and other standard/widely used libraries as transparently as possible Commented Sep 14, 2016 at 12:45
  • Can you write a second .py file that calls the first .py file, receives the DataFrame, and serializes it to JSON? Commented Sep 8, 2021 at 21:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.