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 6 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* 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))

## Previously announced breaking changes

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[compat]
CategoricalArrays = "0.10.0"
Compat = "3.17"
Compat = "3.44, 4.1"
DataAPI = "1.10"
InvertedIndices = "1"
IteratorInterfaceExtensions = "0.1.1, 1"
Expand Down
7 changes: 6 additions & 1 deletion docs/src/lib/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,15 @@ empty
empty!
filter
filter!
keepat!
first
last
only
nonunique
only
pop!
popat!
popfirst!
resize!
subset
subset!
unique
Expand Down
7 changes: 7 additions & 0 deletions src/DataFrames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ else
export only
end

if isdefined(Base, :keepat!) # Introduced in 1.7.0
import Base.keepat!
else
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
89 changes: 85 additions & 4 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,6 @@ 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.

bkamins marked this conversation as resolved.
Show resolved Hide resolved
# Examples
```jldoctest
julia> df = DataFrame(a=1:3, b=4:6)
Expand Down Expand Up @@ -808,6 +804,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 @@ -823,6 +821,9 @@ end
Base.deleteat!(df::DataFrame, inds::Not) = deleteat!(df, axes(df, 1)[inds])

function _deleteat!_helper(df::DataFrame, drop)
if !issorted(drop, lt=<=)
throw(ArgumentError("Indices passed to deleteat! must be unique and sorted"))
end
cols = _columns(df)
isempty(cols) && return df

Expand All @@ -846,6 +847,47 @@ 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
```
"""
function keepat!(df::DataFrame, inds)
if !issorted(inds, lt=<=)
throw(ArgumentError("Indices passed to keepat! must be unique and sorted"))
end
bkamins marked this conversation as resolved.
Show resolved Hide resolved
return deleteat!(df, Not(inds))
end

function keepat!(df::DataFrame, inds::Integer)
inds isa Bool && throw(ArgumentError("Invalid index of type Bool"))
return keepat!(df, Int[inds])
bkamins marked this conversation as resolved.
Show resolved Hide resolved
end

keepat!(df::DataFrame, inds::AbstractVector{Bool}) = deleteat!(df, .!inds)
keepat!(df::DataFrame, inds::Not) = keepat!(df, axes(df, 1)[inds])

"""
empty!(df::DataFrame)

Expand All @@ -856,6 +898,45 @@ 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)
if ncol(df) == 0 && n != 0
throw(ArgumentError("data frame has no columns and requested number " *
"of rows is not zero"))
end
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.
"""
Base.pop!(df::DataFrame) = popat!(df, nrow(df))

"""
popfirst!(df::DataFrame)

Remove the first row from `df` and return a `NamedTuple` created from this row.
"""
Base.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 Base.popat!(df::DataFrame, i::Integer)
nt = NamedTuple(df[i, :])
deleteat!(df, i)
return nt
end

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