Skip to content

Commit

Permalink
Merge pull request #437 from JuliaDataCubes/la/Tables
Browse files Browse the repository at this point in the history
sort dataset variables and add TimeArray example
  • Loading branch information
lazarusA committed Sep 12, 2024
2 parents 1a38f32 + 353f5c9 commit 4a85fa4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ GeoMakie = "db073c08-6b98-4ee5-b6a4-5efafb3259c6"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
MarketData = "945b72a4-3b13-509d-9b46-1525bb5c06de"
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e"
PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043"
Rasters = "a3a2b9e3-a471-40c9-b274-f788e487c689"
SkipNan = "aed68c70-c8b0-4309-8cd1-d392a74f991a"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
TimeSeries = "9e3dc215-6440-5c97-bce1-76c03772f85e"
WGLMakie = "276b4fcb-3e11-5398-bf8b-a0c2d153d008"
WeightedOnlineStats = "bbac0a1f-7c9d-5672-960b-c6ca726e5d5d"
YAXArrayBase = "90b8fcef-0c2d-428d-9c56-5f86629e9d14"
Expand Down
48 changes: 47 additions & 1 deletion docs/src/UserGuide/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ We can also use more than one criteria for grouping the values. In the next exam
fitcube = cubefittable(t, Mean, :values, by=(:classes, :time))
````

## How do I assing variable names to `YAXArrays` in a `Dataset`
## How do I assign variable names to `YAXArrays` in a `Dataset`

### One variable name

Expand All @@ -312,3 +312,49 @@ nothing # hide
````@ansi howdoi
ds = YAXArrays.Dataset(; (keylist .=> varlist)...)
````

## Ho do I construct a `Dataset` from a TimeArray

In this section we will use `MarketData.jl` and `TimeSeries.jl` to simulate some stocks.

````@example howdoi
using YAXArrays, DimensionalData
using MarketData, TimeSeries
stocks = Dict(:Stock1 => random_ohlcv(), :Stock2 => random_ohlcv(), :Stock3 => random_ohlcv())
d_keys = keys(stocks)
````

currently there is not direct support to obtain `dims` from a `TimeArray`, but we can code a function for it

````@example howdoi
getTArrayAxes(ta::TimeArray) = (Dim{:time}(timestamp(ta)), Dim{:variable}(colnames(ta)), );
nothing # hide
````
then, we create the `YAXArrays` as

````@example howdoi
yax_list = [YAXArray(getTArrayAxes(stocks[k]), values(stocks[k])) for k in d_keys];
nothing # hide
````

and a `Dataset` with all `stocks` names

````@ansi howdoi
ds = Dataset(; (d_keys .=> yax_list)...)
````

and, it looks like there some small differences in the axes, they are being printed independently although they should be the same. Well, they are at least at the `==` level but not at `===`. We could use the axes from one `YAXArray` as reference and `rebuild` all the others

````@example howdoi
yax_list = [rebuild(yax_list[1], values(stocks[k])) for k in d_keys];
nothing # hide
````

and voilà

````@ansi howdoi
ds = Dataset(; (d_keys .=> yax_list)...)
````

now they are printed together, showing that is exactly the same axis structure for all variables.
4 changes: 2 additions & 2 deletions src/DatasetAPI/Datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function Base.show(io::IO, ds::Dataset)
if !isempty(variables_with_shared_axes_only)
printstyled(io, "Variables: ", color=:light_blue)
print(io, "\n")
println(io, join(variables_with_shared_axes_only, ", "))
println(io, join(sort(variables_with_shared_axes_only), ", "))
println(io)
end

Expand All @@ -146,7 +146,7 @@ function Base.show(io::IO, ds::Dataset)
end
printstyled(io, " Variables: ", color=:light_blue)
padding = " " ^ 2 # Adjust this number to match the length of " Variables: "
variables_str = join(variables, ", ")
variables_str = join(sort(variables), ", ")
padded_variables = padding * variables_str
print(io, "\n")
println(io, padded_variables)
Expand Down

0 comments on commit 4a85fa4

Please sign in to comment.