-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,19 @@ | |
## Join / merge | ||
## | ||
|
||
# Like similar, but returns a nullable array | ||
similar_nullable{T}(dv::AbstractArray{T}, dims::Union{Int, Tuple{Vararg{Int}}}) = | ||
NullableArray(T, dims) | ||
|
||
similar_nullable{T<:Nullable}(dv::AbstractArray{T}, dims::Union{Int, Tuple{Vararg{Int}}}) = | ||
NullableArray(eltype(T), dims) | ||
|
||
similar_nullable{T,R}(dv::CategoricalArray{T,R}, dims::Union{Int, Tuple{Vararg{Int}}}) = | ||
NullableCategoricalArray(T, dims) | ||
|
||
similar_nullable(dt::AbstractDataTable, dims::Int) = | ||
DataTable(Any[similar_nullable(x, dims) for x in columns(dt)], copy(index(dt))) | ||
|
||
# helper structure for DataTables joining | ||
immutable DataTableJoiner{DT1<:AbstractDataTable, DT2<:AbstractDataTable} | ||
dtl::DT1 | ||
|
@@ -64,9 +77,9 @@ function compose_joined_table(joiner::DataTableJoiner, | |
end | ||
all_orig_right_ixs = vcat(right_ixs.orig, rightonly_ixs.orig) | ||
resizelen = length(all_orig_right_ixs)+length(leftonly_ixs) | ||
rightcols = Any[length(col[all_orig_right_ixs]) >= resizelen ? | ||
resize!(col[all_orig_right_ixs], resizelen)[right_perm] : | ||
NullableArray(vcat(col[all_orig_right_ixs], fill(Nullable(), resizelen - length(col[all_orig_right_ixs]))))[right_perm] | ||
rightcols = Any[length(all_orig_right_ixs) >= resizelen ? | ||
resize!(col[all_orig_right_ixs], resizelen)[right_perm] : | ||
copy!(similar_nullable(col[all_orig_right_ixs], resizelen), col[all_orig_right_ixs])[right_perm] | ||
This comment has been minimized.
Sorry, something went wrong.
nalimilan
Member
|
||
for col in columns(dtr_noon)] | ||
right_dt = DataTable(rightcols, names(dtr_noon)) | ||
# merge left and right parts of the joined table | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -77,25 +77,42 @@ type DataTable <: AbstractDataTable | |||
if length(columns) == length(colindex) == 0 | ||||
return new(Vector{Any}(0), Index()) | ||||
elseif length(columns) != length(colindex) | ||||
throw(DimensionMismatch("Number of columns and column names are different")) | ||||
throw(DimensionMismatch("Number of columns ($(length(columns))) and column names ($(length(colindex))) are not equal")) | ||||
This comment has been minimized.
Sorry, something went wrong.
nalimilan
Member
|
||||
end | ||||
# do we allow people assigning arrays to columns now? | ||||
# make sure that doesn't work | ||||
# can use !get(size(c, 2), 0) | ||||
lengths = length.(columns) | ||||
This comment has been minimized.
Sorry, something went wrong.
nalimilan
Member
|
||||
minlen, maxlen = extrema(lengths) | ||||
if minlen == 0 && maxlen == 0 | ||||
return new(columns, colindex) | ||||
elseif (minlen == 0 && maxlen > 0) || any(x -> x != 0, mod(maxlen, lengths)) | ||||
throw(DimensionMismatch("Incompatible lengths of arguments")) | ||||
else | ||||
for i in 1:length(columns) | ||||
if isa(columns[i], Range) | ||||
columns[i] = collect(columns[i]) | ||||
elseif minlen != maxlen | ||||
# recycle scalars | ||||
if minlen == 1 && maxlen > 1 | ||||
indices = find(lengths .== minlen) | ||||
for i in indices | ||||
This comment has been minimized.
Sorry, something went wrong.
nalimilan
Member
|
||||
if !(typeof(columns[i]) <: AbstractArray) | ||||
columns[i] = fill(columns[i], maxlen) | ||||
lengths[i] = maxlen | ||||
end | ||||
end | ||||
repeats = div(maxlen, length(columns[i])) | ||||
if repeats == 1 && !(typeof(columns[i]) <: AbstractVector) | ||||
columns[i] = [columns[i]] | ||||
elseif repeats !== 1 | ||||
columns[i] = isa(columns[i], Array) ? repeat(columns[i], outer=repeats) : fill(columns[i], repeats) | ||||
end | ||||
uniques = unique(lengths) | ||||
if length(uniques) != 1 | ||||
estring = Vector{String}(length(uniques)) | ||||
strnames = string.(names(colindex)) | ||||
for (i,u) in enumerate(uniques) | ||||
This comment has been minimized.
Sorry, something went wrong. |
||||
indices = find(lengths .== u) | ||||
estring[i] = "column length ($(lengths[1])) for column(s) ($(join(strnames[indices], ", ")))" | ||||
end | ||||
throw(DimensionMismatch(join(estring, " is incompatible with "))) | ||||
end | ||||
end | ||||
for (i,c) in enumerate(columns) | ||||
if isa(c, Range) | ||||
columns[i] = collect(c) | ||||
elseif !isa(c, AbstractVector) | ||||
columns[i] = size(c, 2) > 1 ? reshape(c, length(c)) : [c] | ||||
This comment has been minimized.
Sorry, something went wrong.
nalimilan
Member
|
||||
end | ||||
end | ||||
return new(columns, colindex) | ||||
|
@@ -106,14 +123,18 @@ function DataTable(; kwargs...) | |||
if length(kwargs) == 0 | ||||
return DataTable(Any[], Index()) | ||||
end | ||||
columns = Any[v for (k,v) in kwargs] | ||||
colindex = DataTables.Index([k for (k,v) in kwargs]) | ||||
DataTable(columns, colindex) | ||||
colnames = Vector{Symbol}(length(kwargs)) | ||||
columns = Vector{Any}(length(kwargs)) | ||||
for (i,(k,v)) in enumerate(kwargs) | ||||
colnames[i] = Symbol(k) | ||||
columns[i] = v | ||||
end | ||||
DataTable(columns, Index(colnames)) | ||||
end | ||||
|
||||
function DataTable(columns::AbstractVector, | ||||
cnames::AbstractVector{Symbol} = gennames(length(columns))) | ||||
return DataTable(convert(Vector{Any}, columns), Index(convert(Vector{Symbol}, cnames))) | ||||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cjprybol
Author
Contributor
|
res = Array{Symbol}(n) |
This comment has been minimized.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
This comment has been minimized.
Sorry, something went wrong.
cjprybol
Mar 15, 2017
•
edited
Loading
Author
Contributor
edited
Yes, I hadn't thought of that as being problematic. All of the other constructors have cnames
as either cnames::Vector
or cnames::Vector{Symbol}
. This PR currently changes all instances of cnames::Vector
to being cnames::Vector{Symbol}
. This constructor stood out as being particularly forgiving. We could change all of them to cnames::AbstractVector{Symbol}
with a call to convert(Vector{Symbol}, cnames)
?
This comment has been minimized.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
This comment has been minimized.
Sorry, something went wrong.
values
is an internal field; shouldn't be mentioned here. Just say that the new vectors may alias with the new ones or that it is not guaranteed that a copy will be returned.