Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ext/DimensionalDataPythonCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ function PythonCall.pyconvert(::Type{DimArray}, x::Py, d=nothing)

dim_names = Symbol.(collect(x.dims))
coord_names = Symbol.(collect(x.coords.keys()))
lookups_dict = Dict{Symbol, Any}()
for dim in dim_names
lookups_vec = Pair{Symbol, Any}[]
for dim in reverse(dim_names) # Iterate in reverse order because of row/col major
if dim in coord_names
coord = getproperty(x, dim).data
coord_type = dtype2type(string(coord.dtype.name))
coord_ndim = pyconvert(Int, coord.ndim)

lookups_dict[dim] = pyconvert(Array{coord_type, coord_ndim}, coord)
push!(lookups_vec, dim => pyconvert(Array{coord_type, coord_ndim}, coord))
else
lookups_dict[dim] = NoLookup()
push!(lookups_vec, dim => NoLookup())
end
end

lookups = NamedTuple(lookups_dict)
lookups = NamedTuple(lookups_vec)

metadata = pyconvert(Dict, x.attrs)

Expand Down
8 changes: 8 additions & 0 deletions test/xarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ x2 = xr.DataArray(data2,

@test_throws ArgumentError pyconvert(DimArray, xr)
@test pyconvert(DimArray, xr, 42) == 42

# Sanity test for higher-dimensional arrays
x3 = xr.DataArray(rand(2, 5, 5, 3),
dims=("w", "x", "y", "z"),
coords=Dict("w" => [1, 2], "z" => [1, 2, 3]))
y = pyconvert(DimArray, x3)
@test lookup(y, :w) == [1, 2]
@test lookup(y, :z) == [1, 2, 3]
end

@testset "Dataset to DimStack" begin
Expand Down
Loading