Skip to content

Commit 3431859

Browse files
drizk1kdpsingh
andauthored
fixes n slice_min/max bug (#110)
* fixes `n` slice_min/max bug * adds `@head` * Clean up documentation in prep for release, bump version to v0.16.2. * Fix doctest. --------- Co-authored-by: Karandeep Singh <[email protected]>
1 parent 7f1ac79 commit 3431859

File tree

7 files changed

+326
-175
lines changed

7 files changed

+326
-175
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# TidierData.jl updates
22

3+
## v0.16.2 - 2024-08-05
4+
- Bugfix: `@slice_min` and `@slice_max` respect the `n` argument
5+
- Adds `@head`
6+
37
## v0.16.1 - 2024-06-09
48
- Adds support for tuples and vectors as arguments to select multiple columns. Prefixing tuples/vectors with a `-` or `!` will exclude the selected columns.
59
- The `:` selector from Julia is now available and equivalent to `everything()`

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TidierData"
22
uuid = "fe2206b3-d496-4ee9-a338-6a095c4ece80"
33
authors = ["Karandeep Singh"]
4-
version = "0.16.1"
4+
version = "0.16.2"
55

66
[deps]
77
Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ To support R-style programming, TidierData.jl is implemented using macros.
6767

6868
TidierData.jl currently supports the following top-level macros:
6969

70-
- `@glimpse()`
70+
- `@glimpse()` and `@head()`
7171
- `@select()` and `@distinct()`
7272
- `@rename()` and `@rename_with()`
7373
- `@mutate()` and `@transmute()`

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ TidierData.jl currently supports the following top-level macros:
8484

8585
```@raw html
8686
!!! example "Top-level macros:"
87-
- `@glimpse()`
87+
- `@glimpse()` and `@head()`
8888
- `@select()` and `@distinct()`
8989
- `@rename()` and `@rename_with()`
9090
- `@mutate()` and `@transmute()`

src/TidierData.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export TidierData_set, across, desc, n, row_number, everything, starts_with, end
2121
@group_by, @ungroup, @slice, @arrange, @distinct, @pull, @left_join, @right_join, @inner_join, @full_join, @anti_join, @semi_join,
2222
@pivot_wider, @pivot_longer, @bind_rows, @bind_cols, @clean_names, @count, @tally, @drop_missing, @glimpse, @separate,
2323
@unite, @summary, @fill_missing, @slice_sample, @slice_min, @slice_max, @slice_head, @slice_tail, @rename_with, @separate_rows,
24-
@unnest_longer, @unnest_wider, @nest, @relocate
24+
@unnest_longer, @unnest_wider, @nest, @relocate, @head
2525

2626
# Package global variables
2727
const code = Ref{Bool}(false) # output DataFrames.jl code?
@@ -688,4 +688,24 @@ macro rename_with(df, fn, exprs...)
688688
return df_expr
689689
end
690690

691+
"""
692+
$docstring_head
693+
"""
694+
macro head(df, exprs=6)
695+
return quote
696+
local df_input = $(esc(df))
697+
local n = $(esc(exprs))
698+
699+
if df_input isa GroupedDataFrame
700+
grouped_result = combine(df_input) do sdf
701+
first(sdf, n)
702+
end
703+
groupby(grouped_result, df_input.cols)
704+
else
705+
first(copy(df_input), n)
706+
end
707+
end
708+
end
709+
710+
691711
end

src/docstrings.jl

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,14 +2834,15 @@ julia> @chain df begin
28342834
1 │ 5.0 7.0 5.0
28352835
28362836
julia> @chain df begin
2837-
@slice_max(b, with_ties = false, n = 2)
2837+
@slice_max(b, n = 3)
28382838
end
2839-
2×3 DataFrame
2839+
3×3 DataFrame
28402840
Row │ a b c
28412841
│ Float64? Float64? Float64?
28422842
─────┼──────────────────────────────
28432843
1 │ 5.0 7.0 5.0
28442844
2 │ 6.0 7.0 6.0
2845+
3 │ 1.0 6.0 1.0
28452846
28462847
julia> @chain df begin
28472848
@slice_max(b, prop = 0.5, missing_rm = true)
@@ -2897,15 +2898,15 @@ julia> @chain df begin
28972898
1 │ missing 0.3 0.2
28982899
28992900
julia> @chain df begin
2900-
@slice_min(b, with_ties = true, n = 1)
2901-
end
2902-
2×3 DataFrame
2903-
Row │ a b c
2904-
│ Float64? Float64? Float64?
2905-
─────┼───────────────────────────────
2906-
1 │ missing 0.3 0.2
2907-
2 │ missing 0.3 missing
2908-
2901+
@slice_min(b, n = 3)
2902+
end
2903+
3×3 DataFrame
2904+
Row │ a b c
2905+
│ Float64? Float64? Float64?
2906+
─────┼───────────────────────────────
2907+
1 │ missing 0.3 0.2
2908+
2 │ missing 0.3 missing
2909+
3 │ 0.2 2.0 0.2
29092910
29102911
julia> @chain df begin
29112912
@slice_min(b, prop = 0.5, missing_rm = true)
@@ -2950,7 +2951,7 @@ julia> @chain df begin
29502951
3 │ missing missing 0.2
29512952
29522953
julia> @chain df begin
2953-
@slice_head(prop = .25)
2954+
@slice_head(prop = 0.25)
29542955
end
29552956
2×3 DataFrame
29562957
Row │ a b c
@@ -2991,7 +2992,7 @@ julia> @chain df begin
29912992
3 │ 6.0 7.0 6.0
29922993
29932994
julia> @chain df begin
2994-
@slice_tail(prop = .25)
2995+
@slice_tail(prop = 0.25)
29952996
end
29962997
2×3 DataFrame
29972998
Row │ a b c
@@ -3461,3 +3462,72 @@ julia> @relocate(df, B:C) # bring columns to the front
34613462
5 │ 10 E 5 C 5 E
34623463
```
34633464
"""
3465+
3466+
const docstring_head =
3467+
"""
3468+
@head(df, value)
3469+
Shows the first n rows of the the data frame or of each group in a grouped data frame.
3470+
3471+
# Arguments
3472+
- `df`: The data frame.
3473+
- `value`: number of rows to be returned. Defaults to 6 if left blank.
3474+
3475+
# Examples
3476+
```
3477+
julia> df = DataFrame(a = vcat(repeat(["a"], inner = 4),
3478+
repeat(["b"], inner = 4)),
3479+
b = 1:8)
3480+
8×2 DataFrame
3481+
Row │ a b
3482+
│ String Int64
3483+
─────┼───────────────
3484+
1 │ a 1
3485+
2 │ a 2
3486+
3 │ a 3
3487+
4 │ a 4
3488+
5 │ b 5
3489+
6 │ b 6
3490+
7 │ b 7
3491+
8 │ b 8
3492+
3493+
julia> @head(df, 3)
3494+
3×2 DataFrame
3495+
Row │ a b
3496+
│ String? Int64
3497+
─────┼────────────────
3498+
1 │ a 1
3499+
2 │ a 2
3500+
3 │ a 3
3501+
3502+
julia> @head(df)
3503+
6×2 DataFrame
3504+
Row │ a b
3505+
│ String Int64
3506+
─────┼───────────────
3507+
1 │ a 1
3508+
2 │ a 2
3509+
3 │ a 3
3510+
4 │ a 4
3511+
5 │ b 5
3512+
6 │ b 6
3513+
3514+
julia> @chain df begin
3515+
@group_by a
3516+
@head 2
3517+
end
3518+
GroupedDataFrame with 2 groups based on key: a
3519+
First Group (2 rows): a = "a"
3520+
Row │ a b
3521+
│ String Int64
3522+
─────┼───────────────
3523+
1 │ a 1
3524+
2 │ a 2
3525+
3526+
Last Group (2 rows): a = "b"
3527+
Row │ a b
3528+
│ String Int64
3529+
─────┼───────────────
3530+
1 │ b 5
3531+
2 │ b 6
3532+
```
3533+
"""

0 commit comments

Comments
 (0)