Skip to content

Commit

Permalink
Always collect AbstractRange (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored and nalimilan committed Apr 19, 2018
1 parent 1a241c9 commit 0557c0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ Each column in `columns` should be the same length.
**Notes**
A `DataFrame` is a lightweight object. As long as columns are not
manipulated, creation of a DataFrame from existing AbstractVectors is
manipulated, creation of a `DataFrame` from existing AbstractVectors is
inexpensive. For example, indexing on columns is inexpensive, but
indexing by rows is expensive because copies are made of each column.
Because column types can vary, a DataFrame is not type stable. For
performance-critical code, do not index into a DataFrame inside of
If a column is passed to a `DataFrame` constructor or is assigned as a whole
using `setindex!` then its reference is stored in the `DataFrame`. An exception
to this rule is assignment of an `AbstractRange` as a column, in which case the
range is collected to a `Vector`.
Because column types can vary, a `DataFrame` is not type stable. For
performance-critical code, do not index into a `DataFrame` inside of
loops.
**Examples**
Expand Down Expand Up @@ -325,12 +330,13 @@ end

# Will automatically add a new column if needed
function insert_single_column!(df::DataFrame,
dv::AbstractVector,
v::AbstractVector,
col_ind::ColumnIndex)

if ncol(df) != 0 && nrow(df) != length(dv)
if ncol(df) != 0 && nrow(df) != length(v)
throw(ArgumentError("New columns must have the same length as old columns"))
end
dv = isa(v, AbstractRange) ? collect(v) : v
if haskey(index(df), col_ind)
j = index(df)[col_ind]
df.columns[j] = dv
Expand Down
7 changes: 7 additions & 0 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,13 @@ module TestDataFrame
@test names(df) == [:x3, :x3_1, :x3_2, :x4]
end

@testset "passing range to a DataFrame" begin
df = DataFrame(a=1:3, b='a':'c')
df[:c] = 1:3
df[:d] = 'a':'c'
@test all(typeof(df[i]) <: Vector for i in 1:ncol(df))
end

@testset "handling of end in indexing" begin
z = DataFrame(rand(4,5))
for x in [z, view(z, 1:4)]
Expand Down

0 comments on commit 0557c0e

Please sign in to comment.