Home Business firmai/pandapy

firmai/pandapy

by admin2 admin2
58 views
firmai/pandapy

PandaPy
Install
!pip3 install pandapy

Load

Why PandaPy?
Maintains the full functionality and speed of structured NumPy datatype (eg., array[col1] + array[col2], or np.log(array[col1])
Provides wrapper functions over NumPy to give you the usability of Pandas (eg., pp.group(array, [col1, col2, col2], [‘mean’, ‘std’], [‘Adj_Close’,’Close’])
If you need Pandas for speciality functions, you can easily df = pp.pandas(array) and back array = pp.structured(df)
For simple calculations (i.e, plus, mult, log) PandaPy is 25x – 80x faster than Pandas.
For table functions (i.e., group, pivot, drop, concat, fillna) PandaPy is 5x – 100x times faster than Pandas.
For most use cases, PandaPy is faster than Dask, Modin Ray and Pandas.
The best competing python package for performance on table functions is datatable, it is 2x – 10x faster than PandaPy.
The problem is that datatable is 5x – 10x slower with simple calculations (plus, mult, returns), it is less intuitive, does not have a large range of functions, have very few complementary libraries, e.g. matplotlib, and doesn’t leave you in a Numpy datatype.
For finance applications the speed of simple calculations takes preference over table function speed.
PandaPy is not created to allow you to scale up to clusters for multiple computer processing like Dask, Modin, and Spark, instead it is focused on speed and usability within a single computer’s Memory.
Machines are getting large, EC2 X1 has 2TB of RAM and is remarkably affordable. If it can be done on a single machine then it should be done on a single machine. Quoting Dask – “For data that fits into RAM, Pandas can often be faster and easier to use than Dask DataFrame”
If your dataset is very small you can load your data using PandaPy’s read() function, for medium sized data, it is best to load it with datatable or pyspark and convert it to structured Numpy, if it is large pyspark, Dask, or Modin, if it is very large use pyspark.
Lastly PandaPy can have as input any multidimensional object and does not have to conform to the basic NumPy datatypes. It can include nested datatypes, subarrays, functions as long as each column conforms to the array lenght, this allows for a great amount of flexibility. You can for example, add(array, “panda function”,[[pd for i in range(len(multiple_stocks))]]) to create a list of the panda (pd) module and access it along any index value array[“panda function”][0].read_csv(url).
PandaPy software, similar to the original Pandas project, is developed to improve the usability of python for finance. Structured datatypes are designed to be able to mimic ‘structs’ in the C language, and share a similar memory layout. PandaPy currently houses more than 30 functions. Structured NumPy are meant for interfacing with C code and for low-level manipulation of structured buffers, for example for interpreting binary blobs. For these purposes they support specialized features such as subarrays, nested datatypes, and unions, and allow control over the memory layout of the structure.
Note this is a fledgling project, much room for improvement, all feedback appreciated (issues tab)
Description
A Structured NumPy Array is an array of structures. NumPy arrays can only contain one data type, but structured arrays in a sense create an array of homogeneous structures. This is done without moving out of NumPy such as is required with Xarray. For structured arrays the data type only has to be the same per column like an SQL data base. Each column can be another multidimensional object and does not have to conform to the basic NumPy datatypes.
PandaPy comes with similar functionality like Pandas, such as groupby, pivot, and others. The biggest benefit of this approach is that NumPy dtype(data type) directly maps onto a C structure definition, so the buffer containing the array content can be accessed directly within an appropriately written C program. If you find yourself writing a Python interface to a legacy C or Fortran library that manipulates structured data, you’ll probably find structured arrays quite useful.
Additional
Play around with speed tests here and some more here.
Test and explore the package with this Google Colab Notebook.
Get in touch on LinkedIn or Twitter.
Use table(array) to get a pandas looking table printout
Functions
PandaPy Speed Over Pandas In (X) e.g., (dropnarow) (30x)
Array Structure
Read In Arrays (read)
To Pandas (unstructured)
Pandas to Structured (structured)
To Unstructured (to_unstruct)
To Structured (to_struct)
Print Table (table)

Explorative Functions
Descriptive Statistics (describe) (5x)
Correlation Array (corr) (2x)

Finance Functions
Returns (returns) (50x)
Portfolio Value (portfolio_value) (50x)
Cummulative Value (cummulative_return) (50x)
Column Lags (lags) (7x)

Array Functions
Drop Null Rows (dropnarow) (30x)
Drop Column/s (drop) (100x)
Add Column/s (add) (3x)
Concatenate (concat) (rows 25x columns 70x)
Merge (merge) (2x)
Group by (group) (10x)
Pivot (pivot) (20x)
Fill Nulls (fillna) (20x)
Shift Column (shift) (50x)
Rename (rename) (500x)

Other Speed Tests
Update (array[col] = values) (60x)
Addition (array[col] + array[col]) (80x)
Multiplication (array[col] * array[col]) (80x)
Log (np.log(array[col]) (25x)

note speed tests done on financial dataset only
Documentation by Example
Read In Arrays
# First Example
multiple_stocks = pp.read(‘https://github.com/firmai/random-assets-two/blob/master/numpy/multiple_stocks.csv?raw=true’)
closing = multiple_stocks[[‘Ticker’,’Date’,’Adj_Close’]]
piv = pp.pivot(closing,”Date”,”Ticker”,”Adj_Close”); piv
closing = pp.to_struct(piv, name_list = [x for x in np.unique(multiple_stocks[“Ticker”])])

# Second Example
tsla = pp.read(‘https://github.com/firmai/random-assets-two/raw/master/numpy/tsla.csv’)
crm = pp.read(‘https://github.com/firmai/random-assets-two/raw/master/numpy/crm.csv’)
tsla_sub = tsla[[“Date”,”Adj_Close”,”Volume”]]
crm_sub = crm[[“Date”,”Adj_Close”,”Volume”]]
crm_adj = crm[[‘Date’,’Adj_Close’]]
closing

array([(37.24206924, 100.45429993, 44.57522202, 20.72605705, 130.59109497, 35.80251312, 41.9791832 , 81.51140594, 66.33999634),
(35.08446503, 97.62433624, 43.83200836, 20.34561157, 128.53627014, 35.80251312, 41.59314346, 80.89860535, 66.15000153),
(35.34244537, 97.63354492, 42.79874039, 19.90727234, 125.76422119, 36.07437897, 40.98268127, 80.28580475, 64.58000183),
…,
(21.57999992, 289.79998779, 59.08000183, 11.18000031, 135.27000427, 55.34999847, 158.96000671, 137.53999329, 88.37000275),
(21.34000015, 291.51998901, 58.65999985, 11.07999992, 132.80999756, 55.27000046, 157.58999634, 136.80999756, 87.95999908),
(21.51000023, 293.6499939 , 58.47999954, 11.15999985, 134.03999329, 55.34999847, 157.69999695, 136.66999817, 88.08999634)],
dtype=[(‘AA’, ‘

You may also like

Leave a Comment