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.

MaxPowers
  • 5,235
  • 2
  • 44
  • 69
Mahesha999
  • 22,693
  • 29
  • 116
  • 189
  • http://stackoverflow.com/questions/36213908/how-can-i-import-pandas-with-jython – tim_yates Sep 13 '16 at 12:37
  • http://stackoverflow.com/questions/19455100/can-i-run-numpy-and-pandas-with-jython – tim_yates Sep 13 '16 at 12:37
  • (It's still on the [wishlist for JyNi](http://jyni.org/#wish-list), so I think the answer to the question is "you can't") – tim_yates Sep 13 '16 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 – Mahesha999 Sep 14 '16 at 12:45
  • Can you write a second .py file that calls the first .py file, receives the DataFrame, and serializes it to JSON? – charlesreid1 Sep 08 '21 at 21:35
  • isnt "py file calling another py file" simply means "py module importing another py module"? – Mahesha999 Sep 09 '21 at 11:44

0 Answers0