Skip to content

Commit

Permalink
Make 'values' work on dataframes.
Browse files Browse the repository at this point in the history
  • Loading branch information
malmaud committed Jul 26, 2022
1 parent 8f7e475 commit 902e9b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Pandas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,17 @@ end


function Base.values(x::PandasWrapped)
# Zero-copy conversion to a Julia native type is possible
x_kind = x.pyo.dtype.kind
if x_kind in ["i", "u", "f", "b"]
pyarray = convert(PyArray, x.pyo."values")
unsafe_wrap(Array, pyarray.data, size(pyarray))
else # Convert element by element otherwise
collect(x)
# Check if zero-copy conversion to a Julia native type
# is possible.
if hasproperty(x.pyo, :dtype)
x_kind = x.pyo.dtype.kind
if x_kind in ["i", "u", "f", "b"]
pyarray = convert(PyArray, x.pyo."values")
return unsafe_wrap(Array, pyarray.data, size(pyarray))
end
end
# Convert element by element otherwise
Array(x)
end

"""
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ df3 = Pandas.Series(3:4)
@test all(df1 == df1)
@test all(df1 == df2)
@test df1 != [1, 2]

# Issue #93
df = DataFrame(:a=>[1,2], :b=>[4,5], :c=>["a","b"])
@test values(df) == [1 4 "a"; 2 5 "b"]

0 comments on commit 902e9b0

Please sign in to comment.