### DataFrame setup Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.astype/index.html Initial setup of a DataFrame for casting examples. ```python >>> import cudf >>> df = cudf.DataFrame({'a': [10, 20, 30], 'b': [1, 2, 3]}) >>> df a b 0 10 1 1 20 2 2 30 3 >>> df.dtypes a int64 b int64 dtype: object ``` -------------------------------- ### DataFrame setup for loc examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.loc/index.html Initial state of the DataFrame used in subsequent examples. ```python >>> df a b a 0 5 b 1 6 c 2 7 d 3 8 e 4 9 ``` -------------------------------- ### Setup rolling window examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.rolling/index.html Initializes the cuDF Series used for subsequent rolling window demonstrations. ```python >>> import cudf >>> import numpy as np >>> import pandas as pd >>> a = cudf.Series([1, 2, 3, None, 4]) ``` -------------------------------- ### Setup Series for padding examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.pad Initializes a cudf Series containing strings to be used in subsequent padding examples. ```python >>> import cudf >>> s = cudf.Series(["caribou", "tiger"]) ``` -------------------------------- ### StringMethods.index with start and end parameters Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.index This example shows how to use the start and end parameters to restrict the search range for the substring within each string. ```python >>> s = cudf.Series(['abc', 'abb','ab' ,'ddb']) >>> s.str.index('b', start=1, end=5) 0 1 1 1 2 1 3 2 dtype: int64 ``` -------------------------------- ### Create a DataFrame for info examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.info/index.html Initializes a sample DataFrame with integer, string, and float columns to be used in subsequent examples. ```python >>> import cudf >>> int_values = [1, 2, 3, 4, 5] >>> text_values = ['alpha', 'beta', 'gamma', 'delta', 'epsilon'] >>> float_values = [0.0, 0.25, 0.5, 0.75, 1.0] >>> df = cudf.DataFrame({"int_col": int_values, ... "text_col": text_values, ... "float_col": float_values}) >>> df int_col text_col float_col 0 1 alpha 0.00 1 2 beta 0.25 2 3 gamma 0.50 3 4 delta 0.75 4 5 epsilon 1.00 ``` -------------------------------- ### Initialize DataFrame for string replacement Source: https://docs.nvidia.com/cudf/latest/cudf/missing-data Setup a dictionary and convert it into a cuDF DataFrame for string replacement examples. ```python d = {"a": list(range(4)), "b": list("ab.."), "c": ["a", "b", None, "d"]} ``` ```python df = cudf.DataFrame(d) ``` ```python df ``` -------------------------------- ### DataFrame setup Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.drop/index.html Initialize a DataFrame for demonstration purposes. ```python >>> import cudf >>> df = cudf.DataFrame({"A": [1, 2, 3, 4], ... "B": [5, 6, 7, 8], ... "C": [10, 11, 12, 13], ... "D": [20, 30, 40, 50]}) >>> df A B C D 0 1 5 10 20 1 2 6 11 30 2 3 7 12 40 3 4 8 13 50 ``` -------------------------------- ### Install tpchgen-cli Source: https://docs.nvidia.com/cudf/latest/cudf_pandas/benchmarks Install the Rust-based TPC-H data generator. ```bash pip install tpchgen-cli ``` -------------------------------- ### Setup PyArrow Decimal Type Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.dtypes.Decimal128Dtype.from_arrow Initializes the necessary imports and creates a pyarrow decimal128 type with specified precision and scale for use in subsequent examples. ```python >>> import cudf >>> import pyarrow as pa >>> pa_type = pa.decimal128(precision=9, scale=2) ``` -------------------------------- ### Check for year start dates Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.series.DatetimeProperties.is_year_start This example demonstrates how to use the is_year_start property on a datetime Series to identify dates that fall on January 1st. ```python >>> import pandas as pd, cudf >>> s = cudf.Series(pd.date_range("2017-12-30", periods=3)) >>> dates 0 2017-12-30 1 2017-12-31 2 2018-01-01 dtype: datetime64[ns] >>> dates.dt.is_year_start 0 False 1 False 2 True dtype: bool ``` -------------------------------- ### rindex with start and end parameters Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.rindex This example shows how to restrict the search range for the substring using the start and end parameters. ```python >>> s = cudf.Series(['abc', 'abb','ab' ,'ddb']) >>> s.str.rindex('b', start=1, end=5) 0 1 1 2 2 1 3 2 dtype: int64 ``` -------------------------------- ### Check for quarter start dates Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.series.DatetimeProperties.is_quarter_start This example demonstrates how to use the is_quarter_start property on a cuDF Series containing datetime objects to identify the first day of a quarter. ```python >>> import pandas as pd, cudf >>> s = cudf.Series( ... pd.date_range(start='2000-09-26', end='2000-10-03', freq='1D')) >>> s 0 2000-09-26 1 2000-09-27 2 2000-09-28 3 2000-09-29 4 2000-09-30 5 2000-10-01 6 2000-10-02 7 2000-10-03 dtype: datetime64[ns] >>> s.dt.is_quarter_start 0 False 1 False 2 False 3 False 4 False 5 True 6 False 7 False dtype: bool ``` -------------------------------- ### Import cuDF and NumPy for missing data examples Source: https://docs.nvidia.com/cudf/latest/cudf/missing-data Sets up the environment for the missing data examples by importing cudf and numpy, and creating a random number generator. ```python import cudf import numpy as np rng = np.random.default_rng() ``` -------------------------------- ### Install cudf nightly wheels Source: https://docs.nvidia.com/cudf/latest/cudf_pandas/benchmarks Install the required cudf nightly wheels based on the detected CUDA version. ```bash CUDA_MAJOR=$(nvidia-smi | grep -oP 'CUDA Version: \K[0-9]+') pip install --extra-index-url https://pypi.anaconda.org/rapidsai-wheels-nightly/simple/ \ "cudf-cu${CUDA_MAJOR}>=0.0.0a0" ``` -------------------------------- ### StringMethods.insert(start, repl) Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.insert Inserts the specified string into each string in the Series or Index at the given position. The start parameter defines the beginning position for insertion, and repl is the string to insert. Returns a new Series or Index with the inserted string. ```APIDOC ## StringMethods.insert(start, repl) ### Description Inserts the specified string into each string in the Series or Index at the given position. The start parameter defines the beginning position for insertion, and repl is the string to insert. Returns a new Series or Index with the inserted string. ### Method insert ### Signature StringMethods.insert(start: int = 0, repl: str | None = None) -> Series | Index ### Parameters - **start** (int) - Optional - Beginning position of the string to replace. Default is beginning of each string. Specify -1 to insert at the end of each string. - **repl** (str) - Optional - String to insert into the specified position value. ### Returns - Series/Index of str dtype - A new string series with the specified string inserted at the specified position. ### Examples ```python >>> import cudf >>> s = cudf.Series(["abcdefghij", "0123456789"]) >>> s.str.insert(2, '_') 0 ab_cdefghij 1 01_23456789 dtype: str ``` When no repl is passed, nothing is inserted. ```python >>> s.str.insert(2) 0 abcdefghij 1 0123456789 dtype: str ``` Negative values are also supported for start. ```python >>> s.str.insert(-1,'_') 0 abcdefghij_ 1 0123456789_ dtype: str ``` ``` -------------------------------- ### Install cudf Source: https://docs.nvidia.com/cudf/latest/cudf_pandas/benchmarks Install the cudf package for CUDA 12. ```bash pip install cudf-cu12 ``` -------------------------------- ### Setup MultiIndex DataFrame Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.swaplevel/index.html Initialize a DataFrame with a MultiIndex to demonstrate level swapping. ```python >>> import cudf >>> midx = cudf.MultiIndex(levels=[['llama', 'cow', 'falcon'], ... ['speed', 'weight', 'length'],['first','second']], ... codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], ... [0, 0, 0, 0, 0, 0, 1, 1, 1]]) >>> cdf = cudf.DataFrame(index=midx, columns=['big', 'small'], ... data=[[45, 30], [200, 100], [1.5, 1], [30, 20], ... [250, 150], [1.5, 0.8], [320, 250], [1, 0.8], [0.3, 0.2]]) ``` -------------------------------- ### Create a Series for head() examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.head/index.html Initializes a cuDF Series with string data for demonstration. ```python >>> ser = cudf.Series(['alligator', 'bee', 'falcon', ... 'lion', 'monkey', 'parrot', 'shark', 'whale', 'zebra']) >>> ser 0 alligator 1 bee 2 falcon 3 lion 4 monkey 5 parrot 6 shark 7 whale 8 zebra dtype: str ``` -------------------------------- ### Setup DataFrame for unstacking Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.unstack/index.html Create a DataFrame with a MultiIndex to demonstrate unstacking operations. ```python >>> df = cudf.DataFrame() >>> df['a'] = [1, 1, 1, 2, 2] >>> df['b'] = [1, 2, 3, 1, 2] >>> df['c'] = [5, 6, 7, 8, 9] >>> df['d'] = ['a', 'b', 'a', 'd', 'e'] >>> df = df.set_index(['a', 'b', 'd']) >>> df c a b d 1 1 a 5 2 b 6 3 a 7 2 1 d 8 2 e 9 ``` -------------------------------- ### Series take example Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.take/index.html Demonstrates selecting rows from a cudf.Series using a list of indices. ```python >>> s = cudf.Series(['a', 'b', 'c', 'd', 'e']) >>> s.take([2, 0, 4, 3]) 2 c 0 a 4 e 3 d dtype: str ``` -------------------------------- ### Initialize cuDF Series Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.match Setup a Series of strings for matching operations. ```python >>> import cudf >>> s = cudf.Series(["rapids", "ai", "cudf"]) ``` -------------------------------- ### DataFrame division setup Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.div/index.html Initialize a DataFrame for division operations. ```python >>> df = cudf.DataFrame( ... {'angles': [0, 3, 4], 'degrees': [360, 180, 360]}, ... index=['circle', 'triangle', 'rectangle'] ... ) ``` -------------------------------- ### DataFrame division example Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.divide/index.html Demonstrates creating a DataFrame and performing division by a scalar. ```python >>> df = cudf.DataFrame( ... {'angles': [0, 3, 4], 'degrees': [360, 180, 360]}, ... index=['circle', 'triangle', 'rectangle'] ... ) ``` ```python >>> df.truediv(1) angles degrees circle 0.0 360.0 triangle 3.0 180.0 rectangle 4.0 360.0 ``` -------------------------------- ### Series division setup Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.div/index.html Initialize two Series with missing values for division. ```python >>> a = cudf.Series([1, 1, 1, None], index=['a', 'b', 'c', 'd']) >>> b = cudf.Series([1, None, 1, None], index=['a', 'b', 'd', 'e']) ``` -------------------------------- ### Index argsort example Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.argsort/index.html Shows how to call argsort on a cudf.Index. ```python >>> import cudf >>> idx = cudf.Index([3, 1, 2]) >>> idx.argsort() array([1, 2, 0]) ``` -------------------------------- ### DataFrame take example Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.take/index.html Demonstrates selecting rows from a cudf.DataFrame using a list of indices. ```python >>> a = cudf.DataFrame({'a': [1.0, 2.0, 3.0], ... 'b': cudf.Series(['a', 'b', 'c'])}) >>> a.take([0, 2, 2]) a b 0 1.0 a 2 3.0 c 2 3.0 c ``` -------------------------------- ### Initialize a Series Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.squeeze/index.html Create a sample Series for demonstration. ```python >>> primes = cudf.Series([2, 3, 5, 7]) ``` -------------------------------- ### DataFrame rmul example Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.rmul/index.html Demonstrates initializing a DataFrame and performing element-wise multiplication using rmul. ```python >>> df = cudf.DataFrame( ... {'angles': [0, 3, 4], 'degrees': [360, 180, 360]}, ... index=['circle', 'triangle', 'rectangle'] ... ) ``` ```python >>> df.rmul(1) angles degrees circle 0 360 triangle 3 180 rectangle 4 360 ``` -------------------------------- ### Building and serving documentation Source: https://docs.nvidia.com/cudf/latest/cudf/developer_guide/documentation Commands to build the documentation using make and serve the generated HTML files locally using a Python HTTP server. ```bash make html ``` ```bash python -m http.server ``` -------------------------------- ### Benchmark result format Source: https://docs.nvidia.com/cudf/latest/cudf_pandas/benchmarks Example of the JSONL output format generated by the benchmark runs. ```json { "engine_name": "cudf-pandas", "query_set": "pdsh", "frontend": "in-memory", "dataset_path": "data/tables/scale-50.0", "scale_factor": 50, "records": { "1": [ {"query": 1, "iteration": 0, "duration": 0.79, "status": "success"}, {"query": 1, "iteration": 1, "duration": 0.55, "status": "success"} ] } } ``` -------------------------------- ### Constructing a DatetimeIndex Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DatetimeIndex/index.html Example of creating a DatetimeIndex from a list of integers with a specified name. ```python >>> import cudf >>> cudf.DatetimeIndex([1, 2, 3, 4], name="a") DatetimeIndex(['1970-01-01 00:00:00.000000001', '1970-01-01 00:00:00.000000002', '1970-01-01 00:00:00.000000003', '1970-01-01 00:00:00.000000004'], dtype='datetime64[ns]', name='a') ``` -------------------------------- ### Setup LocalCUDACluster Source: https://docs.nvidia.com/cudf/latest/cudf/10min Initializes a Dask client with a LocalCUDACluster to distribute computations across GPUs. ```python import time from dask.distributed import Client, wait from dask_cuda import LocalCUDACluster cluster = LocalCUDACluster() client = Client(cluster) ``` -------------------------------- ### StringMethods.startswith(pat) Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.startswith Tests if the start of each string element matches a pattern. Equivalent to str.startswith(). ```APIDOC ## StringMethods.startswith(pat) ### Description Test if the start of each string element matches a pattern. Equivalent to str.startswith(). ### Method startswith ### Signature StringMethods.startswith(pat: str | tuple[str, ...]) -> Series | Index ### Parameters #### pat - Type: str or tuple of str - Required: Yes - Description: String pattern or tuple of patterns. Regular expressions are not accepted. ### Returns - Type: Series or Index of bool - Description: A Series of booleans indicating whether the given pattern matches the start of each string element. ### See Also - endswith: Same as startswith, but tests the end of string. - contains: Tests if string element contains a pattern. ### Examples ```python >>> import cudf >>> s = cudf.Series(['bat', 'Bear', 'cat', None]) >>> s 0 bat 1 Bear 2 cat 3 NaN dtype: str >>> s.str.startswith('b') 0 True 1 False 2 False 3 False dtype: bool ``` ``` -------------------------------- ### Initialize Series for split examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.split Create a sample Series containing strings and a null value to demonstrate splitting operations. ```python >>> import cudf >>> data = ["this is a regular sentence", ... "https://docs.python.org/index.html", None] >>> s = cudf.Series(data) >>> s 0 this is a regular sentence 1 https://docs.python.org/index.html 2 NaN dtype: str ``` -------------------------------- ### Get UTF-8 code points for string series Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.code_points Examples showing how to retrieve UTF-8 code point values for characters in a cudf Series. ```python >>> import cudf >>> s = cudf.Series(["a","xyz", "éee"]) >>> s.str.code_points() 0 97 1 120 2 121 3 122 4 50089 5 101 6 101 dtype: int32 >>> s = cudf.Series(["abc"]) >>> s.str.code_points() 0 97 1 98 2 99 dtype: int32 ``` -------------------------------- ### DataFrame floordiv example Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.floordiv/index.html Demonstrates performing integer division on a DataFrame with a scalar value. ```python >>> df = cudf.DataFrame( ... {'angles': [0, 3, 4], 'degrees': [360, 180, 360]}, ... index=['circle', 'triangle', 'rectangle'] ... ) ``` ```python >>> df.floordiv(1) angles degrees circle 0 360 triangle 3 180 rectangle 4 360 ``` -------------------------------- ### Initialize DataFrame for set_index examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.set_index/index.html Creates a sample DataFrame with columns 'a', 'b', and 'c' to be used in subsequent indexing operations. ```python >>> df = cudf.DataFrame({ ... "a": [1, 2, 3, 4, 5], ... "b": ["a", "b", "c", "d","e"], ... "c": [1.0, 2.0, 3.0, 4.0, 5.0] ... }) >>> df a b c 0 1 a 1.0 1 2 b 2.0 2 3 c 3.0 3 4 d 4.0 4 5 e 5.0 ``` -------------------------------- ### Create a DataFrame for nsmallest examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.nsmallest/index.html Initializes a sample DataFrame with population, GDP, and alpha-2 columns to demonstrate sorting operations. ```python >>> import cudf >>> df = cudf.DataFrame({'population': [59000000, 65000000, 434000, ... 434000, 434000, 337000, 337000, ... 11300, 11300], ... 'GDP': [1937894, 2583560 , 12011, 4520, 12128, ... 17036, 182, 38, 311], ... 'alpha-2': ["IT", "FR", "MT", "MV", "BN", ... "IS", "NR", "TV", "AI"]}, ... index=["Italy", "France", "Malta", ... "Maldives", "Brunei", "Iceland", ... "Nauru", "Tuvalu", "Anguilla"]) >>> df population GDP alpha-2 Italy 59000000 1937894 IT France 65000000 2583560 FR Malta 434000 12011 MT Maldives 434000 4520 MV Brunei 434000 12128 BN Iceland 337000 17036 IS Nauru 337000 182 NR Tuvalu 11300 38 TV Anguilla 11300 311 AI ``` -------------------------------- ### Extracting substrings with slice_from Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.slice_from This example demonstrates how to use slice_from with two Series objects defining the start and stop indices for each string in the target Series. ```python >>> import cudf >>> s = cudf.Series(["hello","there"]) >>> s 0 hello 1 there dtype: str >>> starts = cudf.Series([1, 3]) >>> stops = cudf.Series([5, 5]) >>> s.str.slice_from(starts, stops) 0 ello 1 re dtype: str ``` -------------------------------- ### Tiling a DataFrame Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.tile/index.html This example demonstrates how to use the tile method to repeat the rows of a DataFrame twice. ```python >>> import cudf >>> df = cudf.DataFrame([[8, 4, 7], [5, 2, 3]]) >>> count = 2 >>> df.tile(count) 0 1 2 0 8 4 7 1 5 2 3 0 8 4 7 1 5 2 3 ``` -------------------------------- ### Initial Setup for cuDF UDFs Source: https://docs.nvidia.com/cudf/latest/cudf/guide-to-udfs Basic imports and configuration to suppress CUDA low occupancy warnings. ```python import cudf import numpy as np from cudf.datasets import randomdata from numba import config config.CUDA_LOW_OCCUPANCY_WARNINGS = 0 ``` -------------------------------- ### Convert StructDtype to pyarrow.StructType Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.dtypes.StructDtype.to_arrow This example demonstrates how to initialize a StructDtype with specific fields and convert it to a pyarrow.StructType. ```python >>> import cudf >>> struct_type = cudf.StructDtype({"x": "int32", "y": "float64"}) >>> struct_type StructDtype({'x': dtype('int32'), 'y': dtype('float64')}) >>> struct_type.to_arrow() StructType(struct) ``` -------------------------------- ### Initialize DataFrame for isin examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.isin/index.html Sets up a sample DataFrame with 'num_legs' and 'num_wings' columns for subsequent isin operations. ```python >>> import cudf >>> df = cudf.DataFrame({'num_legs': [2, 4], 'num_wings': [2, 0]}, ... index=['falcon', 'dog']) >>> df num_legs num_wings falcon 2 2 dog 4 0 ``` -------------------------------- ### Get days in month for a datetime Series Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.series.DatetimeProperties.days_in_month This example demonstrates how to create a datetime Series using pandas date_range and then retrieve the number of days in the month for each entry using the .dt.days_in_month accessor. ```python >>> import pandas as pd, cudf >>> s = cudf.Series( ... pd.date_range(start='2000-08-01', end='2001-08-01', freq='1M')) >>> s 0 2000-08-31 1 2000-09-30 2 2000-10-31 3 2000-11-30 4 2000-12-31 5 2001-01-31 6 2001-02-28 7 2001-03-31 8 2001-04-30 9 2001-05-31 10 2001-06-30 11 2001-07-31 dtype: datetime64[ns] >>> s.dt.days_in_month 0 31 1 30 2 31 3 30 4 31 5 31 6 28 7 31 8 30 9 31 10 30 11 31 dtype: int16 ``` -------------------------------- ### Describe all available options Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/options Use cudf.describe_option() without arguments to print descriptions for all available configuration options. ```python In [1]: import cudf In [2]: cudf.describe_option() default_integer_bitwidth: Default bitwidth when the dtype of an integer needs to be inferred. If set to `None`, the API will align dtype with pandas. APIs that respect this option include: - cudf object constructors - cudf.read_csv and cudf.read_json when `dtype` is not specified. - APIs that require implicit conversion of cudf.RangeIndex to an integer index. Valid values are None, 32 or 64. Default is None. [Default: None] [Current: None] default_float_bitwidth: Default bitwidth when the dtype of a float needs to be inferred. If set to `None`, the API will align dtype with pandas. APIs that respect this option include: - cudf object constructors - cudf.read_csv and cudf.read_json when `dtype` is not specified. Valid values are None, 32 or 64. Default is None. [Default: None] [Current: None] spill: Enables spilling. Valid values are True or False. Default is False. [Default: False] [Current: False] spill_on_demand: Enables spilling on demand using an RMM out-of-memory error handler. This has no effect if spilling is disabled, see the "spill" option. Valid values are True or False. Default is True. [Default: True] [Current: True] spill_device_limit: Enforce a device memory limit in bytes. This has no effect if spilling is disabled, see the "spill" option. Valid values are any positive integer or None (disabled). Default is None. [Default: None] [Current: None] spill_stats: If not 0, enables statistics at the specified level: 0 - disabled (no overhead). 1+ - duration and number of bytes spilled (very low overhead). 2+ - a traceback for each time a spillable buffer is exposed permanently (potential high overhead). Valid values are any positive integer. Default is 0 (disabled). [Default: 0] [Current: 0] mode.pandas_compatible: If set to `False`, retains `cudf` specific behavior. If set to `True`, enables pandas compatibility mode, which will try to match pandas API behaviors in case of any inconsistency. Valid values are True or False. Default is False. [Default: False] [Current: False] memory_profiling: If set to `False`, disables memory profiling. If set to `True`, enables memory profiling. Read more at: :ref:`memory-profiling-user-doc` Valid values are True or False. Default is False. [Default: False] [Current: False] io.parquet.low_memory: If set to `False`, reads entire parquet in one go. If set to `True`, reads parquet file in chunks. Valid values are True or False. Default is False. [Default: False] [Current: False] io.json.low_memory: If set to `False`, reads entire json in one go. If set to `True`, reads json file in chunks. Valid values are True or False. Default is False. [Default: False] [Current: False] kvikio_remote_io: Whether to use KvikIO's remote IO backend or not. WARN: this is experimental and may be removed at any time without warning or deprecation period. Set KVIKIO_NTHREADS (default is 8) to change the number of concurrent tcp connections, which is important for good performance. Valid values are True or False. Default is False. [Default: False] [Current: False] ``` -------------------------------- ### Numpydoc style docstring example Source: https://docs.nvidia.com/cudf/latest/cudf/developer_guide/documentation An example of a class and method docstring following the numpydoc style, including parameters, return values, exceptions, and examples. ```python class A: """Brief description of A. Longer description of A. Parameters ---------- x : int Description of x, the first constructor parameter. """ def __init__(self, x: int): pass def foo(self, bar: str): """Short description of foo. Longer description of foo. Parameters ---------- bar : str Description of bar. Returns ------- float Description of the return value of foo. Raises ------ ValueError Explanation of when a ValueError is raised. In this case, a ValueError is raised if bar is "fail". Examples -------- The examples section is _strongly_ encouraged. Where appropriate, it may mimic the examples for the corresponding pandas API. >>> a = A() >>> a.foo('baz') 0.0 >>> a.foo('fail') ... ValueError: Failed! """ if bar == "fail": raise ValueError("Failed!") return 0.0 ``` -------------------------------- ### Initialize DataFrame for renaming examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.rename/index.html Create a sample DataFrame with columns 'A' and 'B' to demonstrate renaming operations. ```python >>> import cudf >>> df = cudf.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df A B 0 1 4 1 2 5 2 3 6 ``` -------------------------------- ### Construct an IntervalIndex from breaks Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.IntervalIndex.from_breaks This example demonstrates how to create an IntervalIndex using a list of integers as breaks. ```python >>> import cudf >>> import pandas as pd >>> cudf.IntervalIndex.from_breaks([0, 1, 2, 3]) IntervalIndex([(0, 1], (1, 2], (2, 3]], dtype='interval[int64, right]') ``` -------------------------------- ### Match strings starting with 'a' Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.match Check if each string in the Series starts with the character 'a'. ```python >>> s.str.match('a') 0 False 1 True 2 False dtype: bool ``` -------------------------------- ### Install cudf-polars with conda Source: https://docs.nvidia.com/cudf/latest/cudf_polars Install the cudf-polars package using conda from the rapidsai, conda-forge, and nvidia channels. ```bash conda install -c rapidsai -c conda-forge -c nvidia cudf-polars ``` -------------------------------- ### Import libraries and check version Source: https://docs.nvidia.com/cudf/latest/cudf/performance-comparisons/performance-comparisons Initializes the environment by importing necessary libraries and printing the cuDF version. ```python import timeit import cudf import matplotlib.pyplot as plt import numpy as np import pandas as pd print(f"{cudf.__version__=}") ``` -------------------------------- ### Initialize Series for findall examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.findall Sets up a cudf Series containing strings to be used in subsequent findall operations. ```python >>> import cudf >>> s = cudf.Series(['Lion', 'Monkey', 'Rabbit']) ``` -------------------------------- ### Replace from start index to end Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.slice_replace Specify only the start index to replace the substring from that position until the end of each string. ```python >>> s.str.slice_replace(1, repl='X') 0 aX 1 aX 2 aX 3 aX 4 aX dtype: str ``` -------------------------------- ### Initialize a DataFrame for rounding Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.round/index.html Sets up a sample DataFrame with two columns to demonstrate rounding operations. ```python >>> df = cudf.DataFrame( ... [(.21, .32), (.01, .67), (.66, .03), (.21, .18)], ... columns=['dogs', 'cats'], ... ) >>> df dogs cats 0 0.21 0.32 1 0.01 0.67 2 0.66 0.03 3 0.21 0.18 ``` -------------------------------- ### Find substring with start and end parameters Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.find Shows how to restrict the search range using the start and end parameters. ```python >>> s.str.find('b', start=1, end=5) 0 1 1 -1 2 -1 3 2 dtype: int64 ``` -------------------------------- ### Initialize Series for replacement examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.replace Sets up a cuDF Series containing string data and a null value to demonstrate replacement operations. ```python >>> import cudf >>> s = cudf.Series(['foo', 'fuz', None]) >>> s 0 foo 1 fuz 2 NaN dtype: str ``` -------------------------------- ### StringMethods.slice_from(starts, stops) Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.slice_from Returns a substring of each string using positions for each string. The starts and stops parameters are of Series type. ```APIDOC ## StringMethods.slice_from(starts, stops) ### Description Return substring of each string using positions for each string. The starts and stops parameters are of Series type. ### Method slice_from ### Signature StringMethods.slice_from(starts: Series, stops: Series) -> Series | Index ### Parameters - **starts** (Series) - Required - Beginning position of each string to extract. Default is beginning of each string. - **stops** (Series) - Required - Ending position of each string to extract. Default is end of each string. Use -1 to specify to the end of that string. ### Returns - **Series/Index of str dtype** - A substring of each string using positions for each string. ### Example ```python >>> import cudf >>> s = cudf.Series(["hello","there"]) >>> s 0 hello 1 there dtype: str >>> starts = cudf.Series([1, 3]) >>> stops = cudf.Series([5, 5]) >>> s.str.slice_from(starts, stops) 0 ello 1 re dtype: str ``` ``` -------------------------------- ### Using start and end parameters with rfind Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.rfind Shows how to constrain the search range for the substring using the start and end parameters. ```python >>> s.str.rfind('a', start=2, end=5) 0 -1 1 -1 2 -1 dtype: int64 ``` -------------------------------- ### Match strings starting with 'a' or 'c' Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.match Check if each string in the Series starts with either 'a' or 'c' using a regex character class. ```python >>> s.str.match('[ac]') 0 False 1 True 2 True dtype: bool ``` -------------------------------- ### Using StringMethods.center to pad strings Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.center Examples showing how to center strings in a cuDF Series with different widths and fill characters. ```python >>> import cudf >>> s = cudf.Series(['a', 'b', None, 'd']) >>> s.str.center(1) 0 a 1 b 2 NaN 3 d dtype: str >>> s.str.center(1, fillchar='-') 0 a 1 b 2 NaN 3 d dtype: str >>> s.str.center(2, fillchar='-') 0 a- 1 b- 2 NaN 3 d- dtype: str >>> s.str.center(5, fillchar='-') 0 --a-- 1 --b-- 2 NaN 3 --d-- dtype: str >>> s.str.center(6, fillchar='-') 0 --a--- 1 --b--- 2 NaN 3 --d--- dtype: str ``` -------------------------------- ### Unsupported UDF operation example Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.apply/index.html This example demonstrates a UDF that triggers an AttributeError because it attempts to use the .upper() method on a string column, which is not supported. ```python >>> def f(row): ... st = row['str_col'].upper() ... return 'ABC' in st >>> df.apply(f, axis=1) ``` -------------------------------- ### Install cudf-polars with pip Source: https://docs.nvidia.com/cudf/latest/cudf_polars Install the cudf-polars package using pip. Use the cu13 suffix for CUDA 13 or cu12 for CUDA 12. ```bash pip install cudf-polars-cu13 ``` -------------------------------- ### Create virtual environment for pandas Source: https://docs.nvidia.com/cudf/latest/cudf_pandas/benchmarks Create a virtual environment for the pandas benchmark dependencies. ```bash virtualenv pandas/py-pandas ``` -------------------------------- ### Create a cuDF DataFrame for iloc examples Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.iloc/index.html Creates a DataFrame with columns a, b, and c, each containing values from 0 to 19. This DataFrame is used in the subsequent iloc selection examples. ```python >>> df = cudf.DataFrame({'a': range(20), ... 'b': range(20), ... 'c': range(20)}) ``` -------------------------------- ### Initialize DataFrame for conversion Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.convert_dtypes/index.html Create a DataFrame with various dtypes to demonstrate the conversion process. ```python >>> import cudf >>> import numpy as np >>> df = cudf.DataFrame( ... { ... "a": cudf.Series([1, 2, 3], dtype=np.dtype("int32")), ... "b": cudf.Series(["x", "y", "z"], dtype=np.dtype("O")), ... "e": cudf.Series([10, np.nan, 20], dtype=np.dtype("float")), ... "f": cudf.Series([np.nan, 100.5, 200], dtype=np.dtype("float")), ... } ... ) ``` -------------------------------- ### DataFrame.keys() Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/dataframe Get the columns. ```APIDOC ## DataFrame.keys() ### Description Get the columns. ### Method keys ### Returns Index of column labels. ``` -------------------------------- ### Series.name Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/series Get the name of this object. ```APIDOC ## Series.name ### Description Get the name of this object. ``` -------------------------------- ### Build DataFrame with __setitem__ Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame/index.html Demonstrates creating an empty DataFrame and adding columns using the __setitem__ syntax. ```python >>> import cudf >>> df = cudf.DataFrame() >>> df['key'] = [0, 1, 2, 3, 4] >>> df['val'] = [float(i + 10) for i in range(5)] # insert column >>> df key val 0 0 10.0 1 1 11.0 2 2 12.0 3 3 13.0 4 4 14.0 ``` -------------------------------- ### Series.index Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/series Get the labels for the rows. ```APIDOC ## Series.index ### Description Get the labels for the rows. ``` -------------------------------- ### Sample with replacement from a Series Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.sample/index.html Demonstrates sampling 10 items from a Series with replacement enabled. ```python >>> sr = cudf.Series([1, 2, 3, 4, 5]) >>> sr.sample(10, replace=True, random_state=0) 1 2 4 5 1 2 2 3 4 5 1 2 4 5 2 3 4 5 1 2 dtype: int64 ``` -------------------------------- ### Series.dtype Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/series Get the data type of the Series. ```APIDOC ## Series.dtype ### Description Get the data type of the Series. ``` -------------------------------- ### cudf.get_option Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/options Get the value of the specified option. ```APIDOC ## cudf.get_option(name) ### Description Get the value of the specified option. ### Parameters - **name** (str) - Required - The name of the option to retrieve. ### Returns The current value of the option. ``` -------------------------------- ### Write a dataframe to csv Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.to_csv/index.html This example demonstrates how to create a DataFrame, set an index, and export the data to a CSV file named 'foo.csv'. ```python >>> import cudf >>> filename = 'foo.csv' >>> df = cudf.DataFrame({'x': [0, 1, 2, 3], ... 'y': [1.0, 3.3, 2.2, 4.4], ... 'z': ['a', 'b', 'c', 'd']}) >>> df = df.set_index(cudf.Series([3, 2, 1, 0])) >>> df.to_csv(filename) ``` -------------------------------- ### DataFrameGroupBy.diff Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/groupby Gets the difference between the values in each group. ```APIDOC ## DataFrameGroupBy.diff ### Description Get the difference between the values in each group. ### Method DataFrameGroupBy.diff ### Parameters - periods (int, optional) - Periods to shift for calculating difference. - axis (int, optional) - Take difference over rows (0) or columns (1). ### Returns - Series or DataFrame - First differences of the series. ``` -------------------------------- ### Padding strings with rjust Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.rjust Examples showing how to pad strings to a specific width using a custom fill character, and how the method behaves when the string length is already equal to or greater than the specified width. ```python >>> import cudf >>> s = cudf.Series(["hello world", "rapids ai"]) >>> s.str.rjust(20, fillchar="_") 0 _________hello world 1 ___________rapids ai dtype: str >>> s = cudf.Series(["a", "", "ab", "__"]) >>> s.str.rjust(1, fillchar="-") 0 a 1 - 2 ab 3 __ dtype: str ``` -------------------------------- ### Shallow versus deep copy initialization Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.DataFrame.copy/index.html Shows how to initialize deep and shallow copies of a Series. ```python >>> s = cudf.Series([1, 2], index=["a", "b"]) >>> deep = s.copy() >>> shallow = s.copy(deep=False) ``` -------------------------------- ### Compute Minhash for Strings and Lists Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/api/cudf.core.accessors.string.StringMethods.minhash This example demonstrates how to use minhash on a Series of strings and a Series of lists of strings, providing the necessary seed, hash coefficients, and width parameters. ```python >>> import cudf >>> import numpy as np >>> s = cudf.Series(['this is my', 'favorite book']) >>> a = cudf.Series([1, 2, 3], dtype=np.uint32) >>> b = cudf.Series([4, 5, 6], dtype=np.uint32) >>> s.str.minhash(0, a=a, b=b, width=5) 0 [1305480171, 462824409, 74608232] 1 [32665388, 65330773, 97996158] dtype: list >>> sl = cudf.Series([['this', 'is', 'my'], ['favorite', 'book']]) >>> sl.str.minhash(width=2, seed=0, a=a, b=b) 0 [416367551, 832735099, 1249102647] 1 [1906668704, 3813337405, 1425038810] dtype: list ``` -------------------------------- ### Series.shape Source: https://docs.nvidia.com/cudf/latest/cudf/api_docs/series Get a tuple representing the dimensionality of the Index. ```APIDOC ## Series.shape ### Description Get a tuple representing the dimensionality of the Index. ```