vegabook

vegabook

Does Explorer have the Polars `gather_every` function and if not, how do I achieve the same thing?

I have a 40 x 120k row CSVs which I want to concatenate into a one single DataFrame. But these won’t fit into my 32GB memory, so I want to take every 10th row from each input DataFrame. How do I do that? Polars has gather_every (polars.DataFrame.gather_every — Polars documentation) but Explorer doesn’t seem to have it.

34   def mktax_all(skiprate \\ 10) do
 35     Ftp.local_paths_ls(:marketaxess)
 36     |> Enum.take(3)
 37     |> Enum.filter(fn x -> String.contains?(x, "mabond") end)
 38     |> Enum.map(&read_csv/1)
 39     |> Enum.filter(fn df -> DF.n_rows(df) > 0 end)
 40     |> Enum.map(fn df -> DF.filter_with(df,
 41         &Explorer.Series.equal(&1["SETTLEMENTDATE"], Explorer.Series.mode(df["SETTLEMENTDATE"])[0])) end)
 42     |> Enum.map(fn df -> DF.gather_every(df, skiprate) end)

EDIT

messy:

 41     |> Enum.map(fn df -> DF.slice(df, Enum.map(0..(Kernel.div(DF.n_rows(df), skiprate) - 1),
 42       fn x -> x * skiprate end)) end)

Marked As Solved

03juan

03juan

The chain of Enums are greedily creating a lot of intermediate data that you then end up dropping before the DF operations.

Consider using a lazy Stream to process each file up to the filter_with one at a time, and then join the results together at the end.

Ftp.local_paths_ls(:marketaxess)
|> Stream.take(3)
|> Stream.filter(fn x -> String.contains?(x, "mabond") end)
|> Stream.map(&read_csv/1)
|> Stream.filter(fn df -> DF.n_rows(df) > 0 end)
|> Enum.map(fn df ->
      DF.filter_with(df,
         &Explorer.Series.equal(&1["SETTLEMENTDATE"], 
           Explorer.Series.mode(df["SETTLEMENTDATE"])[0]))
   end)
|> DF.concat_rows()

As for your “messy” slicing (if it’s even necessary using the streams approach). DF.slice/2 can enumerate a lazy Elixir Range struct, so you could do:

|> Enum.map(fn df ->
      n_rows = DF.n_rows(df)
      range = 0..n_rows//skiprate # pretty sure DFs are 0-indexed

      df
      |> DF.filter_with(
         &Explorer.Series.equal(&1["SETTLEMENTDATE"], 
           Explorer.Series.mode(df["SETTLEMENTDATE"])[0])
         )
      |> DF.slice(range)
    end)
|> DF.concat_rows()

Also Liked

vegabook

vegabook

Feel compelled to say, as an R and Python data science expert, my initial explorations of Explorer are incredibly pleasant.

I was not expecting anything like this kind of performance, and the ergonomics are excellent, far better than Pandas, and a proper rival for the R (which is built from the ground up for wrangling). Excellent job so far.

Documentation super helpful too and an h away in the REPL which is great too.

vegabook

vegabook

Particularly love the // thing didn’t realise that was possible. And yeah I don’t need the skiprate anymore anyway, but nice to know.

Last Post!

vegabook

vegabook

Feel compelled to say, as an R and Python data science expert, my initial explorations of Explorer are incredibly pleasant.

I was not expecting anything like this kind of performance, and the ergonomics are excellent, far better than Pandas, and a proper rival for the R (which is built from the ground up for wrangling). Excellent job so far.

Documentation super helpful too and an h away in the REPL which is great too.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New

We're in Beta

About us Mission Statement