Skip to content
This repository has been archived by the owner on May 5, 2019. It is now read-only.

usefulness of a colwise! method? #36

Open
mkborregaard opened this issue Mar 17, 2017 · 5 comments
Open

usefulness of a colwise! method? #36

mkborregaard opened this issue Mar 17, 2017 · 5 comments

Comments

@mkborregaard
Copy link
Contributor

DataTables implement a colwise method, but no colwise!. There aren't a massive number of use cases for this, but I can think of e.g. data centering and normalization. If there are no major technical obstacles with making such a function I think it'd make a nice addition.

@cjprybol
Copy link
Contributor

Are you imagining that colwise! would work like an in-place aggregate?

example:

julia> dt
10×2 DataTables.DataTable
│ Row │ A  │ B  │
├─────┼────┼────┤
│ 111  │
│ 222  │
│ 333  │
│ 444  │
│ 555  │
│ 666  │
│ 777  │
│ 888  │
│ 999  │
│ 101010 │

julia> colwise!(normalize, dt)
10×2 DataTables.DataTable
│ Row │ A         │ B         │
├─────┼───────────┼───────────┤
│ 10.05096470.0509647 │
│ 20.1019290.101929  │
│ 30.1528940.152894  │
│ 40.2038590.203859  │
│ 50.2548240.254824  │
│ 60.3057880.305788  │
│ 70.3567530.356753  │
│ 80.4077180.407718  │
│ 90.4586820.458682  │
│ 100.5096470.509647# compared to

julia> aggregate(dt, normalize)
10×2 DataTables.DataTable
│ Row │ A_normalize │ B_normalize │
├─────┼─────────────┼─────────────┤
│ 10.05096470.0509647   │
│ 20.1019290.101929    │
│ 30.1528940.152894    │
│ 40.2038590.203859    │
│ 50.2548240.254824    │
│ 60.3057880.305788    │
│ 70.3567530.356753    │
│ 80.4077180.407718    │
│ 90.4586820.458682    │
│ 100.5096470.509647

That should be pretty straight forward to do. What would be the preferable behavior for functions that reduce to scalars like sum/length or extrema? Fail on a dimension mismatch or resize the DataTable to be a single row? (I'm not sure if resize!(column, 1) will work, in which case it'd have to throw an error because it couldn't modify in place).

example

julia> colwise!(length, dt)
1×2 DataTables.DataTable
│ Row │ A  │ B  │
├─────┼────┼────┤
│ 11010# or
Error: DimensionMismatch: columns cannot be resized in place, use aggregate(function, dt) instead

@nalimilan
Copy link
Member

aggregate is made to be used with a grouping variable. colwise! would just be an in-place equivalent of colwise, so something very simple. Cf. https://discourse.julialang.org/t/announcement-dataframes-future-plans/266/25.

Though thinking about it more, I'm not sure how useful such a function would be. colwise is mainly useful for summary functions which return one or two values for each column. OTC, colwise! would only work for functions which return the vectors or the same length as the input. So probably better give this function a different name.

The other issue with such a function is that colwise!(col -> col .- mean(col), df) it would encourage an inefficient pattern which allocates a copy of col before replacing the old column. A manual loop with x0 = mean(col); map!(x -> x - x0, col) is much more efficient in that regard. So if we offer such a helper function, it should be smarter than that. We could have it call the function for each element of a column, with additional column-wise arguments à la broadcast. For example: colwise!((x, x0) -> x - x0, df, colwise(mean, df).

@mkborregaard
Copy link
Contributor Author

mkborregaard commented Mar 20, 2017

Reading your concerns here and thinking about it more, I am also not sure this functionality would be broadly useful. It came out of a natural impulse to work with a DataTable inplace because this is so ideomatic in julia.
Maybe if map! could simply take an eachcol iterator as argument?

@nalimilan
Copy link
Member

Yes, that would make sense. Maybe a bit surprising that you would be able to modify an iterator, but doesn't sound like a big issue.

@nalimilan
Copy link
Member

Leaving a breadcrumb to https://stackoverflow.com/questions/44235201/shortcut-to-transforming-a-dataframe, which provides an example of a use case for this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants