Download Python Module for Table Valued Functions

As I wrote before, I am working on a Python module which makes the table functions of the non-commercial .NET library Finaquant Protos available for Python developers. You may download the latest release of this module (tablenet.py) here:
Download tablenet.py - Version 1.02 (29. June 2015)
Prerequisites:
Unzip the downloaded zip file. You will find two files in the folder: tablenet.py (Python module) and FinaquantProtos.dll (.NET assembly with the class MatrixTable that contain table-valued functions). You can now run tablenet.py in Python to test the module.
As of today (26. June 2015), the first version of the module tablenet.py includes only a small subset of the table functions available in Finaquant Protos. I will continue to work on the module to extend the features, and publish the latest release regularly here.
This development process may continue for several months, even years; I will append the release history here. Please let me know if you have any questions, or improvement suggestions.
Module information for the first release:
Table-Valued Functions in Python. Version: 1.01 (26. June 2015)
Python (2.7) module with the Table class, which is a wrapper for the MatrixTable
class of the .NET library 'Finaquant Protos' with table-valued functions.
This module (tablenet.py) requires .NET library 'Finaquant Protos' for execution.
The .NET integration module pythonnet must be installed
('pip install pythonnet' in command line) before running or importing this module.
Author: Tunc Ali Kutukcuoglu, tuncalik (at) finaquant.com
Website: http://www.finaquant.com
LinkedIn: http://ch.linkedin.com/pub/tunc-ali-kutukcuoglu/45/664/105
Stackoverflow: http://stackoverflow.com/users/1625567/tuncalik
Example test code from the module:
Converting a pandas DataFrame to a Table (MatrixTable)
Converting a Table to a to a DataFrame
As you see above, a DataFrame can easily be converted to a Table (MatrixTable), and vice versa. This is practical, because DataFrame class of pandas doesn't have all the table functions included in Table, like table arithmetic, filtering with a condition table, subtable transformers, function routers, filtering cells and cell lists, and so on. On the other hand, Table class doesn't have all the table functions included in DataFrame, especially the more SQL-like ones (select, update etc.).
More examples with Table class:
Download tablenet.py - Version 1.02 (29. June 2015)
Prerequisites:
- Python 2.7
- .NET4 Framework
- .NET integration module pythonnet must be installed ("pip install pythonnet" in command line)
Unzip the downloaded zip file. You will find two files in the folder: tablenet.py (Python module) and FinaquantProtos.dll (.NET assembly with the class MatrixTable that contain table-valued functions). You can now run tablenet.py in Python to test the module.
As of today (26. June 2015), the first version of the module tablenet.py includes only a small subset of the table functions available in Finaquant Protos. I will continue to work on the module to extend the features, and publish the latest release regularly here.
This development process may continue for several months, even years; I will append the release history here. Please let me know if you have any questions, or improvement suggestions.
Module information for the first release:
Table-Valued Functions in Python. Version: 1.01 (26. June 2015)
Python (2.7) module with the Table class, which is a wrapper for the MatrixTable
class of the .NET library 'Finaquant Protos' with table-valued functions.
This module (tablenet.py) requires .NET library 'Finaquant Protos' for execution.
The .NET integration module pythonnet must be installed
('pip install pythonnet' in command line) before running or importing this module.
Author: Tunc Ali Kutukcuoglu, tuncalik (at) finaquant.com
Website: http://www.finaquant.com
LinkedIn: http://ch.linkedin.com/pub/tunc-ali-kutukcuoglu/45/664/105
Stackoverflow: http://stackoverflow.com/users/1625567/tuncalik
Example test code from the module:
Converting a pandas DataFrame to a Table (MatrixTable)
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
- mdat = MetaData.CreateEmptyMetaData()
- # convert a [url=http://pandas.pydata.org/]pandas[/url] DataFrame to a Table object, and vice versa
- import pandas as pd
- df = pd.DataFrame({ 'Product' : ['car','bus','cycle','motor'],
- 'Price' : np.array([120.3, 150, 23, 87.6]),
- 'Serial' : np.array([3,5,6,7], int),
- 'Category' : pd.Categorical(["test","train","test","train"]),
- 'Type' : 'pricey' })
- print "df w/0 Timestamp:\n", df
- tbl = DataFrameToMatrixTable(df, mdat)
- tbl.ViewTable("Converted DataFrame w/o a Timestamp field")
- GeSHi ©
Converting a Table to a to a DataFrame
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
- # create table manually with values
- fields = ['product', 'id', 'date', 'price']
- types = [Table.TextAttributeID(), Table.IntegerAttributeID(), Table.DateAttributeID(), Table.KeyFigureID()]
- # types = [FieldType.TextAttribute, FieldType.DateAttribute, FieldType.KeyFigure]
- print "types = " , types
- values = [
- "car", 12, 5, 120.0,
- "bus", 9, 10, 145.0]
- tbl3 = Table.CreateTableWithValues(mdat,fields,types,values)
- tbl3.ViewTable("tbl 3")
- # convet Table to DataFrame
- df = tbl3.ToDataFrame
- GeSHi ©
As you see above, a DataFrame can easily be converted to a Table (MatrixTable), and vice versa. This is practical, because DataFrame class of pandas doesn't have all the table functions included in Table, like table arithmetic, filtering with a condition table, subtable transformers, function routers, filtering cells and cell lists, and so on. On the other hand, Table class doesn't have all the table functions included in DataFrame, especially the more SQL-like ones (select, update etc.).
More examples with Table class:
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
- def Execute(execstr):
- print execstr, ':\n', eval(execstr)
- # write/read table to/from file (saving and reading tables)
- filepath = r"tbl3.txt"
- tbl3.WriteTableToFile(filepath)
- tbl4 = Table(mdat)
- tbl4.ReadFromFile(filepath)
- print "tbl4:\n", tbl4
- # create table with NumPy arrays
- FieldNValuesDic = (
- {'product' : np.array(['car', 'bus', 'cycle', 'motor']),
- 'category': np.array(['economy','luxury','economy','luxury']),
- 'date': np.array([2,4,6,8], int),
- 'price': np.array([120, 200, 300, 250], float)})
- tbl5 = Table.CreateTableWithNumArrays(mdat, FieldNValuesDic)
- print "tbl5:\n", tbl5
- # create table with field value combination
- FieldNValuesDic2 = (
- {'product' : np.array(['car', 'bus', 'cycle', 'motor']),
- 'category': np.array(['economy','luxury']),
- 'price': np.array([120, 200, 300], float)})
- tbl = Table.CreateTableByFieldValueComb(mdat, FieldNValuesDic2)
- tbl.ViewTable("Field Value Combination - 1")
- # get and set table elements by indices
- print "tbl6[(2,2)] = ", tbl6[(2,2)]
- print "tbl6[('product',2)] = ", tbl6[('product',2)]
- tbl6[(2,2)] = 7
- tbl6[('product',2)] = 'cycle zzz'
- print "tbl6[(2,2)] = ", tbl6[(2,2)]
- print "tbl6[('product',2)] = ", tbl6[('product',2)]
- # check if two tables are equal
- print "tbl.IsEqual(tbl6): ", tbl.IsEqual(tbl6)
- # sort table
- Execute("tbl6.SortRows()")
- Execute("tbl6.SortRows('product DESC')")
- # create table containing all possible row combinations
- fields = ['product', 'date']
- types = [Table.TextAttributeID(), Table.DateAttributeID()]
- values = [
- "car", 1200,
- "bus", 2300]
- tbl1 = Table.CreateTableWithValues(mdat,fields,types,values)
- fields = ['category', 'price']
- types = [Table.TextAttributeID(), Table.KeyFigureID()]
- values = [
- "luxury", 250.0,
- "economy", 500.0]
- tbl2 = Table.CreateTableWithValues(mdat,fields,types,values)
- tbl = Table.CreateTableByRowComb(mdat, tbl1, tbl2)
- tbl.ViewTable("Table by Row Combination")
- # add a scalar value to all key figures of table
- tbl = tbl6.InsertNewColumn('length', Table.KeyFigureID()) # insert key figure
- Execute("tbl.AddScalarToAllKeyFigures(15.55)")
- Execute("tbl + 25.33")
- # multiply all key figures of table with a scalar value
- Execute("tbl.MultiplyAllKeyFiguresWithScalar(5.55)")
- Execute("(tbl + 10) * 3.333")
- # subtract a scalar value from all key figures of table
- Execute("tbl.SubtractScalarFromAllKeyFigures(5.11)")
- Execute("(tbl * 10) - 33.33")
- # combine tables
- FieldNValuesDic1 = (
- {'product' : np.array(['car', 'bus', 'cycle', 'motor']),
- 'category': np.array(['economy','luxury']),
- 'price': np.array([120, 200, 300], float)})
- tbl1 = Table.CreateTableByFieldValueComb(mdat, FieldNValuesDic2)
- fields = ['product', 'category', 'brand', 'costs']
- types = [Table.TextAttributeID(), Table.TextAttributeID(), Table.TextAttributeID(), Table.KeyFigureID()]
- values = [
- "car", "luxury", 'WW', 400.0,
- "ALL", "luxury", 'ZZ', 300.0,
- "ALL", "economy", 'XX', 200.0]
- tbl2 = Table.CreateTableWithValues(mdat,fields,types,values)
- Execute("tbl1.CombineTables(tbl2, JokerMatchesAllvalues = True)")
- Execute("tbl1.CombineTablesFirstMatch(tbl2, JokerMatchesAllvalues = True)")
- GeSHi ©