Edit this page on our live server and create a PR by running command !create-pr in the console panel

Working with Julia

  • Difficulty level: easy
  • Time need to lean: 10 minutes or less
  • Key points
    • There is almost a one-to-one correspondence between Julia and Python types
    • Python has one str type and Julia has Char and String

Julia

Julia's data types are pretty close to Python and there is almost a one-to-one correspondence between Julia and Python types.

When transferring data from Python (SoS) to Julia (e.g. %get from Julia), the resulting Julia types are list as follows:

Python condition Julia
None NaN
boolean Bool
integer Int64
numpy.float64 Float64
float Float64
complex Complex{}
str String
Sequence (list, tuple, ...) homogenous type, all integer Array{Int64,1}
Sequence (list, tuple, ...) homogenous type, all numeric with any float Array{Float64,1}
Sequence (list, tuple, ...) homogenous type, all str Array{String,1}
Sequence (list, tuple, ...) multiple types Array{Any,1}
set homogenous type, all integer Set{Int64}
set homogenous type, all numeric with any float Set{Float64}
set homogenous type, all str Set{String}
set multiple types Set{Any}
dict Dict{}
numpy.ndarray Array{}
numpy.matrix Array{}
pandas.Series NamedArrays.NamedArray{}
pandas.DataFrame Dataframes.Dataframe{}

Python objects in other datatypes are transferred as string "Unsupported datatype".

It is worth noting that although Julia supports Char (single character) and String types, SoS always transfer Python str to Julia String, so if you have a Python string X,

In [1]:
In [2]:
Out[2]:
"X"
In [3]:
Out[3]:
String

If you need to access the first character as a Char, just use

In [4]:
Out[4]:
88

Conversion of datatypes from Julia to SoS (%get var --from Julia from SoS) follows the following rules:

Julia condition Python
NaN None
Bool boolean
Int64 integer
Char str
Complex{} complex
Float64 numpy.float64
String str
Array{,1} Sequence (list, tuple, ...)
Set{} set
Dict{} dict
Array{Int64/Float64,2} 2 dimensions numpy.matrix
Array{Int64/Float64,} > 2 dimensions numpy.ndarray
NamedArrays.NamedArray 1 dimension pandas.Series
Dataframes.Dataframe pandas.DataFrame

Julia objects in other datatypes are transferred as string "Unsupported datatype".

Julia supports DataFrame from its DataFrames package so you will need to install this package before using the Julia kernel. For example, a R dataframe is transfered as Dataframes.Dataframe to Julia.

In [5]:
Raw index is ignored because Julia DataFrame does not support raw index.
/Users/bpeng/anaconda3/envs/sos/lib/python3.8/site-packages/pyarrow/feather.py:83: FutureWarning: The SparseDataFrame class is removed from pandas. Accessing it from the top-level namespace will also be removed in the next version
  if isinstance(df, _pandas_api.pd.SparseDataFrame):
Out[5]:

32 rows × 11 columns (omitted printing of 2 columns)

mpgcyldisphpdratwtqsecvsam
Float64Float64Float64Float64Float64Float64Float64Float64Float64
121.06.0160.0110.03.92.6216.460.01.0
221.06.0160.0110.03.92.87517.020.01.0
322.84.0108.093.03.852.3218.611.01.0
421.46.0258.0110.03.083.21519.441.00.0
518.78.0360.0175.03.153.4417.020.00.0
618.16.0225.0105.02.763.4620.221.00.0
714.38.0360.0245.03.213.5715.840.00.0
824.44.0146.762.03.693.1920.01.00.0
922.84.0140.895.03.923.1522.91.00.0
1019.26.0167.6123.03.923.4418.31.00.0
1117.86.0167.6123.03.923.4418.91.00.0
1216.48.0275.8180.03.074.0717.40.00.0
1317.38.0275.8180.03.073.7317.60.00.0
1415.28.0275.8180.03.073.7818.00.00.0
1510.48.0472.0205.02.935.2517.980.00.0
1610.48.0460.0215.03.05.42417.820.00.0
1714.78.0440.0230.03.235.34517.420.00.0
1832.44.078.766.04.082.219.471.01.0
1930.44.075.752.04.931.61518.521.01.0
2033.94.071.165.04.221.83519.91.01.0
2121.54.0120.197.03.72.46520.011.00.0
2215.58.0318.0150.02.763.5216.870.00.0
2315.28.0304.0150.03.153.43517.30.00.0
2413.38.0350.0245.03.733.8415.410.00.0
2519.28.0400.0175.03.083.84517.050.00.0
2627.34.079.066.04.081.93518.91.01.0
2726.04.0120.391.04.432.1416.70.01.0
2830.44.095.1113.03.771.51316.91.01.0
2915.88.0351.0264.04.223.1714.50.01.0
3019.76.0145.0175.03.622.7715.50.01.0

As you can see from the warning, row labels of the dataframe is not transferred because Julia's dataframe does not support row label. If you really need the row labels, you would have to assign row labels to a separate variable and transfer.

In [6]:
┌ Warning: `getindex(df::DataFrame, col_ind::ColumnIndex)` is deprecated, use `df[!, col_ind]` instead.
│   caller = top-level scope at In[11]:1
└ @ Core In[11]:1
Out[6]:
32-element Arrow.Primitive{Float64}:
 21.0
 21.0
 22.8
 21.4
 18.7
 18.1
 14.3
 24.4
 22.8
 19.2
 17.8
 16.4
 17.3
  ⋮
 21.5
 15.5
 15.2
 13.3
 19.2
 27.3
 26.0
 30.4
 15.8
 19.7
 15.0
 21.4
In [7]:

When an Julia Array is transferred to SoS (Python3), it becomes a numpy.array.

In [8]:
Out[8]:
'Untransferrable variable'
In [ ]: