Skip to content

Commit

Permalink
Add view kwarg to first and last (#2951)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandu-4444 authored Dec 1, 2021
1 parent 2e18895 commit b07a8bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
([#2459](https://github.com/JuliaData/DataFrames.jl/pull/2459))
* `sort!` now supports general `AbstractDataFrame`
([#2946](https://github.com/JuliaData/DataFrames.jl/pull/2946))
* `filter` now supports `view` keyword argument
([#2951](https://github.com/JuliaData/DataFrames.jl/pull/2951))

## Bug fixes

Expand Down
16 changes: 12 additions & 4 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,15 @@ Get the first row of `df` as a `DataFrameRow`.
Base.first(df::AbstractDataFrame) = df[1, :]

"""
first(df::AbstractDataFrame, n::Integer)
first(df::AbstractDataFrame, n::Integer; view::Bool=false)
Get a data frame with the `n` first rows of `df`.
If `view=false` a freshly allocated `DataFrame` is returned.
If `view=true` then a `SubDataFrame` view into `df` is returned.
"""
Base.first(df::AbstractDataFrame, n::Integer) = df[1:min(n, nrow(df)), :]
@inline Base.first(df::AbstractDataFrame, n::Integer; view::Bool=false) =
view ? Base.view(df, 1:min(n ,nrow(df)), :) : df[1:min(n, nrow(df)), :]

"""
last(df::AbstractDataFrame)
Expand All @@ -500,11 +504,15 @@ Get the last row of `df` as a `DataFrameRow`.
Base.last(df::AbstractDataFrame) = df[nrow(df), :]

"""
last(df::AbstractDataFrame, n::Integer)
last(df::AbstractDataFrame, n::Integer; view::Bool=false)
Get a data frame with the `n` last rows of `df`.
If `view=false` a freshly allocated `DataFrame` is returned.
If `view=true` then a `SubDataFrame` view into `df` is returned.
"""
Base.last(df::AbstractDataFrame, n::Integer) = df[max(1, nrow(df)-n+1):nrow(df), :]
@inline Base.last(df::AbstractDataFrame, n::Integer; view::Bool=false) =
view ? Base.view(df, max(1, nrow(df)-n+1):nrow(df), :) : df[max(1, nrow(df)-n+1):nrow(df), :]


"""
Expand Down
13 changes: 13 additions & 0 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,19 @@ end
@test last(df, 6) == DataFrame(A=5:10)
@test last(df, 1) == DataFrame(A=10)

@inferred first(df, 6)
@inferred last(df, 6)
@inferred first(df)
@inferred last(df)

@test first(df, 6, view=true) == DataFrame(A=1:6)
@test last(df, 6, view=true) == DataFrame(A=5:10)

@test first(df, 6, view=true) isa SubDataFrame
@test first(df, 6, view=false) isa DataFrame
@test last(df, 6, view=true) isa SubDataFrame
@test last(df, 6, view=false) isa DataFrame

@test_throws ArgumentError only(df)
@test_throws ArgumentError only(DataFrame())
df = DataFrame(a=1, b=2)
Expand Down

0 comments on commit b07a8bf

Please sign in to comment.