petbox-dev / tafra

a minimalist dataframe

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tafra: a minimalist dataframe

image

image

Documentation Status

Coverage Status

The tafra began life as a thought experiment: how could we reduce the idea of a dataframe (as expressed in libraries like pandas or languages like R) to its useful essence, while carving away the cruft? The original proof of concept stopped at "group by".

This library expands on the proof of concept to produce a practically useful tafra, which we hope you may find to be a helpful lightweight substitute for certain uses of pandas.

A tafra is, more-or-less, a set of named columns or dimensions. Each of these is a typed numpy array of consistent length, representing the values for each column by rows.

The library provides lightweight syntax for manipulating rows and columns, support for managing data types, iterators for rows and sub-frames, pandas-like "transform" support and conversion from pandas Dataframes, and SQL-style "group by" and join operations.

Tafra Tafra
Aggregations Union, GroupBy, Transform, IterateBy, InnerJoin, LeftJoin, CrossJoin
Aggregation Helpers union, union_inplace, group_by, transform, iterate_by, inner_join, left_join, cross_join
Constructors as_tafra, from_dataframe, from_series, from_records
SQL Readers read_sql, read_sql_chunks
Destructors to_records, to_list, to_tuple, to_array, to_pandas
Properties rows, columns, data, dtypes, size, ndim, shape
Iter Methods iterrows, itertuples, itercols
Functional Methods row_map, tuple_map, col_map, pipe
Dict-like Methods keys, values, items, get, update, update_inplace, update_dtypes, update_dtypes_inplace
Other Helper Methods select, head, copy, rename, rename_inplace, coalesce, coalesce_inplace, _coalesce_dtypes, delete, delete_inplace
Printer Methods pprint, pformat, to_html
Indexing Methods _slice, _index, _ndindex

Getting Started

Install the library with pip:

pip install tafra

A short example

Flexibility

Have some code that works with pandas, or just a way of doing things that you prefer? tafra is flexible:

And going back is just as simple:

Timings

In this case, lightweight also means performant. Beyond any additional features added to the library, tafra should provide the necessary base for organizing data structures for numerical processing. One of the most important aspects is fast access to the data itself. By minimizing abstraction to access the underlying numpy arrays, tafra provides an order of magnitude increase in performance.

  • Import note If you assign directly to the Tafra.data or Tafra._data attributes, you must call Tafra._coalesce_dtypes afterwards in order to ensure the typing is consistent.

Construct a Tafra and a DataFrame:

Read Operations

Direct access:

Indirect with some penalty to support Tafra slicing and numpy's advanced indexing:

pandas timing:

This is the fastest methed for accessing the numpy array among alternatives of df.values(), df.to_numpy(), and df.loc[].

Assignment Operations

Direct access is not recommended as it avoids the validation steps, but it does provide fast access to the data attribute:

Indidrect access has a performance penalty due to the validation checks to ensure consistency of the tafra:

Even so, there is considerable performance improvement over pandas.

pandas timing:

Grouping Operations

tafra also excels at aggregation methods, the primary of which are a SQL-like GROUP BY and the split-apply-combine equivalent to a SQL-like GROUP BY following by a LEFT JOIN back to the original table.

The equivalent pandas functions are given below. They require a chain of several object methods to perform the same role, and the transform requires a copy operation and assignment into the copied DataFrame in order to preserve immutability.

About

a minimalist dataframe

License:MIT License


Languages

Language:Python 99.7%Language:Shell 0.2%Language:Batchfile 0.1%