Packages

Flexible and powerful data analysis / manipulation library for Elixir, inspired by Pandas.

Current section

Files

Jump to
flask lib flask data.ex
Raw

lib/flask/data.ex

defmodule Flask.Data do
alias Flask.{DataFrame, Series}
defstruct contents: %{}, index: [], columns: [], shape: [1]
defimpl Inspect do
def inspect(data, opts) do
case length(data.shape) do
1 ->
Series.Inspect.inspect(data, opts)
2 ->
DataFrame.Inspect.inspect(data, opts)
end
end
end
def new_from_lists(data, index, columns) do
contents = data |> lists_to_nested_map()
shape = [length(index), length(columns)]
%__MODULE__{contents: contents, index: index, columns: columns, shape: shape}
end
defp lists_to_nested_map(list) do
list
|> Enum.with_index()
|> Enum.reduce(%{}, fn
{sublist, index}, map when is_list(sublist) ->
Map.put(map, index, lists_to_nested_map(sublist))
{item, index}, map ->
Map.put(map, index, item)
end)
end
@doc """
Converts the data as a nested list of values.
For a Series, returns a list of values
For a DataFrame, returns a list of lists of values
"""
def to_list(data) do
do_to_list(data.contents, data.shape)
end
defp do_to_list(_data_contents, [shape | _shapes]) when shape <= 0 do
[]
end
defp do_to_list(data_contents, [shape]) do
for x <- 0..(shape - 1) do
Map.get(data_contents, x)
end
end
defp do_to_list(data_contents, [shape | shapes]) do
for x <- 0..(shape - 1) do
do_to_list(Map.get(data_contents, x, %{}), shapes)
end
end
end