Skip to content

Commit 96df926

Browse files
authored
Merge pull request #59 from davidanthoff/get_columns_copy_using_missing
Support TableTraits.get_columns_copy_using_missing
2 parents f7f3bee + 2b37f09 commit 96df926

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ DataValues = "≥ 0.4.4"
1919
IteratorInterfaceExtensions = "^0.1.1, ^1"
2020
Lazy = "≥ 0.11.4"
2121
PyCall = "≥ 1.90.0"
22-
TableTraits = "^0.3.1, ^0.4, ^1"
22+
TableTraits = "^0.4, ^1"
2323
TableTraitsUtils = "≥ 0.3.0"
2424
julia = "≥ 0.7.0"

src/tabletraits.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ function TableTraits.getiterator(df::DataFrame)
1414
return create_tableiterator(column_data, col_names)
1515
end
1616

17+
TableTraits.supports_get_columns_copy_using_missing(df::DataFrame) = true
18+
19+
function TableTraits.get_columns_copy_using_missing(df::Pandas.DataFrame)
20+
# return a named tuple of columns here
21+
col_names_raw = [i for i in Pandas.columns(df)]
22+
col_names = Symbol.(col_names_raw)
23+
cols = (Array(eltype(df[i])==String ? [df[i][j] for j=1:length(df)] : df[i]) for i in col_names_raw)
24+
return NamedTuple{tuple(col_names...)}(tuple(cols...))
25+
end
26+
1727
function _construct_pandas_from_iterabletable(source)
1828
y = create_columns_from_iterabletable(source, errorhandling=:returnvalue)
1929
y===nothing && return nothing

test/test_tabletraits.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ it_collected = collect(it)
2828
@test it_collected[1] == (a=1, b="John", c=3.2)
2929
@test it_collected[2] == (a=2, b="Sally", c=5.8)
3030

31+
@test TableTraits.supports_get_columns_copy_using_missing(df) == true
32+
cols = TableTraits.get_columns_copy_using_missing(df)
33+
@test cols == (a=[1,2], b=["John", "Sally"], c=[3.2, 5.8])
34+
3135
table_array2 = [(a=1, b=DataValue("John"), c=3.2), (a=2, b=DataValue("Sally"), c=5.8)]
3236

3337
@test_throws ArgumentError DataFrame(table_array2)
@@ -46,4 +50,7 @@ it3_collected = collect(IteratorInterfaceExtensions.getiterator(df3))
4650
@test it3_collected[2].b == "Sally"
4751
@test isnan(it3_collected[2].c)
4852

53+
cols3 = TableTraits.get_columns_copy_using_missing(df3)
54+
@test isequal(cols3, (a=[NaN,2.], b=["John", "Sally"], c=[3.2, NaN]))
55+
4956
end

0 commit comments

Comments
 (0)