# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # cuDF Cheat Sheets sample code
#
# (c) 2020 NVIDIA, Blazing SQL
#
# Distributed under Apache License 2.0

# ### Imports

import cudf
import numpy as np

# ### Sample DataFrame

df = cudf.DataFrame(
    [
          (39, 6.88, np.datetime64('2020-10-08T12:12:01'), np.timedelta64(14378,'s'), 'C', 'D', 'data'
            , 'RAPIDS.ai is a suite of open-source libraries that allow you to run your end to end data science and analytics pipelines on GPUs.')
        , (11, 4.21, None,                                 None                     , 'A', 'D', 'cuDF'
            , 'cuDF is a Python GPU DataFrame (built on the Apache Arrow columnar memory format)')
        , (31, 4.71, np.datetime64('2020-10-10T09:26:43'), np.timedelta64(12909,'s'), 'U', 'D', 'memory'
            , 'cuDF allows for loading, joining, aggregating, filtering, and otherwise manipulating tabular data using a DataFrame style API.')
        , (40, 0.93, np.datetime64('2020-10-11T17:10:00'), np.timedelta64(10466,'s'), 'P', 'B', 'tabular'
            , '''If your workflow is fast enough on a single GPU or your data comfortably fits in memory on 
                 a single GPU, you would want to use cuDF.''')
        , (33, 9.26, np.datetime64('2020-10-15T10:58:02'), np.timedelta64(35558,'s'), 'O', 'D', 'parallel'
            , '''If you want to distribute your workflow across multiple GPUs or have more data than you can fit 
                 in memory on a single GPU you would want to use Dask-cuDF''')
        , (42, 4.21, np.datetime64('2020-10-01T10:02:23'), np.timedelta64(20480,'s'), 'U', 'C', 'GPUs'
            , 'BlazingSQL provides a high-performance distributed SQL engine in Python')
        , (36, 3.01, np.datetime64('2020-09-30T14:36:26'), np.timedelta64(24409,'s'), 'T', 'D', None
            , 'BlazingSQL is built on the RAPIDS GPU data science ecosystem')
        , (38, 6.44, np.datetime64('2020-10-10T08:34:36'), np.timedelta64(90171,'s'), 'X', 'B', 'csv'
            , 'BlazingSQL lets you ETL raw data directly into GPU memory as a GPU DataFrame (GDF)')
        , (17, 5.28, np.datetime64('2020-10-09T08:34:40'), np.timedelta64(30532,'s'), 'P', 'D', 'dataframes'
            , 'Dask is a flexible library for parallel computing in Python')
        , (10, 8.28, np.datetime64('2020-10-03T03:31:21'), np.timedelta64(23552,'s'), 'W', 'B', 'python'
            , None)
    ]
    , columns = ['num', 'float', 'datetime', 'timedelta', 'char', 'category', 'word', 'string']
)
df['category'] = df['category'].astype('category')


# ---
#
# # Transforming
#
# ---

# #### cudf.core.dataframe.DataFrame.apply_rows()

# +
def regression(a, b, output, A_coeff, B_coeff, constant):
    for i, (aa, bb) in enumerate(zip(a,b)):
        output[i] = A_coeff * aa + B_coeff * bb + constant
        
df.apply_rows(
    regression
    , incols = {'num': 'a', 'float': 'b'}
    , outcols = {'output': np.float64}
    , kwargs = {'A_coeff': 0.21, 'B_coeff': -2.82, 'constant': 3.43}
)
# -

# #### cudf.core.dataframe.DataFrame.drop()

df.drop(1)

df.drop(range(1,7))

df.drop('word', axis=1)

df.drop(['word', 'category'], axis=1)

# #### cudf.core.dataframe.DataFrame.dropna()

df.dropna()

df.dropna(subset=['datetime'])

df.dropna(how='all')

df.dropna(axis=1)

# #### cudf.core.dataframe.DataFrame.fillna()

df.fillna({'num': 1})

# #### cudf.core.dataframe.DataFrame.join()

# +
categories = cudf.DataFrame([
    ('B', 'cuDF')
    , ('C', 'BlazingSQL')
    , ('D', 'Dask')
], columns=['cat', 'name'])

df.join(categories)
# -

df.join(categories, lsuffix='_l', rsuffix='_r', how='left')

# #### cudf.core.dataframe.DataFrame.merge()

categories = cudf.DataFrame([
    ('B', 'cuDF')
    , ('C', 'BlazingSQL')
    , ('D', 'Dask')
], columns=['category', 'name'])

df.merge(categories, on='category')

df.merge(categories, on='category', how='left')

# #### cudf.core.dataframe.DataFrame.rename()

df.rename({0: 100})

df.rename({'num': 'numbers'}, axis=1)

# #### cudf.core.dataframe.DataFrame.reset_index()

df.reset_index()

df.reset_index(drop=True)

# #### cudf.core.dataframe.DataFrame.set_index()

df.set_index('category')

df.set_index('category', drop=False)

StarcoderdataJupyterScriptsDedupFiltered