@@ -55,11 +55,11 @@ function _columns(df, columnnames, columntypes)
5555 pycolumns = Py[]
5656 if df. indexname != = nothing
5757 push! (colnames, df. indexname)
58- push! (pycolumns, df. py. index)
58+ push! (pycolumns, df. py. index. values )
5959 end
6060 for pycolname in df. py. columns
6161 colname = columnnames (pycolname):: Symbol
62- pycolumn = df. py[pycolname]
62+ pycolumn = df. py[pycolname]. values
6363 push! (colnames, colname)
6464 push! (pycolumns, pycolumn)
6565 end
@@ -78,20 +78,28 @@ function _columns(df, columnnames, columntypes)
7878 for (colname, pycolumn) in zip (colnames, pycolumns)
7979 coltype = columntypes (colname):: Union{Nothing,Type}
8080 if coltype != = nothing
81- column = pyconvert_and_del (AbstractVector{coltype}, pycolumn)
81+ column = pyconvert (AbstractVector{coltype}, pycolumn)
8282 else
83- column = pyconvert_and_del (AbstractVector, pycolumn)
84- # narrow the type
85- column = identity .(column)
86- # convert any Py to something more useful
87- if Py <: eltype (column)
88- column = [x isa Py ? pyconvert (Any, x) : x for x in column]
89- end
90- # convert NaN to missing
91- if eltype (column) != Float64 && Float64 <: eltype (column)
92- column = [x isa Float64 && isnan (x) ? missing : x for x in column]
83+ column = pyconvert (AbstractVector, pycolumn)
84+ @show colname typeof (column)
85+ if eltype (column) == Py
86+ # guess a column type based on the types of the elements
87+ ts = pybuiltins. set (pybuiltins. map (pybuiltins. type, pycolumn))
88+ Ts = Type[pyconvert_preferred_type (t) for t in ts]
89+ T = isempty (Ts) ? Any : reduce (promote_type, Ts)
90+ column = pyconvert (AbstractVector{T}, pycolumn)
91+ @show ts Ts T typeof (column)
92+ # if all items are either NaN or not Float64, convert NaN to missing
93+ if T != Float64 && Float64 in Ts && ! any (x isa Float64 && ! isnan (x) for x in column)
94+ Ts = Type[T for T in Ts if T != Float64]
95+ push! (Ts, Missing)
96+ T = reduce (promote_type, Ts)
97+ column = pyconvert (AbstractVector{T}, pycolumn)
98+ @show Ts T typeof (column)
99+ end
93100 end
94101 end
102+ pydel! (pycolumn)
95103 push! (columns, column)
96104 push! (coltypes, eltype (column))
97105 end
0 commit comments