Module: RedAmber::DataFrameDisplayable
- Included in:
- DataFrame
- Defined in:
- lib/red_amber/data_frame_displayable.rb
Overview
Mix-in for the class DataFrame
Instance Method Summary collapse
-
#inspect ⇒ String
Show information of self.
-
#shape_str(with_id: false) ⇒ String
Return class and shape of self by a String.
-
#summary ⇒ DataFrame
(also: #describe)
Show statistical summary by a new DataFrame.
-
#tdr(limit = 10, tally: 5, elements: 5) ⇒ nil
(also: #glimpse)
Shows some information about self in a transposed style.
-
#tdr_str(limit = 10, tally: 5, elements: 5) ⇒ String
Returns some information about self in a transposed style by a string.
-
#tdra(tally: 5, elements: 5) ⇒ nil
Shortcut for ‘tdr(:all)`.
-
#to_iruby ⇒ String
Returns html formatted text of self by IRuby::HTML.table.
-
#to_s(width: 90, head: 5, tail: 4) ⇒ String
Show a preview of self as a string.
Instance Method Details
#inspect ⇒ String
Show information of self.
According to ‘ENV [“RED_AMBER_OUTPUT_MODE”].upcase`,
-
If it is ‘TDR’, returns class name, shape, object id and transposed preview for up to 10 variables.
-
If it is ‘TDRA’, returns class name, shape, object id and transposed preview for all variables.
-
If it is ‘MINIMUM’, returns class name and shape.
-
If it is ‘PLAIN’, returns class name, shape and Table preview for up to 512 columns and 128 columns.
-
If it is ‘TABLE’ or otherwise, returns class name, shape, object id and Table preview for up to 512 rows and 512 columns. Default value of the ENV is ‘Table’.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/red_amber/data_frame_displayable.rb', line 129 def inspect mode = ENV.fetch('RED_AMBER_OUTPUT_MODE', 'Table') case mode.upcase when 'TDR' "#<#{shape_str(with_id: true)}>\n#{dataframe_info(10)}" when 'TDRA' "#<#{shape_str(with_id: true)}>\n#{dataframe_info(:all)}" when 'MINIMUM' shape_str when 'PLAIN' "#<#{shape_str}>\n#{to_s(width: 128, head: 128)}" else "#<#{shape_str(with_id: true)}>\n#{to_s(width: 100, head: 20)}" end end |
#shape_str(with_id: false) ⇒ String
Return class and shape of self by a String.
342 343 344 345 346 |
# File 'lib/red_amber/data_frame_displayable.rb', line 342 def shape_str(with_id: false) shape_info = empty? ? '(empty)' : "#{size} x #{n_keys} Vector#{pl(n_keys)}" id = with_id ? format(', 0x%016x', object_id) : '' "#{self.class} : #{shape_info}#{id}" end |
#summary ⇒ DataFrame Also known as: describe
Show statistical summary by a new DataFrame.
This method will make stats only for numeric columns.
-
NaNs are ignored.
-
‘count` shows non-NaN counts.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/red_amber/data_frame_displayable.rb', line 70 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) ⇒ nil Also known as: glimpse
Shows some information about self in a transposed style.
267 268 269 |
# File 'lib/red_amber/data_frame_displayable.rb', line 267 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) ⇒ String
Returns some information about self in a transposed style by a string.
289 290 291 |
# File 'lib/red_amber/data_frame_displayable.rb', line 289 def tdr_str(limit = 10, tally: 5, elements: 5) "#{shape_str}\n#{dataframe_info(limit, tally_level: tally, max_element: elements)}" end |
#tdra(tally: 5, elements: 5) ⇒ nil
Shortcut for ‘tdr(:all)`.
277 278 279 |
# File 'lib/red_amber/data_frame_displayable.rb', line 277 def tdra(tally: 5, elements: 5) puts tdr_str(:all, tally: tally, elements: elements) end |
#to_iruby ⇒ String
Returns html formatted text of self by IRuby::HTML.table.
According to ‘ENV [“RED_AMBER_OUTPUT_MODE”].upcase`,
-
If it is ‘MINIMUM’, returns shape by plain text.
-
If it is ‘PLAIN’, returns ‘#inspect` value by plain text.
-
If it is ‘TDR’, returns shape and transposed preview by plain text.
-
If it is ‘TDRA’, returns shape and transposed preview by plain text.
-
If it is ‘TABLE’ or otherwise, returns Table preview by html format. Default value of the ENV is ‘TABLE’.
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/red_amber/data_frame_displayable.rb', line 305 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] when 'TDRA' ['text/plain', tdr_str(:all)] else # 'TABLE' ['text/html', html_table] end end |
#to_s(width: 90, head: 5, tail: 4) ⇒ String
Show a preview of self as a string.
44 45 46 47 48 |
# File 'lib/red_amber/data_frame_displayable.rb', line 44 def to_s(width: 90, head: 5, tail: 4) return '' if empty? format_table(width: width, head: head, tail: tail) end |