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.

Where Next?

Popular in Questions Top

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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement