NVIDIA / NVIDIA/cudf

[FEA] Support DataFrame.div() in axis=0

Open
#11,894 0 comments 0 reactions 0 assignees View on GitHub
feature request Python
Dominant language
C++
Stars
9.8k
Forks
1.1k
Avg merge
3d 6m
Merged PRs (30d)
278

Description

**Is your feature request related to a problem? Please describe.**
Hi!

While porting some Pandas code to cuDF, I have been making use of the new `cudf.crosstab()` feature. Unfortunately, the parameter `normalize='index'` is not implemented yet, so I decided to perform the normalization by index by hand.

I had planned to do it as follows, but `DataFrame.div()` is not supported in `axis = 0`:

```python
gdf = gdf.div(gdf.sum(axis=1), axis=0)

---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
/tmp/ipykernel_5103/715192400.py in
----> 1 gdf = gdf.div(gdf.sum(axis=1), axis=0)

/opt/conda/envs/rapids/lib/python3.9/site-packages/cudf/core/dataframe.py in wrapper(self, other, axis, level, fill_value)
6887 def wrapper(self, other, axis="columns", level=None, fill_value=None):
6888 if axis not in (1, "columns"):
-> 6889 raise NotImplementedError("Only axis=1 supported at this time.")
6890 output = wrapped_func(self, other, axis, level, fill_value)
6891 if postprocess is None:

NotImplementedError: Only axis=1 supported at this time.

```

Therefore, I have performed the following workaround:

```python
tmp = gdf.sum(axis=1)
for col in gdf.columns:
gdf[col] = gdf[col]/tmp
```

**Describe the solution you'd like**
I would like that `DataFrame.div()` would support `axis=0`. Thanks!!!!

**Additional context**
Please, find below a reproducer:

```python
import cudf
import pandas as pd

print(cudf.__version__, pd.__version__, '\n')

features = {'y1': [2, 1],
'y2': [1, 1],
'y3': [0, 2]}

pdf = pd.DataFrame(features)
gdf = cudf.DataFrame(features)

pdf = pdf.div(pdf.sum(axis=1), axis=0)

gdf = gdf.div(gdf.sum(axis=1), axis=0)
#
# The code above fails as follows:
# ---------------------------------------------------------------------------
# NotImplementedError Traceback (most recent call last)
# /tmp/ipykernel_5103/715192400.py in
# ----> 1 gdf = gdf.div(gdf.sum(axis=1), axis=0)

# /opt/conda/envs/rapids/lib/python3.9/site-packages/cudf/core/dataframe.py in wrapper(self, other, axis, level, fill_value)
# 6887 def wrapper(self, other, axis="columns", level=None, fill_value=None):
# 6888 if axis not in (1, "columns"):
# -> 6889 raise NotImplementedError("Only axis=1 supported at this time.")
# 6890 output = wrapped_func(self, other, axis, level, fill_value)
# 6891 if postprocess is None:

# NotImplementedError: Only axis=1 supported at this time.

tmp = gdf.sum(axis=1)
for col in gdf.columns:
gdf[col] = gdf[col]/tmp

print(gdf.to_pandas().equals(pdf))
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.