mooreryan
I have a couple of questions about Explorer’s DataFrame and the Access behaviour.
In the selecting columns section of the Explorer manual, it mentions that the Access behaviour is implemented for DataFrames.
Because of this I would expect that accessing a column that doesn’t exist with the bracket notation to return nil and using DataFrame.fetch to try an access a column that doesn’t exist to return :error.
However, this is not the case. Here are some examples.
Examples
df =
DataFrame.new(
a: [1, 2, 3],
b: [10, 20, 30]
)
And here are some ways to access it.
Using brackets
iex> df["a"]
#Explorer.Series<
Polars[3]
s64 [1, 2, 3]
iex> df["c"]
** (ArgumentError) could not find column name "c". The available columns are: ["a", "b"].
If you are attempting to interpolate a value, use ^c.
Trying to access column c raises an ArgumentError, but I would expect that to return nil given the Access behaviour.
Using fetch
Using fetch was also surprising:
iex> Explorer.DataFrame.fetch(df, "a")
{:ok,
#Explorer.Series<
Polars[3]
s64 [1, 2, 3]
>}
iex> Explorer.DataFrame.fetch(df, "c")
** (ArgumentError) could not find column name "c". The available columns are: ["a", "b"].
If you are attempting to interpolate a value, use ^c.
The first makes sense ({:ok, val}) but the second, raises an ArgumentError, where I would expect it to return :error given the Access behaviour.
I assume that this is the intended way for it to work given that it is included in the test suite. See this test for example, which shows that an ArgumentError is expected to be raised.
(The fact that it is an ArgumentError is also interesting given that fetch!/2 from Access raises a KeyError exception rather than ArgumentError.)
Other Access behaviour functions
Another interesting thing is that not all of the functions in the Access behaviour are available. E.g.,
iex> Explorer.DataFrame.fetch!(df, "c")
** (UndefinedFunctionError) function Explorer.DataFrame.fetch!/2 is undefined or private.
Summary
To summarize, here are my questions:
- Why does accessing columns in a
DataFramenot behave in the way that theAccessbehaviour docs imply that it should behave? - Why aren’t all the
Accessbehaviour functions available onDataFrame? - How are you supposed to check if a column exists in a
DataFrameother than using atryblock?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
The Explorer team has decided that it makes more sense to raise (catching errors early) than return nil. How much this is violation of the Access behaviour is a good question. Here is what we can say:
map.fooI’d have to put more thought into it but, in the face of the two conflicting positions below, I’d probably stick with the first one:
nilThe Access behaviour requires you only to implement
fetchandget_and_update, the other functionality is made available through theAccessmodule itself. It is similar to a GenServer, where you implementhandle_callbut you must invokeGenServer.call.We should add an API for it.
billylanchantin
You can also do:
It’s not perfect because
Accessis more inclusive. For example:So it’s a bit of a leaky abstraction. But depending on what you’re doing it may be good enough.
mooreryan
Thanks for the responses josevalim and billylanchantin!!
That makes sense. I was not aware that the return types for a behaviour were flexible.
This is interesting. I’m new to Elixir, so I can’t really say how it ought to be, but at least for me, the
DataFrameimplantation ofAccessbeing different from theAccessdocs was surprising. But that could be addressed by adding something toDataFramedocs.Thanks for the explanation. That makes sense.
Thanks, I will go with something like this for now.