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

add resize!, keepat!, pop!, popfist!, popat! #3047

Merged
merged 20 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
* Add `allcombinations` function that returns a data frame created
from all combinations of the passed vectors
([#3031](https://github.com/JuliaData/DataFrames.jl/pull/3031))
* Add `keepat!` and `pop!`, `popfirst!`, `popat!`
([#3047](https://github.com/JuliaData/DataFrames.jl/pull/3047))
* In `deleteat!` allow unsorted or duplicate indices passed
([#3047](https://github.com/JuliaData/DataFrames.jl/pull/3047))

## Previously announced breaking changes

Expand Down
4 changes: 4 additions & 0 deletions docs/src/lib/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ empty
empty!
filter
filter!
keepat!
first
last
only
pop!
popat!
popfirst!
nonunique
subset
subset!
Expand Down
8 changes: 8 additions & 0 deletions src/DataFrames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ else
export only
end

if isdefined(Base, :keepat!) # Introduced in 1.7.0
import Base.keepat!
else
# TODO: uncomment when keepat! is added to Compat.jl
# import Compat.keepat!
export keepat!
bkamins marked this conversation as resolved.
Show resolved Hide resolved
end

if VERSION >= v"1.3"
using Base.Threads: @spawn
else
Expand Down
101 changes: 96 additions & 5 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,8 @@ end

Delete rows specified by `inds` from a `DataFrame` `df` in place and return it.

Internally `deleteat!` is called for all columns so `inds` must be:
a vector of sorted and unique integers, a boolean vector, an integer,
or `Not` wrapping any valid selector.
`inds` do not have to be sorted nor unique. Non-unique elements of `inds`
are de-duplicated before deleting rows.

# Examples
```jldoctest
Expand All @@ -793,7 +792,7 @@ julia> deleteat!(df, 2)
2 │ 3 6
```
"""
function Base.deleteat!(df::DataFrame, inds)
function Base.deleteat!(df::DataFrame, inds::AbstractVector)
bkamins marked this conversation as resolved.
Show resolved Hide resolved
if !isempty(inds) && size(df, 2) == 0
throw(BoundsError(df, (inds, :)))
end
Expand All @@ -808,6 +807,8 @@ function Base.deleteat!(df::DataFrame, inds)
return _deleteat!_helper(df, inds)
end

Base.deleteat!(df::DataFrame, inds::Integer) = deleteat!(df, Int[inds])

function Base.deleteat!(df::DataFrame, inds::AbstractVector{Bool})
if length(inds) != size(df, 1)
throw(BoundsError(df, (inds, :)))
Expand All @@ -822,7 +823,34 @@ end

Base.deleteat!(df::DataFrame, inds::Not) = deleteat!(df, axes(df, 1)[inds])

function _deleteat!_helper(df::DataFrame, drop)
function _isincreasing(v::AbstractVector)
bkamins marked this conversation as resolved.
Show resolved Hide resolved
length(v) < 2 && return true
last = v[begin]
for i in firstindex(v)+1:lastindex(v)
cur = @inbounds x[i]
cur > last || return false
last = cur
end
return true
end

function _uniquesorted(v::AbstractVector)
x = sort(v)
# here we know that length of v is at least 2
last = v[begin]
u = eltype(v)[last]
for i in firstindex(v)+1:lastindex(v)
cur = @inbounds x[i]
cur == last && continue
push!(u, cur)
bkamins marked this conversation as resolved.
Show resolved Hide resolved
last = cur
end
return u
end

function _deleteat!_helper(df::DataFrame,
drop_raw::AbstractVector{<:Union{Signed, Unsigned}})
drop = _isincreasing(drop_raw) ? drop_raw : _uniquesorted(drop_raw)
cols = _columns(df)
isempty(cols) && return df

Expand All @@ -846,6 +874,34 @@ function _deleteat!_helper(df::DataFrame, drop)
return df
end

"""
keepat!(df::DataFrame, inds)

Delete rows at all indices not specified by `inds` from a `DataFrame` `df`
in place and return it.

# Examples
```jldoctest
julia> df = DataFrame(a=1:3, b=4:6)
3×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 4
2 │ 2 5
3 │ 3 6

julia> keepat!(df, [1, 3])
2×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 4
2 │ 3 6
```
"""
keepat!(df::DataFrame, inds) = deleteat!(df, Not(inds))

"""
empty!(df::DataFrame)

Expand All @@ -856,6 +912,41 @@ function Base.empty!(df::DataFrame)
return df
end

"""
resize!(df::DataFrame, n::Integer)

Resize `df` to have `n` rows by calling `resize!` on all columns of `df`.
"""
function Base.resize!(df::DataFrame, n::Integer)
foreach(col -> resize!(col, n), eachcol(df))
bkamins marked this conversation as resolved.
Show resolved Hide resolved
return df
end

"""
pop!(df::DataFrame)

Remove the last row from `df` and return a `NamedTuple` created from this row.
"""
pop!(df::DataFrame) = popat!(df, nrow(df))

"""
popfirst!(df::DataFrame)

Remove the first row from `df` and return a `NamedTuple` created from this row.
"""
popfirst!(df::DataFrame) = popat!(df, 1)

"""
popat!(df::DataFrame, i::Integer)

Remove the `i`-th row from `df` and return a `NamedTuple` created from this row.
"""
function popat!(df::DataFrame, i::Integer)
nt = NamedTuple(df[i, :])
deleteat!(df, i)
return nt
end

##############################################################################
##
## hcat!
Expand Down