Comments (4)
Another example:
julia> combine(groupby(a, :k), :t=>stack)
4×2 DataFrame
Row │ k t_stack
│ Int64 Int64
─────┼────────────────
1 │ 1 1
2 │ 1 2
3 │ 2 3
4 │ 2 4
Expected behavior:
julia> combine(groupby(a, :k), :t=>stack)
4×2 DataFrame
Row │ k t_stack
│ Int64 Array…
─────┼────────────────
1 │ 1 [1, 2]
2 │ 2 [3, 4]
I'm uncertain about the internal mechanism, but it appears that the DataFrame might undergo flatten
ing after the combination of grouped DataFrames.
from dataframes.jl.
Alternative using Base.vect
and Ref
.
from dataframes.jl.
This is a design feature. The rule is that if function returns a vector it gets expanded. The reason is that in a vast majority of cases this is what users expect, and requiring them to flatten
the result every time in this case would be inconvenient.
Note that even the simplest :a => identity
requires flattening to produce a correct result.
It is important to understand that aggregation functions decide about how to handle the results on transformations based on the VALUE returned, not based on a function called. Relying on a function called would produce many special cases that would be even harder to learn.
Your case is rare (applying sum
over vector of vectors) therefore the decision was that it should be handled by a special rule. As you have found, and as is written in the docstring:
In all of these cases, function can return either a single row or multiple rows. As a particular rule, values wrapped in a
Ref
or a 0-dimensionalAbstractArray
are unwrapped and then treated as a single row.
So you can write e.g. one of these (whichever is easier to remember for you):
julia> combine(groupby(a, :k), :v=>Ref∘sum)
2×2 DataFrame
Row │ k v_Ref_sum
│ Int64 Array…
─────┼──────────────────
1 │ 1 [4, 6]
2 │ 2 [12, 14]
julia> combine(groupby(a, :k), :v=>fill∘sum)
2×2 DataFrame
Row │ k v_fill_sum
│ Int64 Array…
─────┼───────────────────
1 │ 1 [4, 6]
2 │ 2 [12, 14]
To get what you want.
from dataframes.jl.
Thank you for your response. I have updated the document to ensure proper dissemination.
from dataframes.jl.
Related Issues (20)
- What is the best way to write large DataFrames efficiently and with high performance in Julia while minimizing memory usage? HOT 4
- Segmentation Fault when reading compressed file HOT 1
- Revisit spreading for `AsTable` output` HOT 6
- Better error message when forming a DataFrame from a vector of dictionaries with missing data. HOT 2
- `describe` is slow HOT 3
- CartesianIndex error in Julia 1.11 HOT 4
- `DataFrame(x=Int[], y=Int)` HOT 3
- Add comparison function for dataframes which can handle both isapprox and isequal column types HOT 2
- unique fails with column-type FixedDecimal HOT 5
- mapcols! should modify the parent of a SubDataFrame HOT 11
- Feature request: Pairs in stack HOT 2
- error when combining a grouped empty dataframe using `first` HOT 6
- Short circuit && on subset? HOT 1
- Integer strings as colnames/selectors are error prone HOT 2
- Suggestion - Matrix Syntax for hcat (as well as vcat) HOT 4
- Document custom generation of column names in manual HOT 9
- `join` should not introduce `Missing` types to schema HOT 1
- Consider removing Tables.allocatecolumn in vcat
- DataFrame(t::Table) converts PooledVector columns HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dataframes.jl.