[FEA] Support 'normalize' parameter in cudf.crosstab
- 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 code to Pandas, I have noticed that `normalize` parameter is not supported in `cudf.crosstab`.
**Describe the solution you'd like**
I would like to have `normalize` parameter supported. Thanks!!!
**Describe alternatives you've considered**
I have performed the normalization by hand. While doing it, I have also found another feature missing. Please see issue https://github.com/rapidsai/cudf/issues/11894
**Additional context**
Please, find below the code that fails, and the workaround I have implemented:
```python
import cudf
import pandas as pd
print(cudf.__version__, pd.__version__, '\n')
features = {'x': ['x1', 'x1', 'x2', 'x2', 'x2', 'x1', 'x2'],
'y': ['y1', 'y2', 'y1', 'y2', 'y3', 'y1', 'y3']}
pdf = pd.DataFrame(features)
pdf = pd.crosstab(index = pdf.x, columns = pdf.y, normalize='index')
gdf = cudf.DataFrame(features)
gdf = cudf.crosstab(index = gdf.x, columns = gdf.y, normalize='index')
# The code above fails as follows:
# ---------------------------------------------------------------------------
# NotImplementedError Traceback (most recent call last)
# /tmp/ipykernel_1281/4238469792.py in
# 1 gdf = cudf.DataFrame(features)
# ----> 2 gdf = cudf.crosstab(index = gdf.x, columns = gdf.y, normalize='index')
# /opt/conda/envs/rapids/lib/python3.9/site-packages/cudf/core/reshape.py in crosstab(index, columns, values, rownames, colnames, aggfunc, margins, margins_name, dropna, normalize)
# 1252 """
# 1253 if normalize is not False:
# -> 1254 raise NotImplementedError("normalize is not supported yet")
# 1255
# 1256 if values is None and aggfunc is not None:
# NotImplementedError: normalize is not supported yet
gdf = cudf.crosstab(index = gdf.x, columns = gdf.y)
# The following code workarounds feature request https://github.com/rapidsai/cudf/issues/11894
tmp = gdf.sum(axis=1)
for col in gdf.columns:
gdf[col] = gdf[col]/tmp
print(pdf, '\n', gdf)
```
Contributor guide
Assessment
This issue has not been assessed yet.