When exchanging data frames with Python-> .NET in the first place, the .NET side has a standard library class called DataTable, which is suitable. It doesn't have the high-performance data processing power of Pandas and dplyr, but it can be mapped for the time being. If you want to call it from Python on BayesServer, it seems that it is recommended to access the Java library using JPype as shown below.
On the other hand, it says that pythonnet is fine, and I'm not sure why I have to go through a Java wrapper even though it's native to .NET, so I decided to go there. The R wrapper also goes through the Java wrapper. With this, you have to look at two types of API references. (Because I will modify the .NET library anyway and rewrap it) I should have used Python from the beginning. .. ..
pythonnet
pythonnet is a Python to .NET access library called Python for .NET. http://pythonnet.github.io/ You can use it both when calling .NET from Python and when calling Python from .NET.
Python + .NET may remind you of IronPython, but it's a CLI language that runs on .NET. With this, the support for libraries that have a good reputation in data science etc. is limited. Or rather, numpy doesn't work.
Since pythonnet only connects languages, you can use it while keeping the good points of both. It also supports the latest .NET Core and so on. Speaking sensuously
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form
There is a taste of import clr.
dynamic np = Py.Import("numpy");
var x = np.cos(np.pi * 2);
There is an article by @hogegex in this direction.
Possible uses include calling .NET-specific libraries (business systems, etc.) from Python, and throwing numerical calculation tasks from .NET. It seems good to be able to create an app using the .NET GUI library on Python. However, my purpose is to call the BayesServer library from Python, so if I can do the following, I'm almost done.
The BayesServer reference provides a dataframe mapping helper function using jpype1 as a helper class.
Pandas DataFrame helper functions
Probably, conversion using jpype1 has a habit, so I think that they have prepared such a thing, but pythonnet can write it more straightforwardly. I almost rewrote it for .NET with copy and paste.
# %%
import numpy as np
import pandas as pd
import clr
from System.Data import *
# %%
def _to_net_class(data_type):
"""
Converts numpy data type to equivalent .NET class
:param data_type: the numpy data type
:return: The Net Class
"""
if data_type == np.int32:
return clr.GetClrType(Int32)
if data_type == np.int64:
return clr.GetClrType(Int64)
if data_type == np.float32:
return clr.GetClrType(Single)
if data_type == np.float64:
return clr.GetClrType(Double)
if data_type == np.bool:
return clr.GetClrType(Boolean)
if data_type == np.object:
return clr.GetClrType(Object)
raise ValueError('dtype [{}] not currently supported'.format(data_type))
# %%
def to_data_table(df):
data_table = DataTable()
for name, data_type in df.dtypes.iteritems():
net_class = _to_net_class(data_type)
data_table.Columns.Add(str(name),net_class)
for index, row in df.iterrows():
xs = [None if pd.isnull(x) else x for x in row]
data_table.Rows.Add(xs)
return data_table
With this, pandas data can be brought into the .NET library, so it may be easier to exchange with a GUI such as a so-called Excel table or a database. Think of clr.GetClrType () as an alternative to typeof (or Object.GetType ()).
Recommended Posts