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

Fix ordering of integer-named columns. #53

Closed
ericemc3 opened this issue Dec 12, 2020 · 2 comments · Fixed by #54
Closed

Fix ordering of integer-named columns. #53

ericemc3 opened this issue Dec 12, 2020 · 2 comments · Fixed by #54
Labels
bug Something isn't working

Comments

@ericemc3
Copy link

I love pivot and fold fonctions in Arquero, they are so powerful !

I just ran into few small difficulties.
Here is a use case:

t = aq.table({x:['01','01','01','02','02','02'], y:['1','2','3','1','2','3'], z:[123,256,854, 652,734,222]})

x y z
01 1 123
01 2 256
01 3 854
02 1 652
02 2 734
02 3 222

I pivot that table:

t.groupby(0).pivot(1,2)

The problem is that x is now located on the right, i'd like it to stay in the first position.

1 2 3 x
123 256 854 01
652 734 222 02

I am trying all these variants, but in vain, x doesn't want to move ;)

t.groupby(0).pivot(1,2).relocate('x', {before:0}).view() ;
t.groupby(0).pivot(1,2).relocate('x', {before:1}).view() ;
t.groupby(0).pivot(1,2).relocate('x', {before:"1"}).view() ;

Maybe i am missing something there. Of course i can write a select.
The fact that new column names are not by default prefixed probably does not help.

My humble suggestions would be:

  1. after a pivot, make the groupby columns appear at first
  2. add a prefix option in pivot (and fold as well)

For instance:
p = t.groupby(0).pivot(1,2, {prefix:'age_'})

could produce:

x age_1 age_2 age_3
01 123 256 854
02 652 734 222

and conversely:
p.fold(aq.range(1,3),{as:['y','z'], prefix:'age_'})

would take me back to the original table.

For the moment i use for pivot something like:
p = t.groupby(0).pivot({ d: d => 'age_' + d.y },2, {prefix:'age_'})

But after a fold i have to write something like this this, which i find not very elegant:
.derive({y: d => op.substring(d.y,4)})

cf. for inspiration (and old habits):
https://tidyr.tidyverse.org/reference/pivot_longer.html (aka fold) and names_prefix option
https://tidyr.tidyverse.org/reference/pivot_wider.html (aka pivot) and names_prefix option

@jheer jheer changed the title pivot, column names and position Fix ordering of integer-named columns. Dec 13, 2020
@jheer jheer added the bug Something isn't working label Dec 13, 2020
@jheer
Copy link
Member

jheer commented Dec 13, 2020

Thanks! This bug arises from JavaScript's built-in key ordering semantics for objects (integers always come first), which Arquero uses currently but of course needs to circumvent. I'm aware of the issue (I bumped into it more or less the same way you did!) and have started work on a fix.

@jheer
Copy link
Member

jheer commented Dec 17, 2020

Fixed and released in v1.3.0.

@jheer jheer closed this as completed Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants