Open In App

How to speed up Pandas with cuDF?

Last Updated : 26 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Pandas data frames in Python are extremely useful; they provide an easy and flexible way to deal with data and a large number of in-built functions to handle, analyze, and process the data. While Pandas data frames have a decent processing time, still in the case of computationally intensive operations, Pandas data frames tend to be slow, causing delays in data science and ML workflows. This limited speed of pandas data frames is because pandas work on CPUs that only have 8 cores. However, GPU acceleration of data science and machine learning workflows provides a solution to this problem and enhances the speed of operations at an impressive level.

cuDF

cuDF (CUDA DF) is a Python GPU data frame library that helps accelerate the loading, processing, and manipulating of massive data – thus, enabling users to perform computer-intensive operations fast. cuDF is based on an apache arrow columnar layout which we will discuss later. 

In order to shift from CPU to GPU, i.e. Pandas to cuDF, one doesn’t need to learn a new library from scratch. cuDF provides a Pandas-like API – making the shift from Pandas to cuDF quite simple for data scientists, analysts, and Machine Learning Engineers. Just like Pandas, cuDF offers two data structures: Series and Dataframe – most of the in-built functions are also available in cuDF with the same syntax.

CUDA/GPU requirements:

  • CUDA 11.0+
  • NVIDIA driver 450.80.02+
  • Pascal architecture or better (Compute Capability >=6.0)
  • Conda

cuDF can be installed with conda from the rapidsai channel:

# for CUDA 11.0
conda install -c rapidsai -c nvidia -c numba -c conda-forge \
   cudf=21.08 python=3.7 cudatoolkit=11.0

# or, for CUDA 11.2
conda install -c rapidsai -c nvidia -c numba -c conda-forge \
   cudf=21.08 python=3.7 cudatoolkit=11.2

Comparison between computational times of Pandas and cuDF

In order to analyze the time taken in both cases, let us try to load a huge dataset data.csv – first using pandas library and then using cuDF, and compare the computational time in both the cases.

In the following example, we have taken a massive dataset ‘Data.csv’ comprising 887379 Rows and 22 Columns. First, we will load the dataset using Pandas compute the time taken, then we will repeat the same using cuDF to load the same data set and compare the runtimes.

Using Pandas to load a Dataset:

Python3




# Loading the Dataset using Pandas Library (CPU Based)
import pandas as pd
import time
  
  
start = time.time()
df = pd.read_csv("Data.csv")
print("no. of rows in the dataset", df.shape[0])
print("no. of columns in the dataset", df.shape[1])
end = time.time()
print("CPU time= ", end-start)


Output:

no. of rows in the dataset 887379
no. of columns in the dataset 22
CPU time=  2.3006720542907715

The output of the above code uses Pandas to load Data.csv.

Using cuDF to load a Dataset:

Python3




# Loading the Dataset using Pandas Library (GPU Based)
import cudf
import time
  
start = time.time()
df = cudf.read_csv("../input/data-big/Data.csv")
print("no. of rows in the dataset", df.shape[0])
print("no. of columns in the dataset", df.shape[1])
end = time.time()
print("GPU time= ", end-start)


Output:

no. of rows in the dataset 887379
no. of columns in the dataset 22
GPU time=  0.1478710174560547

The output of the above code uses cuDF to load Data.csv.

From the above two cases, it can be seen that the CPU (Pandas) takes 2.3006720542907715 seconds to load the dataset while GPU (cuDF) takes only 0.1478710174560547 seconds which is much faster.

Arrow Columnar Layout in cuDF

As stated earlier, cuDF employs Apache Arrow Columnar Layout, an in-memory columnar format used to represent structured datasets. This columnar format is fast and allows computational intensive operations to work with maximum efficiency while handling and iterating big datasets.

The following represents a sample dataset in Traditional Memory Buffer and Arrow Memory Buffer (Columnar Layout).

Traditional memory Buffer Vs Arrow Memory Buffer 

Traditional Memory Buffer the data is stored in contiguous memory locations row-wise. In contrast, in the case of Arrow Memory Buffer, the data is stored in contiguous memory locations column-wise. This is one of the contributing factors towards accelerating the speed of cuDF data frames.

Note: Since cuDF requires you to have specific RAPIDS compatible GPUs, for the sake of practice/exploring one can use Kaggle or Google Colaboratory as both these platforms provide free GPU access. However, while using Google Colabs just ensure that you’ve been allocated either of the following GPUs: Tesla T4, P4, or P100 as these are the only RAPIDS compatible GPUs on Google Colab.

Thus, it is evident that using cuDF we can employ GPU acceleration on Python data frames and make the processing of data quite fast. This holds immense significance in fields of data science and ML as data is being generated in overwhelming quantities every second – and its speedy processing is imperative. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads