Module: RedAmber::DataFrameDisplayable

Included in:
DataFrame
Defined in:
lib/red_amber/data_frame_displayable.rb

Overview

mix-ins for the class DataFrame

Constant Summary collapse

INDEX_KEY =
:index_key_for_format_table

Instance Method Summary collapse

Instance Method Details

#inspectObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/red_amber/data_frame_displayable.rb', line 39

def inspect
  mode = ENV.fetch('RED_AMBER_OUTPUT_MODE', 'Table')
  case mode.upcase
  when 'TDR'
    "#<#{shape_str(with_id: true)}>\n#{dataframe_info(3)}"
  when 'MINIMUM'
    shape_str
  else
    "#<#{shape_str(with_id: true)}>\n#{self}"
  end
end

#summaryDataFrame Also known as: describe

Show statistical summary by a new DatFrame.

Make stats for numeric columns only.
NaNs are ignored.
Counts also show non-NaN counts.

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/red_amber/data_frame_displayable.rb', line 22

def summary
  num_keys = keys.select { |key| self[key].numeric? }

  DataFrame.new(
    variables: num_keys,
    count: num_keys.map { |k| self[k].count },
    mean: num_keys.map { |k| self[k].mean },
    std: num_keys.map { |k| self[k].std },
    min: num_keys.map { |k| self[k].min },
    '25%': num_keys.map { |k| self[k].quantile(0.25) },
    median: num_keys.map { |k| self[k].median },
    '75%': num_keys.map { |k| self[k].quantile(0.75) },
    max: num_keys.map { |k| self[k].max }
  )
end

#tdr(limit = 10, tally: 5, elements: 5) ⇒ Object

  • limit: max num of Vectors to show

  • tally: max level to use tally mode

  • elements: max element to show values in each vector



54
55
56
# File 'lib/red_amber/data_frame_displayable.rb', line 54

def tdr(limit = 10, tally: 5, elements: 5)
  puts tdr_str(limit, tally: tally, elements: elements)
end

#tdr_str(limit = 10, tally: 5, elements: 5) ⇒ Object



58
59
60
# File 'lib/red_amber/data_frame_displayable.rb', line 58

def tdr_str(limit = 10, tally: 5, elements: 5)
  "#{shape_str}\n#{dataframe_info(limit, tally_level: tally, max_element: elements)}"
end

#to_irubyObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/red_amber/data_frame_displayable.rb', line 62

def to_iruby
  require 'iruby'
  return ['text/plain', '(empty DataFrame)'] if empty?

  mode = ENV.fetch('RED_AMBER_OUTPUT_MODE', 'Table')
  case mode.upcase
  when 'PLAIN'
    ['text/plain', inspect]
  when 'MINIMUM'
    ['text/plain', shape_str]
  when 'TDR'
    size <= 5 ? ['text/plain', tdr_str(tally: 0)] : ['text/plain', tdr_str]
  else # 'TABLE'
    ['text/html', html_table]
  end
end

#to_s(width: 80) ⇒ Object



10
11
12
13
14
# File 'lib/red_amber/data_frame_displayable.rb', line 10

def to_s(width: 80)
  return '' if empty?

  format_table(width: width)
end