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

bug: names_sort argument in table.pivot_wider has no effect #9203

Closed
1 task done
contang0 opened this issue May 16, 2024 · 1 comment
Closed
1 task done

bug: names_sort argument in table.pivot_wider has no effect #9203

contang0 opened this issue May 16, 2024 · 1 comment
Labels
bug Incorrect behavior inside of ibis

Comments

@contang0
Copy link

contang0 commented May 16, 2024

What happened?

I'm using Table.pivot_wider to unstack rows into columns. pivot_wider has an argument names_sort, which is False by default. However, setting it True doesn't seem to have any effect.

Here's an example to reproduce and the outputs. I expected the following sorting order after setting names_sort=True:
col1_T1, col1_T2, col2_T1, col2_T2.

`

    import ibis
    con = ibis.duckdb.connect(":memory:")

    data = pd.DataFrame(
        {
            "unique_id": ["A", "A", "B", "B"],
            "secondary_id": [
                "T1",
                "T2",
                "T1",
                "T2",
            ],
            "col1": ["x1", "x2", "x3", "x4"],
            "col2": ["y1", "y2", "y3", "y4"],
        }
    )

    tbl = ibis.memtable(data)

image

    tbl.pivot_wider(
        id_cols=["unique_id"],
        names_from=["secondary_id"],
        values_from=["col1", "col2"],
        names_sort=False,
    )

image

    tbl.pivot_wider(
        id_cols=["unique_id"],
        names_from=["secondary_id"],
        values_from=["col1", "col2"],
        names_sort=True,
    )`

image

What version of ibis are you using?

9.0

What backend(s) are you using, if any?

duckdb

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@contang0 contang0 added the bug Incorrect behavior inside of ibis label May 16, 2024
@contang0 contang0 changed the title bug: table.pivot_wider argument names_sort has no effect bug: names_sort argument in table.pivot_wider has no effect May 16, 2024
@gforsyth
Copy link
Member

Hey @contang0 -- it has an effect, but the names column you've given it is already sorted (taking into account that we only need a set of column names.

[ins] In [1]: import ibis
         ...: import pandas as pd
         ...: 
         ...: ibis.options.interactive = True
         ...: 
         ...: con = ibis.duckdb.connect(":memory:")
         ...: 
         ...: data = pd.DataFrame(
         ...:     {
         ...:         "unique_id": ["A", "A", "B", "B"],
         ...:         "secondary_id": [
         ...:             "T2",
         ...:             "T2",
         ...:             "T1",
         ...:             "T1",
         ...:         ],
         ...:         "col1": ["x1", "x2", "x3", "x4"],
         ...:         "col2": ["y1", "y2", "y3", "y4"],
         ...:     }
         ...: )
         ...: 
         ...: tbl = ibis.memtable(data)

[ins] In [2]: tbl.pivot_wider(
         ...:     id_cols=["unique_id"],
         ...:     names_from=["secondary_id"],
         ...:     values_from=["col1", "col2"],
         ...:     names_sort=False,
         ...: )
Out[2]: 
┏━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
┃ unique_idcol1_T2col2_T2col1_T1col2_T1 ┃
┡━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
│ stringstringstringstringstring  │
├───────────┼─────────┼─────────┼─────────┼─────────┤
│ Ax1y1NULLNULL    │
│ BNULLNULLx3y3      │
└───────────┴─────────┴─────────┴─────────┴─────────┘

[ins] In [3]: tbl.pivot_wider(
         ...:     id_cols=["unique_id"],
         ...:     names_from=["secondary_id"],
         ...:     values_from=["col1", "col2"],
         ...:     names_sort=True,
         ...: )
Out[3]: 
┏━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
┃ unique_idcol1_T1col2_T1col1_T2col2_T2 ┃
┡━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
│ stringstringstringstringstring  │
├───────────┼─────────┼─────────┼─────────┼─────────┤
│ ANULLNULLx1y1      │
│ Bx3y3NULLNULL    │
└───────────┴─────────┴─────────┴─────────┴─────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior inside of ibis
Projects
Status: done
Development

No branches or pull requests

2 participants