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

[FEA]use cuDF dataframe cannot use unstack() function #6694

Closed
dominicshanshan opened this issue Nov 6, 2020 · 3 comments · Fixed by #7054
Closed

[FEA]use cuDF dataframe cannot use unstack() function #6694

dominicshanshan opened this issue Nov 6, 2020 · 3 comments · Fixed by #7054
Assignees
Labels
bug Something isn't working Python Affects Python cuDF API.

Comments

@dominicshanshan
Copy link
Contributor

dominicshanshan commented Nov 6, 2020

Describe the bug
cuDF dataframe not support .unstack() function

Steps/Code to reproduce bug

issue_test_df = cudf.DataFrame({'0':[1,1,1,2,2,3,3,3,3],
                                '1':[1,2,3,1,2,1,2,3,4],
                                '2':[0.2,0.2,0.3,0.3,0.4,0.5,0.6,0.7,0.8]})
issue_test_df.unstack()

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-63-3fff8526a571> in <module>
----> 1 issue_test_df.unstack()

/opt/conda/envs/rapids/lib/python3.7/site-packages/cudf/core/dataframe.py in unstack(self, level, fill_value)
   7061     def unstack(self, level=-1, fill_value=None):
   7062         return cudf.core.reshape.unstack(
-> 7063             self, level=level, fill_value=fill_value
   7064         )
   7065 

/opt/conda/envs/rapids/lib/python3.7/site-packages/cudf/core/reshape.py in unstack(df, level, fill_value)
    897     if not isinstance(df.index, cudf.MultiIndex):
    898         raise NotImplementedError(
--> 899             "Calling unstack() on a DataFrame without a MultiIndex "
    900             "is not supported"
    901         )

NotImplementedError: Calling unstack() on a DataFrame without a MultiIndex is not supported

Expected behavior
Like pandas.DataFrame().unstack(), it should output the unstack table

Environment overview (please complete the following information)

  • Environment location: Docker
  • Method of cuDF install: Docker
    • If method of install is [Docker], provide docker pull & docker run commands used
      sudo docker run --gpus all -it -v /data/project/demo/:/rapids/notebooks/demo/ -p 8889:8888 nvcr.io/nvidia/rapidsai/rapidsai:cuda11.0-runtime-ubuntu18.04

Please let me know if this is enough to reproduce the issue, thanks !

@dominicshanshan dominicshanshan added Needs Triage Need team to review and classify bug Something isn't working labels Nov 6, 2020
@kkraus14 kkraus14 added Python Affects Python cuDF API. and removed Needs Triage Need team to review and classify labels Nov 9, 2020
@taureandyernv
Copy link
Contributor

@dominicshanshan Thanks for bringing this to our attention. This seems more like a feature request than a bug as the caught error states that this feature isn't implemented. Can you change the issue from a bug [BUG] to a feature request [FEA]?

898 raise NotImplementedError(
--> 899 "Calling unstack() on a DataFrame without a MultiIndex "
900 "is not supported"

It does seem like we didn't quite reach full equivalence to pandas as hoped for in the closed issue #1821. Should we reopen it @kkraus14?

Following the pandas example from https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.unstack.html,

Pandas attempt:

import pandas as pd
import numpy as np
index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'),
                                   ('two', 'a'), ('two', 'b')])

# FYI, cuDF unstack() doesn't support Series, so I used switched both to DataFrame
s = pd.DataFrame(np.arange(1.0, 5.0), index=index)
print(s)
print(s.unstack(level=-1))
print(s.unstack(level=0))
df = s.unstack(level=0)
print(df.unstack()) 

Successfully outputs

         0
one a  1.0
    b  2.0
two a  3.0
    b  4.0

       0     
       a    b
one  1.0  2.0
two  3.0  4.0

     0     
   one  two
a  1.0  3.0
b  2.0  4.0

0  one  a    1.0
        b    2.0
   two  a    3.0
        b    4.0
dtype: float64
import cudf
import numpy as np
index = cudf.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'),
                                   ('two', 'a'), ('two', 'b')])

gs = cudf.DataFrame(np.arange(1.0, 5.0), index=index)
print(gs)
print(gs.unstack(level=-1))
print(gs.unstack(level=0))
df = gs.unstack(level=0)
print(df.unstack()) #throws error

Outputs

         0
one a  1.0
    b  2.0
two a  3.0
    b  4.0

       0     
       a    b
0            
one  1.0  2.0
two  3.0  4.0

     0     
   one  two
1          
a  1.0  3.0
b  2.0  4.0

NotImplementedError                       
Traceback (most recent call last)

...

NotImplementedError: Calling unstack() on a DataFrame without a MultiIndex is not supported

@dominicshanshan dominicshanshan changed the title [BUG]use cuDF dataframe cannot use unstack() function [FEA]use cuDF dataframe cannot use unstack() function Nov 13, 2020
@dominicshanshan
Copy link
Contributor Author

@taureandyernv , as you request, changed label from BUG -> FEA

@isVoid
Copy link
Contributor

isVoid commented Dec 29, 2020

This should be a workaround for now:

>>> df = cudf.DataFrame({'B': [1, 3, 5],
...                    'C': [2, 4, 6]})
>>> df.T.stack(dropna=False)
0  1
B  0    1
   1    3
   2    5
C  0    2
   1    4
   2    6
dtype: int64

Note that cudf requires all columns to have the same datatype to complete the transpose. Otherwise some type-casts may be needed.

@isVoid isVoid self-assigned this Dec 30, 2020
@rapids-bot rapids-bot bot closed this as completed in #7054 Jan 6, 2021
rapids-bot bot pushed a commit that referenced this issue Jan 6, 2021
Closes #6694 

When `unstack()` receives a dataframe with "single" index, returns a series to match pandas behavior.

Authors:
  - Michael Wang <[email protected]>

Approvers:
  - null

URL: #7054
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Python Affects Python cuDF API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants