Skip to content

Commit

Permalink
error when using one dimension for indexing (#2553)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Nov 20, 2020
1 parent fd0d798 commit 8386d0a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DataFrames"
uuid = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
version = "0.22.0"
version = "0.22.1"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
8 changes: 8 additions & 0 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1995,3 +1995,11 @@ function repeat_lengths!(longnew::AbstractVector, shortold::AbstractVector,
counter += l
end
end

# Disallowed operations that are a common mistake

Base.getindex(::AbstractDataFrame, ::Union{Symbol, Integer, AbstractString}) =
throw(ArgumentError("syntax df[column] is not supported use df[!, column] instead"))

Base.setindex!(::AbstractDataFrame, ::Any, ::Union{Symbol, Integer, AbstractString}) =
throw(ArgumentError("syntax df[column] is not supported use df[!, column] instead"))
24 changes: 17 additions & 7 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1741,14 +1741,14 @@ end
@testset "unsupported df[col] and df[col] for getindex, view, and setindex!" begin
@testset "getindex DataFrame" begin
df = DataFrame(a=1:3, b=4:6, c=7:9)
@test_throws MethodError df[1]
@test_throws ArgumentError df[1]
@test_throws MethodError df[end]
@test_throws MethodError df[1:2]
@test_throws MethodError df[r"[ab]"]
@test_throws MethodError df[Not(3)]
@test_throws MethodError df[:]
@test_throws MethodError df[:a]
@test_throws MethodError df["a"]
@test_throws ArgumentError df[:a]
@test_throws ArgumentError df["a"]
end
@testset "view DataFrame" begin
df = DataFrame(a=1:3, b=4:6, c=7:9)
Expand All @@ -1763,10 +1763,10 @@ end
@testset "getindex SubDataFrame" begin
df = DataFrame(x=-1:3, a=0:4, b=3:7, c=6:10, d=9:13)
sdf = view(df, 2:4, 2:4)
@test_throws MethodError sdf[1]
@test_throws ArgumentError sdf[1]
@test_throws MethodError sdf[end]
@test_throws MethodError sdf["x"]
@test_throws MethodError sdf[:x]
@test_throws ArgumentError sdf[:x]
@test_throws ArgumentError sdf["x"]
@test_throws MethodError sdf[1:2]
@test_throws MethodError sdf[r"[ab]"]
@test_throws MethodError sdf[Not(Not(r"[ab]"))]
Expand Down Expand Up @@ -1800,7 +1800,7 @@ end

# Broadcasting assignment of columns
@test_throws MethodError df[:, 1] = 1
@test_throws MethodError df[:x3] = 2
@test_throws ArgumentError df[:x3] = 2

# assignment of subtables
@test_throws MethodError df[1, 1:2] = df[2:2, 2:3]
Expand Down Expand Up @@ -1848,4 +1848,14 @@ end
@test_throws ArgumentError dfv."a" = 1
end

@testset "disallowed getindex and setindex! methods" begin
df = DataFrame(a=1)
@test_throws ArgumentError df[:a]
@test_throws ArgumentError df[:a] = [2]
@test_throws ArgumentError df["a"]
@test_throws ArgumentError df["a"] = [2]
@test_throws ArgumentError df[1]
@test_throws ArgumentError df[1] = [2]
end

end # module

2 comments on commit 8386d0a

@bkamins
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/25051

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.22.1 -m "<description of version>" 8386d0a3d9d1261a3e8efa5c4541a57f7c88695b
git push origin v0.22.1

Please sign in to comment.