An Overview of Python’s Datatable package
Modern machine learning applications need to process a humongous amount of data and generate multiple features. Python’s datatable module was created to address this issue. It is a toolkit for performing big data (up to 100GB) operations on a single-node machine, at the maximum possible speed.
By Parul Pandey
“There were 5 Exabytes of information created between the dawn of civilization through 2003, but that much information is now created every 2 days”:Eric Schmidt
If you are an R user, chances are that you have already been using the
Data.table is an extension of the
data.frame package in R. It’s also the go-to package for R users when it comes to the fast aggregation of large data (including 100GB in RAM).
data.table package is a very versatile and a high-performance package due to its ease of use, convenience and programming speed. It is a fairly famous package in the R community with over 400k downloads per month and almost 650 CRAN and Bioconductor packages using it(source).
So, what is in it for the Python users? Well, the good news is that there also exists a Python counterpart to the
data.table package called
datatablewhich has a clear focus on big data support, high performance, both in-memory and out-of-memory datasets, and multi-threaded algorithms. In a way, it can be called as data.table’s younger sibling.
Modern machine learning applications need to process a humongous amount of data and generate multiple features. This is necessary in order to build models with greater accuracy. Python’s
datatable module was created to address this issue. It is a toolkit for performing big data (up to 100GB) operations on a single-node machine, at the maximum possible speed. The development of datatable is sponsored by H2O.ai and the first user of
datatable was Driverless.ai.
This toolkit resembles pandas very closely but is more focussed on speed and big data support. Python’s
datatable also strives to achieve good user experience, helpful error messages, and a powerful API. In this article, we shall see how we can use datatable and how it scores over pandas when it comes to large datasets.
On MacOS, datatable can be easily installed with pip:
On Linux, installation is achieved with a binary distribution as follows:
Currently, datatable does not work on Windows but work is being done to add support for Windows also.
For more information see Build instructions.
The code for this article can be accessed from the associated Github Repository or can be viewed on my binder by clicking the image below.
Reading the Data
The dataset being used has been taken from Kaggle and belongs to the Lending Club Loan Data Dataset. The dataset consists of complete loan data for all loans issued through the 2007–2015, including the current loan status (Current, Late, Fully Paid, etc.) and latest payment information. The file consists of 2.26 Million rows and 145 columns. The data size is ideal to demonstrate the capabilities of the datatable library.
Let’s load in the data into the
Frame object. The fundamental unit of analysis in datatable is a
Frame. It is the same notion as a pandas DataFrame or SQL table: data arranged in a two-dimensional array with rows and columns.
fread() function above is both powerful and extremely fast. It can automatically detect and parse parameters for the majority of text files, load data from .zip archives or URLs, read Excel files, and much more.
Additionally, the datatable parser :
- Can automatically detect separators, headers, column types, quoting rules, etc.
- Can read data from multiple sources including file, URL, shell, raw text, archives and glob.
- Provides multi-threaded file reading for maximum speed
- Includes a progress indicator when reading large files
- Can read both RFC4180-compliant and non-compliant files.
Now, let us calculate the time taken by pandas to read the same file.
The results show that datatable clearly outperforms pandas when reading large datasets. Whereas pandas take more than a minute, datatable only takes seconds for the same.
The existing Frame can also be converted into a numpy or pandas dataframe as follows:
Let’s convert our existing frame into a pandas dataframe object and compare the time taken.
It appears that reading a file as a datatable frame and then converting it to pandas dataframe takes less time than reading through pandas dataframe. Thus, it might be a good idea to import a large data file through datatable and then convert it to pandas dataframe.
Basic Frame Properties
Let’s look at some of the basic properties of a datatable frame which are similar to the pandas’ properties:
We can also use the
head command to output the top ‘n’ rows.
The colour signifies the datatype where red denotes string, green denotes int and blue stands for float.
Calculating the summary stats in pandas is a memory consuming process but not anymore with datatable. We can compute the following per-column summary stats using datatable:
Let’s calculate the mean of the columns using both datatable and pandas to measure the time difference.
The above command cannot be completed in pandas as it starts throwing memory error.
Data Tables like dataframes are columnar data structures. In datatable, the primary vehicle for all these operations is the square-bracket notationinspired by traditional matrix indexing but with more functionalities.
The same DT[i, j] notation is used in mathematics when indexing matrices, in C/C++, in R, in pandas, in numpy, etc. Let’s see how we can perform common data manipulation activities using datatable:
#Selecting Subsets of Rows/Columns
The following code selects all rows and the
funded_amnt column from the dataset.
Here is how we can select the first 5 rows and 3 columns
#Sorting the Frame
Sorting the frame by a particular column can be accomplished by
Notice the substantial time difference between datable and pandas.
Here is how we can delete the column named
Just like in pandas, datatable also has the groupby functionalities. Let’s see how we can get the mean of
funded_amount column grouped by the
What does .f stand for?
f stands for
frame proxy, and provides a simple way to refer to the Frame that we are currently operating upon. In the case of our example,
dt.fsimply stands for
The syntax for filtering rows is pretty similar to that of GroupBy. Let us filter those rows of
loan_amntfor which the values of
loan_amnt are greater than
Saving the Frame
It is also possible to write the Frame’s content into a
csv file so that it can be used in future.
For more data manipulation functions, refer to the documentation page.
The datatable module definitely speeds up the execution as compared to the default pandas and this definitely is a boon when working on large datasets. However, datatable lags behind pandas in terms of the functionalities. But since datatable is still undergoing active development, we might see some major additions to the library in the future.
- R’s data.table
- Datatable documentation
- Getting started with Python datatable: A wonderful Kaggle Kernel on the usage of datatable
Bio: Parul Pandey is a Data Science enthusiast who frequently writes for Data Science publications such as Towards Data Science.
Original. Reposted with permission.
- 25 Tricks for Pandas
- 10 Simple Hacks to Speed up Your Data Analysis in Python
- Become a Pro at Pandas, Python’s Data Manipulation Library