Two small problems with examples using 1.7beta3 and DataFrames 1.2.1
Closed this issue · 5 comments
Hello,
thank you for making this package available.
I believe there are two minor problems with the examples if a current julia and DataFrames version is used. Please find information regarding the julia version and package versions at the very bottom.
The mechanism to pass the key via environment variable does not appear to work as I would expect from the example:
% export ALPHA_VANTAGE_API_KEY=demo
julia> using AlphaVantage
julia> AlphaVantage.GLOBAL[]
AVClient("", "https://www.alphavantage.co/")
julia> get(ENV, "ALPHA_VANTAGE_API_KEY", "")
"demo"
julia> AVClient(key = get(ENV, "ALPHA_VANTAGE_API_KEY", ""))
AVClient("demo", "https://www.alphavantage.co/")
Not sure what additional information would be helpful here.
Second, the DataFrame call appears to have changed from the time the example was written:
julia> using AlphaVantage, DataFrames, Dates
julia> spy = time_series_daily("SPY", datatype="csv");
julia> data = DataFrame(spy[1]);
ERROR: ArgumentError: `DataFrame` constructor from a `Matrix` requires passing :auto as a second argument to automatically generate column names: `DataFrame(matrix, :auto)`
Stacktrace:
[1] DataFrame(matrix::Matrix{Any})
@ DataFrames ~/.julia/packages/DataFrames/pVFzb/src/dataframe/dataframe.jl:368
[2] top-level scope
@ REPL[4]:1
julia> data = DataFrame(spy[1], :auto);
julia> data
100×6 DataFrame
Row │ x1 x2 x3 x4 x5 x6
│ Any Any Any Any Any Any
─ ──┼──────────────────────────────────────────────────────────
1 │ 2021-07-30 437.91 440.06 437.77 438.51 68951202
Thank you for taking the time read this.
Best Regards
Version Info:
Julia Version: Version 1.7.0-beta3.0 (2021-07-07)
(@v1.7) pkg> status
Status `~/.julia/environments/v1.7/Project.toml`
[6348297c] AlphaVantage v0.3.0
[6e4b80f9] BenchmarkTools v1.1.1
[a93c6f00] DataFrames v1.2.1
[cd3eb016] HTTP v0.9.12
[682c06a0] JSON v0.21.1
[b8865327] UnicodePlots v1.3.0
@ckoegit thanks for the comments. Would you try the v0.4.0 branch and let me know if it works for you?
Hello @ellisvalentiner ,
I tested with the package versions given at the end of the reply. Passing the key via environment still appears to be problematic.
% export ALPHA_VANTAGE_API_KEY=demo
julia> using AlphaVantage
julia> AlphaVantage.GLOBAL[]
AlphaVantageClient("https", "", "www.alphavantage.co")
julia> get(ENV, "ALPHA_VANTAGE_API_KEY", "")
"demo"
I tested this with a bash and a zsh, same result.
Interestingly if I execute manually it appears to work to some degree:
julia> using AlphaVantage
julia> const GLOBAL = Ref(AlphaVantageClient(key = get(ENV, "ALPHA_VANTAGE_API_KEY", ""), host = get(ENV, "ALPHA_VANTAGE_HOST", "www.alphavantage.co")))
Base.RefValue{AlphaVantageClient}(AlphaVantageClient("https", "demo", "www.alphavantage.co"))
julia> AlphaVantage.GLOBAL[]
AlphaVantageClient("https", "", "www.alphavantage.co")
julia> AlphaVantage.GLOBAL
Base.RefValue{AlphaVantageClient}(AlphaVantageClient("https", "", "www.alphavantage.co"))
Same without using AlphaVantage
, i.e. in a new julia session:
julia> mutable struct AlphaVantageClient
scheme::String
key::String
host::String
end
julia> AlphaVantageClient(; scheme = "https", key = "", host = alphavantage_api) = AlphaVantageClient(scheme, key, host)
AlphaVantageClient
julia> const GLOBAL = Ref(AlphaVantageClient(key = get(ENV, "ALPHA_VANTAGE_API_KEY", ""), host = get(ENV, "ALPHA_VANTAGE_HOST", "www.alphavantage.co")))
Base.RefValue{AlphaVantageClient}(AlphaVantageClient("https", "demo", "www.alphavantage.co"))
julia> AlphaVantage.GLOBAL
ERROR: UndefVarError: AlphaVantage not defined
Stacktrace:
[1] top-level scope
@ REPL[4]:1
julia> GLOBAL[]
AlphaVantageClient("https", "demo", "www.alphavantage.co")
With this version I could not test the DataFrames import because I do not know how to set the key correctly, apparently the function is no longer available:
julia> AlphaVantage.global_key!("demo")
ERROR: UndefVarError: global_key! not defined
Stacktrace:
[1] getproperty(x::Module, f::Symbol)
@ Base ./Base.jl:35
[2] top-level scope
@ REPL[3]:1
Thank you for your help !
Best Regards
[6348297c] AlphaVantage v0.4.0 `https://github.com/ellisvalentiner/AlphaVantage.jl.git#v0.4.0`
[6e4b80f9] BenchmarkTools v1.1.1
[336ed68f] CSV v0.8.5
[5d742f6a] CSVFiles v1.0.1
[a93c6f00] DataFrames v1.2.2
[5789e2e9] FileIO v1.10.1
[92fee26a] GZip v0.5.1
[682c06a0] JSON v0.21.2
[b8865327] UnicodePlots v1.3.0
[a5390f91] ZipFile v0.9.3
@ckoegit Are you trying to sett the API key in the Julia REPL shell mode? I believe that wouldn't work. Instead the environment variable would need to be set before launching the REPL.
In v0.4.0 AlphaVantageClient
is a mutable struct so you can set/modify the values directly without needing dedicated functions. There's updated examples in the v0.4.0 README.
using AlphaVantage
# Set the API key
AlphaVantage.GLOBAL[].key = "demo"
# Check the key has been set
AlphaVantage.GLOBAL[]
Hello @ellisvalentiner,
I am setting the key as an environment variable before starting julia in the same shell. I am not using julias shell mode to set anything, which woul be pointless indeed. Full output in a fresh shell:
host 16:16 ~ % export ALPHA_VANTAGE_API_KEY=demo
host 16:16 ~ % /opt/ckoe/julia-e76c9dad42/bin/julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.7.0-beta3.0 (2021-07-07)
_/ |\__'_|_|_|\__'_| | release-1.7/e76c9dad42 (fork: 68 commits, 62 days)
|__/ |
julia> using AlphaVantage
julia> AlphaVantage.GLOBAL[]
AlphaVantageClient("https", "", "www.alphavantage.co")
julia> get(ENV, "ALPHA_VANTAGE_API_KEY", "")
"demo"
julia> AlphaVantage.GLOBAL[].key = "demo" # This works of course
"demo"
julia> AlphaVantage.GLOBAL[]
AlphaVantageClient("https", "demo", "www.alphavantage.co")
I did not think of reading the v0.4.0 Readme.md, sorry. With my real key the original problem persists with v0.4.0
julia> using DataFrames, Dates
julia> spy = time_series_daily("SPY", datatype="csv");
julia> data = DataFrame(spy[1]);
ERROR: ArgumentError: `DataFrame` constructor from a `Matrix` requires passing :auto as a second argument to automatically generate column names: `DataFrame(matrix, :auto)`
Stacktrace:
[1] DataFrame(matrix::Matrix{Any})
@ DataFrames ~/.julia/packages/DataFrames/vuMM8/src/dataframe/dataframe.jl:368
[2] top-level scope
@ REPL[10]:1
And setting :auto
as an additional argument as suggested by the error message solves that.
Sorry @ellisvalentiner, I should have paid attention. Of course the Readme.md also contains updated information about how to load the data into a DataFrame. Using the right instructions this step works.
julia> spy = time_series_daily("SPY");
julia> data = DataFrame(spy);
julia> data
100×6 DataFrame
Row │ timestamp open high low close volume
│ Any Any Any Any Any Any