Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a section to the docs that compares cuDF with Pandas #10796

Merged
merged 15 commits into from
May 16, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions docs/cudf/source/user_guide/pandas-comparison.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Comparison of cuDF and Pandas
shwina marked this conversation as resolved.
Show resolved Hide resolved

cuDF is a DataFrame library that closely matches the Pandas API, but
leverages NVIDIA GPUs for performing computations for speed. However,
shwina marked this conversation as resolved.
Show resolved Hide resolved
there are some differences between cuDF and Pandas, both in terms of API
and behavior. This page documents the similarities and differences
between cuDF and Pandas.

## Supported operations

cuDF supports many of the same data structures and operations as
Pandas. This includes `Series`, `DataFrame`, `Index` and
operations on them such as unary and binary operations, indexing,
filtering, concatenating, joining, groupby and window operations -
among many others.

The best way to check if we support a particular Pandas API is to search
our [API docs](/api_docs/index).
shwina marked this conversation as resolved.
Show resolved Hide resolved

## Data types

cuDF supports many of the commonly-used data types in Pandas,
including numeric, datetime, timestamp, string, and categorical data
types. In addition, we support special data types for decimal, list
shwina marked this conversation as resolved.
Show resolved Hide resolved
and "struct" values. See the section on [Data Types](data-types) for
details.

Note that we do not support custom data types like Pandas'
`ExtensionDtype`.

## Null (or "missing") values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a great place to call out the subtle differences in null handling logic we have vs pandas. Most of it can be dug up from the source code here but a good summary might be something like this (I think this is all of them?)

Nulls in cuDF behave differently from pandas in several edge cases. In cuDF, the rule is that nulls always propagate, whereas in pandas they may not if the mathematical result can be inferred without knowing the missing value: 
- `NA ** 0 == 1`
- `1 ** NA == 1`
- `NA | True == True`
- `True or NA == True`
- `False and NA == False`

Maybe a table or something might be better than this.

Copy link
Contributor

@bdice bdice May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these cases are also described in the docs (as a cross-reference with the source code linked above):

I find it a little concerning that we differ in this way because it means that cuDF cannot be consistent in its behaviors between scalars and columns. It should be specifically noted that scalar operations act like Pandas (because we use the same magic NA singleton object), and column operations always propagate NA.

>>> import cudf
>>> cudf.NA ** 0
1
>>> cudf.Scalar(cudf.NA, dtype=float) ** 0
Scalar(1.0, dtype=float64)
>>> cudf.Series([cudf.NA], dtype=float) ** 0
0    <NA>
dtype: float64

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the difference in column vs scalar behaviour is problematic. I think @brandon-b-miller has thought a lot about this, where maybe we should take this discussion offline and come back and raise a separate issue if needed.

For this PR, I'll hold off on adding any further information about null behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend thoroughly reading the discussion on pandas-dev/pandas#29997 before we relitigate any of that discussion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our discussions offline, I'm going to hold off on documenting the exceptional cases here. I think our priority should be to first align the behavior of nulls in all three of the following cases:

  • Scalar operations involving NA
  • Column operations involving NA
  • Operations in UDFs involving NA

We can choose to always return NA in all three cases, or make an exception for ** in all three cases, but we must be consistent. That done, we can come back here to document the difference from Pandas - if any.


Unlike Pandas, *all* data types in cuDF are nullable,
meaning they can contain missing values (represented by `cudf.NA`).

```{code} python
>>> s = cudf.Series([1, 2, cudf.NA])
>>> s
>>> s
shwina marked this conversation as resolved.
Show resolved Hide resolved
0 1
1 2
2 <NA>
dtype: int64
```

Nulls are not coerced to `nan` in any situation;
shwina marked this conversation as resolved.
Show resolved Hide resolved
compare the behaviour of cuDF with Pandas below:
shwina marked this conversation as resolved.
Show resolved Hide resolved

```{code} python
>>> s = cudf.Series([1, 2, cudf.NA], dtype="category")
>>> s
0 1
1 2
2 <NA>
dtype: category
Categories (2, int64): [1, 2]

>>> s = pd.Series([1, 2, pd.NA], dtype="category")
>>> s
0 1
1 2
2 NaN
dtype: category
Categories (2, int64): [1, 2]
```

See the docs on [missing data](missing-data) for
details.
shwina marked this conversation as resolved.
Show resolved Hide resolved

## Iteration

Iterating over a cuDF `Series`, `DataFrame` or `Index` is not
supported. This is because iterating over data that resides on the GPU
will yield *extremely* poor performance, as GPUs are optimized for
highly parallel operations rather than sequential operations.

In the vast majority of cases, it is possible to avoid iteration and
use an existing function or method to accomplish the same task. If you
absolutely must iterate, copy the data from GPU to CPU by using
`.to_arrow()` or `.to_pandas()`, then copy the result back to GPU
using `.from_arrow()` or `.from_pandas()`.

## Result ordering
shwina marked this conversation as resolved.
Show resolved Hide resolved

By default, `join` (or `merge`) and `groupby` operations in cuDF
do *not* guarantee output ordering by default.
shwina marked this conversation as resolved.
Show resolved Hide resolved
Compare the results obtained from Pandas and cuDF below:

```{code} python
>>> import cupy as cp
>>> df = cudf.DataFrame({'a': cp.random.randint(0, 1000, 1000), 'b': range(1000)})
>>> df.groupby("a").mean().head()
b
a
742 694.5
29 840.0
459 525.5
442 363.0
666 7.0
>>> df.to_pandas().groupby("a").mean().head()
b
a
2 643.75
6 48.00
7 631.00
9 906.00
10 640.00
```

To match Pandas behavior, you must explicitly pass `sort=True`:

```{code} python
>>> df.to_pandas().groupby("a", sort=True).mean().head()
b
a
2 643.75
6 48.00
7 631.00
9 906.00
10 640.00
```

## Column names

Unlike Pandas, cuDF does not support duplicate column names.
It is best to use strings for column names.

## No true `"object"` data type

In Pandas and NumPy, the `"object"` data type is used for
shwina marked this conversation as resolved.
Show resolved Hide resolved
collections of arbitrary Python objects. For example, in Pandas you
can do the following:

```{code} python
>>> import pandas as pd
>>> s = pd.Series(["a", 1, [1, 2, 3]])
0 a
1 1
2 [1, 2, 3]
dtype: object
```

For compatibilty with Pandas, cuDF reports the data type for strings
as `"object"`, but we do *not* support storing or operating on
collections of arbitrary Python objects.

## `.apply()` function limitations

The `.apply()` function in Pandas accecpts a user-defined function
shwina marked this conversation as resolved.
Show resolved Hide resolved
(UDF) that can include arbitrary operations that are applied to each
value of a `Series`, `DataFrame`, or in the case of a groupby,
each group. cuDF also supports `apply()`, but it relies on Numba to
shwina marked this conversation as resolved.
Show resolved Hide resolved
JIT compile the UDF and execute it on the GPU. This can be extremely
fast, but imposes a few limitations on what operations are allowed in
the UDF. See the docs on [UDFs](guide-to-udfs) for details.