This repository was archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathabstractdatatable.jl
835 lines (648 loc) · 21 KB
/
abstractdatatable.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
"""
An abstract type for which all concrete types expose a database-like
interface.
**Common methods**
An AbstractDataTable is a two-dimensional table with Symbols for
column names. An AbstractDataTable is also similar to an Associative
type in that it allows indexing by a key (the columns).
The following are normally implemented for AbstractDataTables:
* [`describe`](@ref) : summarize columns
* [`dump`](@ref) : show structure
* `hcat` : horizontal concatenation
* `vcat` : vertical concatenation
* `names` : columns names
* [`names!`](@ref) : set columns names
* [`rename!`](@ref) : rename columns names based on keyword arguments
* [`eltypes`](@ref) : `eltype` of each column
* `length` : number of columns
* `size` : (nrows, ncols)
* [`head`](@ref) : first `n` rows
* [`tail`](@ref) : last `n` rows
* `convert` : convert to an array
* `NullableArray` : convert to a NullableArray
* [`completecases`](@ref) : boolean vector of complete cases (rows with no nulls)
* [`dropnull`](@ref) : remove rows with null values
* [`dropnull!`](@ref) : remove rows with null values in-place
* [`nonunique`](@ref) : indexes of duplicate rows
* [`unique!`](@ref) : remove duplicate rows
* `similar` : a DataTable with similar columns as `d`
**Indexing**
Table columns are accessed (`getindex`) by a single index that can be
a symbol identifier, an integer, or a vector of each. If a single
column is selected, just the column object is returned. If multiple
columns are selected, some AbstractDataTable is returned.
```julia
d[:colA]
d[3]
d[[:colA, :colB]]
d[[1:3; 5]]
```
Rows and columns can be indexed like a `Matrix` with the added feature
of indexing columns by name.
```julia
d[1:3, :colA]
d[3,3]
d[3,:]
d[3,[:colA, :colB]]
d[:, [:colA, :colB]]
d[[1:3; 5], :]
```
`setindex` works similarly.
"""
@compat abstract type AbstractDataTable end
##############################################################################
##
## Interface (not final)
##
##############################################################################
# index(dt) => AbstractIndex
# nrow(dt) => Int
# ncol(dt) => Int
# getindex(...)
# setindex!(...) exclusive of methods that add new columns
##############################################################################
##
## Basic properties of a DataTable
##
##############################################################################
immutable Cols{T <: AbstractDataTable} <: AbstractVector{Any}
dt::T
end
Base.start(::Cols) = 1
Base.done(itr::Cols, st) = st > length(itr.dt)
Base.next(itr::Cols, st) = (itr.dt[st], st + 1)
Base.length(itr::Cols) = length(itr.dt)
Base.size(itr::Cols, ix) = ix==1 ? length(itr) : throw(ArgumentError("Incorrect dimension"))
Base.size(itr::Cols) = (length(itr.dt),)
@compat Base.IndexStyle(::Type{<:Cols}) = IndexLinear()
Base.getindex(itr::Cols, inds...) = getindex(itr.dt, inds...)
# N.B. where stored as a vector, 'columns(x) = x.vector' is a bit cheaper
columns{T <: AbstractDataTable}(dt::T) = Cols{T}(dt)
Base.names(dt::AbstractDataTable) = names(index(dt))
_names(dt::AbstractDataTable) = _names(index(dt))
"""
Set column names
```julia
names!(dt::AbstractDataTable, vals)
```
**Arguments**
* `dt` : the AbstractDataTable
* `vals` : column names, normally a Vector{Symbol} the same length as
the number of columns in `dt`
* `allow_duplicates` : if `false` (the default), an error will be raised
if duplicate names are found; if `true`, duplicate names will be suffixed
with `_i` (`i` starting at 1 for the first duplicate).
**Result**
* `::AbstractDataTable` : the updated result
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
names!(dt, [:a, :b, :c])
names!(dt, [:a, :b, :a]) # throws ArgumentError
names!(dt, [:a, :b, :a], allow_duplicates=true) # renames second :a to :a_1
```
"""
function names!(dt::AbstractDataTable, vals; allow_duplicates=false)
names!(index(dt), vals; allow_duplicates=allow_duplicates)
return dt
end
function rename!(dt::AbstractDataTable, args...)
rename!(index(dt), args...)
return dt
end
rename!(f::Function, dt::AbstractDataTable) = rename!(dt, f)
rename(dt::AbstractDataTable, args...) = rename!(copy(dt), args...)
rename(f::Function, dt::AbstractDataTable) = rename(dt, f)
"""
Rename columns
```julia
rename!(dt::AbstractDataTable, from::Symbol, to::Symbol)
rename!(dt::AbstractDataTable, d::Associative)
rename!(f::Function, dt::AbstractDataTable)
rename(dt::AbstractDataTable, from::Symbol, to::Symbol)
rename(f::Function, dt::AbstractDataTable)
```
**Arguments**
* `dt` : the AbstractDataTable
* `d` : an Associative type that maps the original name to a new name
* `f` : a function that has the old column name (a symbol) as input
and new column name (a symbol) as output
**Result**
* `::AbstractDataTable` : the updated result
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
rename(x -> @compat(Symbol)(uppercase(string(x))), dt)
rename(dt, @compat(Dict(:i=>:A, :x=>:X)))
rename(dt, :y, :Y)
rename!(dt, @compat(Dict(:i=>:A, :x=>:X)))
```
"""
(rename!, rename)
"""
Return element types of columns
```julia
eltypes(dt::AbstractDataTable)
```
**Arguments**
* `dt` : the AbstractDataTable
**Result**
* `::Vector{Type}` : the element type of each column
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
eltypes(dt)
```
"""
eltypes(dt::AbstractDataTable) = map!(eltype, Vector{Type}(size(dt,2)), columns(dt))
Base.size(dt::AbstractDataTable) = (nrow(dt), ncol(dt))
function Base.size(dt::AbstractDataTable, i::Integer)
if i == 1
nrow(dt)
elseif i == 2
ncol(dt)
else
throw(ArgumentError("DataTables only have two dimensions"))
end
end
Base.length(dt::AbstractDataTable) = ncol(dt)
Base.endof(dt::AbstractDataTable) = ncol(dt)
Base.ndims(::AbstractDataTable) = 2
##############################################################################
##
## Similar
##
##############################################################################
Base.similar(dt::AbstractDataTable, dims::Int) =
DataTable(Any[similar(x, dims) for x in columns(dt)], copy(index(dt)))
##############################################################################
##
## Equality
##
##############################################################################
# Imported in DataTables.jl for compatibility across Julia 0.4 and 0.5
@compat(Base.:(==))(dt1::AbstractDataTable, dt2::AbstractDataTable) = isequal(dt1, dt2)
function Base.isequal(dt1::AbstractDataTable, dt2::AbstractDataTable)
size(dt1, 2) == size(dt2, 2) || return false
isequal(index(dt1), index(dt2)) || return false
for idx in 1:size(dt1, 2)
isequal(dt1[idx], dt2[idx]) || return false
end
return true
end
##############################################################################
##
## Associative methods
##
##############################################################################
Base.haskey(dt::AbstractDataTable, key::Any) = haskey(index(dt), key)
Base.get(dt::AbstractDataTable, key::Any, default::Any) = haskey(dt, key) ? dt[key] : default
Base.isempty(dt::AbstractDataTable) = ncol(dt) == 0
##############################################################################
##
## Description
##
##############################################################################
head(dt::AbstractDataTable, r::Int) = dt[1:min(r,nrow(dt)), :]
head(dt::AbstractDataTable) = head(dt, 6)
tail(dt::AbstractDataTable, r::Int) = dt[max(1,nrow(dt)-r+1):nrow(dt), :]
tail(dt::AbstractDataTable) = tail(dt, 6)
"""
Show the first or last part of an AbstractDataTable
```julia
head(dt::AbstractDataTable, r::Int = 6)
tail(dt::AbstractDataTable, r::Int = 6)
```
**Arguments**
* `dt` : the AbstractDataTable
* `r` : the number of rows to show
**Result**
* `::AbstractDataTable` : the first or last part of `dt`
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
head(dt)
tail(dt)
```
"""
(head, tail)
# get the structure of a DT
"""
Show the structure of an AbstractDataTable, in a tree-like format
```julia
dump(dt::AbstractDataTable, n::Int = 5)
dump(io::IO, dt::AbstractDataTable, n::Int = 5)
```
**Arguments**
* `dt` : the AbstractDataTable
* `n` : the number of levels to show
* `io` : optional output descriptor
**Result**
* nothing
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
dump(dt)
```
"""
function Base.dump(io::IO, dt::AbstractDataTable, n::Int, indent)
println(io, typeof(dt), " $(nrow(dt)) observations of $(ncol(dt)) variables")
if n > 0
for (name, col) in eachcol(dt)
print(io, indent, " ", name, ": ")
dump(io, col, n - 1, string(indent, " "))
end
end
end
# summarize the columns of a DT
# TODO: clever layout in rows
"""
Summarize the columns of an AbstractDataTable
```julia
describe(dt::AbstractDataTable)
describe(io, dt::AbstractDataTable)
```
**Arguments**
* `dt` : the AbstractDataTable
* `io` : optional output descriptor
**Result**
* nothing
**Details**
If the column's base type derives from Number, compute the minimum, first
quantile, median, mean, third quantile, and maximum. Nulls are filtered and
reported separately.
For boolean columns, report trues, falses, and nulls.
For other types, show column characteristics and number of nulls.
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
describe(dt)
```
"""
StatsBase.describe(dt::AbstractDataTable) = describe(STDOUT, dt)
function StatsBase.describe(io, dt::AbstractDataTable)
for (name, col) in eachcol(dt)
println(io, name)
describe(io, col)
println(io, )
end
end
StatsBase.describe(nv::AbstractArray) = describe(STDOUT, nv)
function StatsBase.describe{T<:Number}(io, nv::AbstractArray{T})
if all(_isnull, nv)
println(io, " * All null * ")
return
end
filtered = float(dropnull(nv))
qs = quantile(filtered, [0, .25, .5, .75, 1])
statNames = ["Min", "1st Qu.", "Median", "Mean", "3rd Qu.", "Max"]
statVals = [qs[1:3]; mean(filtered); qs[4:5]]
for i = 1:6
println(io, string(rpad(statNames[i], 10, " "), " ", string(statVals[i])))
end
nulls = countnull(nv)
println(io, "NULLs $(nulls)")
println(io, "NULL % $(round(nulls*100/length(nv), 2))%")
return
end
function StatsBase.describe{T}(io, nv::AbstractArray{T})
ispooled = isa(nv, CategoricalVector) ? "Pooled " : ""
nulls = countnull(nv)
# if nothing else, just give the length and element type and null count
println(io, "Length $(length(nv))")
println(io, "Type $(ispooled)$(string(eltype(nv)))")
println(io, "NULLs $(nulls)")
println(io, "NULL % $(round(nulls*100/length(nv), 2))%")
println(io, "Unique $(length(unique(nv)))")
return
end
##############################################################################
##
## Miscellaneous
##
##############################################################################
function _nonnull!(res, col)
for (i, el) in enumerate(col)
res[i] &= !_isnull(el)
end
end
function _nonnull!(res, col::NullableArray)
for (i, el) in enumerate(col.isnull)
res[i] &= !el
end
end
function _nonnull!(res, col::NullableCategoricalArray)
for (i, el) in enumerate(col.refs)
res[i] &= el > 0
end
end
"""
Indexes of complete cases (rows without null values)
```julia
completecases(dt::AbstractDataTable)
```
**Arguments**
* `dt` : the AbstractDataTable
**Result**
* `::Vector{Bool}` : indexes of complete cases
See also [`dropnull`](@ref) and [`dropnull!`](@ref).
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
dt[[1,4,5], :x] = Nullable()
dt[[9,10], :y] = Nullable()
completecases(dt)
```
"""
function completecases(dt::AbstractDataTable)
res = trues(size(dt, 1))
for i in 1:size(dt, 2)
_nonnull!(res, dt[i])
end
res
end
"""
Remove rows with null values.
```julia
dropnull(dt::AbstractDataTable)
```
**Arguments**
* `dt` : the AbstractDataTable
**Result**
* `::AbstractDataTable` : the updated copy
See also [`completecases`](@ref) and [`dropnull!`](@ref).
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
dt[[1,4,5], :x] = Nullable()
dt[[9,10], :y] = Nullable()
dropnull(dt)
```
"""
dropnull(dt::AbstractDataTable) = deleterows!(copy(dt), find(!, completecases(dt)))
"""
Remove rows with null values in-place.
```julia
dropnull!(dt::AbstractDataTable)
```
**Arguments**
* `dt` : the AbstractDataTable
**Result**
* `::AbstractDataTable` : the updated version
See also [`dropnull`](@ref) and [`completecases`](@ref).
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
dt[[1,4,5], :x] = Nullable()
dt[[9,10], :y] = Nullable()
dropnull!(dt)
```
"""
dropnull!(dt::AbstractDataTable) = deleterows!(dt, find(!, completecases(dt)))
function Base.convert(::Type{Array}, dt::AbstractDataTable)
convert(Matrix, dt)
end
function Base.convert(::Type{Matrix}, dt::AbstractDataTable)
T = reduce(promote_type, eltypes(dt))
T <: Nullable && (T = eltype(T))
convert(Matrix{T}, dt)
end
function Base.convert{T}(::Type{Array{T}}, dt::AbstractDataTable)
convert(Matrix{T}, dt)
end
function Base.convert{T}(::Type{Matrix{T}}, dt::AbstractDataTable)
n, p = size(dt)
res = Matrix{T}(n, p)
idx = 1
for (name, col) in zip(names(dt), columns(dt))
anynull(col) && error("cannot convert a DataTable containing null values to array (found for column $name)")
copy!(res, idx, convert(Vector{T}, col))
idx += n
end
return res
end
function Base.convert(::Type{NullableArray}, dt::AbstractDataTable)
convert(NullableMatrix, dt)
end
function Base.convert(::Type{NullableMatrix}, dt::AbstractDataTable)
T = reduce(promote_type, eltypes(dt))
T <: Nullable && (T = eltype(T))
convert(NullableMatrix{T}, dt)
end
function Base.convert{T}(::Type{NullableArray{T}}, dt::AbstractDataTable)
convert(NullableMatrix{T}, dt)
end
function Base.convert{T}(::Type{NullableMatrix{T}}, dt::AbstractDataTable)
n, p = size(dt)
res = NullableArray(T, n, p)
idx = 1
for col in columns(dt)
copy!(res, idx, col)
idx += n
end
return res
end
"""
Indexes of duplicate rows (a row that is a duplicate of a prior row)
```julia
nonunique(dt::AbstractDataTable)
nonunique(dt::AbstractDataTable, cols)
```
**Arguments**
* `dt` : the AbstractDataTable
* `cols` : a column indicator (Symbol, Int, Vector{Symbol}, etc.) specifying the column(s) to compare
**Result**
* `::Vector{Bool}` : indicates whether the row is a duplicate of some
prior row
See also [`unique`](@ref) and [`unique!`](@ref).
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
dt = vcat(dt, dt)
nonunique(dt)
nonunique(dt, 1)
```
"""
function nonunique(dt::AbstractDataTable)
gslots = row_group_slots(dt)[3]
# unique rows are the first encountered group representatives,
# nonunique are everything else
res = fill(true, nrow(dt))
@inbounds for g_row in gslots
(g_row > 0) && (res[g_row] = false)
end
return res
end
nonunique(dt::AbstractDataTable, cols::Union{Real, Symbol}) = nonunique(dt[[cols]])
nonunique(dt::AbstractDataTable, cols::Any) = nonunique(dt[cols])
unique!(dt::AbstractDataTable) = deleterows!(dt, find(nonunique(dt)))
unique!(dt::AbstractDataTable, cols::Any) = deleterows!(dt, find(nonunique(dt, cols)))
# Unique rows of an AbstractDataTable.
Base.unique(dt::AbstractDataTable) = dt[(!).(nonunique(dt)), :]
Base.unique(dt::AbstractDataTable, cols::Any) = dt[(!).(nonunique(dt, cols)), :]
"""
Delete duplicate rows
```julia
unique(dt::AbstractDataTable)
unique(dt::AbstractDataTable, cols)
unique!(dt::AbstractDataTable)
unique!(dt::AbstractDataTable, cols)
```
**Arguments**
* `dt` : the AbstractDataTable
* `cols` : column indicator (Symbol, Int, Vector{Symbol}, etc.)
specifying the column(s) to compare.
**Result**
* `::AbstractDataTable` : the updated version of `dt` with unique rows.
When `cols` is specified, the return DataTable contains complete rows,
retaining in each case the first instance for which `dt[cols]` is unique.
See also [`nonunique`](@ref).
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
dt = vcat(dt, dt)
unique(dt) # doesn't modify dt
unique(dt, 1)
unique!(dt) # modifies dt
```
"""
(unique, unique!)
function nonuniquekey(dt::AbstractDataTable)
# Here's another (probably a lot faster) way to do `nonunique`
# by grouping on all columns. It will fail if columns cannot be
# made into CategoricalVector's.
gd = groupby(dt, _names(dt))
idx = [1:length(gd.idx)][gd.idx][gd.starts]
res = fill(true, nrow(dt))
res[idx] = false
res
end
# Count the number of missing values in every column of an AbstractDataTable.
function colmissing(dt::AbstractDataTable) # -> Vector{Int}
nrows, ncols = size(dt)
missing = zeros(Int, ncols)
for j in 1:ncols
missing[j] = countnull(dt[j])
end
return missing
end
function without(dt::AbstractDataTable, icols::Vector{Int})
newcols = _setdiff(1:ncol(dt), icols)
dt[newcols]
end
without(dt::AbstractDataTable, i::Int) = without(dt, [i])
without(dt::AbstractDataTable, c::Any) = without(dt, index(dt)[c])
##############################################################################
##
## Hcat / vcat
##
##############################################################################
# hcat's first argument must be an AbstractDataTable
# Trailing arguments (currently) may also be NullableVectors, Vectors, or scalars.
# hcat! is defined in datatables/datatables.jl
# Its first argument (currently) must be a DataTable.
# catch-all to cover cases where indexing returns a DataTable and copy doesn't
Base.hcat(dt::AbstractDataTable, x) = hcat!(dt[:, :], x)
Base.hcat(dt::AbstractDataTable, x, y...) = hcat!(hcat(dt, x), y...)
# vcat only accepts DataTables. Finds union of columns, maintaining order
# of first dt. Missing data become null values.
Base.vcat(dt::AbstractDataTable) = dt
Base.vcat(dts::AbstractDataTable...) = vcat(AbstractDataTable[dts...])
function Base.vcat{T<:AbstractDataTable}(dts::Vector{T})
isempty(dts) && return DataTable()
coltyps, colnams, similars = _colinfo(dts)
res = DataTable()
Nrow = sum(nrow, dts)
for j in 1:length(colnams)
colnam = colnams[j]
col = similar(similars[j], coltyps[j], Nrow)
i = 1
for dt in dts
if haskey(dt, colnam)
copy!(col, i, dt[colnam])
end
i += size(dt, 1)
end
res[colnam] = col
end
res
end
_isnullable{T}(::AbstractArray{T}) = T <: Nullable
const EMPTY_DATA = NullableArray(Void, 0)
function _colinfo{T<:AbstractDataTable}(dts::Vector{T})
dt1 = dts[1]
colindex = copy(index(dt1))
coltyps = eltypes(dt1)
similars = collect(columns(dt1))
nonnull_ct = Int[_isnullable(c) for c in columns(dt1)]
for i in 2:length(dts)
dt = dts[i]
for j in 1:size(dt, 2)
col = dt[j]
cn, ct = _names(dt)[j], eltype(col)
if haskey(colindex, cn)
idx = colindex[cn]
oldtyp = coltyps[idx]
if !(ct <: oldtyp)
coltyps[idx] = promote_type(oldtyp, ct)
# Needed on Julia 0.4 since e.g.
# promote_type(Nullable{Int}, Nullable{Float64}) gives Nullable{T},
# which is not a usable type: fall back to Nullable{Any}
if VERSION < v"0.5.0-dev" &&
coltyps[idx] <: Nullable && !isa(coltyps[idx].types[2], DataType)
coltyps[idx] = Nullable{Any}
end
end
nonnull_ct[idx] += !_isnullable(col)
else # new column
push!(colindex, cn)
push!(coltyps, ct)
push!(similars, col)
push!(nonnull_ct, !_isnullable(col))
end
end
end
for j in 1:length(colindex)
if nonnull_ct[j] < length(dts) && !_isnullable(similars[j])
similars[j] = EMPTY_DATA
end
end
colnams = _names(colindex)
coltyps, colnams, similars
end
##############################################################################
##
## Hashing
##
## Make sure this agrees with isequals()
##
##############################################################################
function Base.hash(dt::AbstractDataTable)
h = hash(size(dt)) + 1
for i in 1:size(dt, 2)
h = hash(dt[i], h)
end
return @compat UInt(h)
end
## Documentation for methods defined elsewhere
"""
Number of rows or columns in an AbstractDataTable
```julia
nrow(dt::AbstractDataTable)
ncol(dt::AbstractDataTable)
```
**Arguments**
* `dt` : the AbstractDataTable
**Result**
* `::AbstractDataTable` : the updated version
See also [`size`](@ref).
NOTE: these functions may be depreciated for `size`.
**Examples**
```julia
dt = DataTable(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
size(dt)
nrow(dt)
ncol(dt)
```
"""
# nrow, ncol