Module: RedAmber::DataFrameVariableOperation
- Included in:
- DataFrame
- Defined in:
- lib/red_amber/data_frame_variable_operation.rb
Overview
Mix-in for the class DataFrame
Instance Method Summary collapse
-
#assign ⇒ Object
Assign new or updated variables (columns) and create an updated DataFrame.
-
#assign_left ⇒ Object
Assign new or updated variables (columns) and create an updated DataFrame.
-
#drop(*args, &block) ⇒ Object
Drop off some variables (columns) to create a remainer DataFrame.
-
#pick(*args, &block) ⇒ Object
Select variables (columns) to create a new DataFrame.
-
#rename(*renamer, &block) ⇒ Object
rename keys (variable/column names) to create a updated DataFrame.
Instance Method Details
#assign(key_value_pairs) ⇒ DataFrame #assign {|self| ... } ⇒ DataFrame #assign(keys) {|self| ... } ⇒ DataFrame
Assign new or updated variables (columns) and create an updated DataFrame.
-
Array-like variables with new keys will append new columns from right.
-
Array-like variables with exisiting keys will update corresponding vectors.
-
Symbol key and String key are considered as the same key.
-
If assigner is empty or nil, returns self.
515 516 517 |
# File 'lib/red_amber/data_frame_variable_operation.rb', line 515 def assign(...) assign_update(false, ...) end |
#assign_left(key_value_pairs) ⇒ DataFrame #assign_left {|self| ... } ⇒ DataFrame #assign_left(keys) {|self| ... } ⇒ DataFrame
Assign new or updated variables (columns) and create an updated DataFrame.
-
Array-like variables with new keys will append new columns from left.
-
Array-like variables with exisiting keys will update corresponding vectors.
-
Symbol key and String key are considered as the same key.
-
If assigner is empty or nil, returns self.
586 587 588 |
# File 'lib/red_amber/data_frame_variable_operation.rb', line 586 def assign_left(...) assign_update(true, ...) end |
#drop(keys) ⇒ DataFrame #drop(booleans) ⇒ DataFrame #drop(indices) ⇒ DataFrame #drop {|self| ... } ⇒ DataFrame
DataFrame#drop creates a DataFrame even if it is a single column (not a Vector).
Drop off some variables (columns) to create a remainer DataFrame.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/red_amber/data_frame_variable_operation.rb', line 251 def drop(*args, &block) if block unless args.empty? raise DataFrameArgumentError, 'Must not specify both arguments and block.' end args = [instance_eval(&block)] end return self if args.compact.empty? || empty? picker = if args.symbol? keys - args elsif args.boolean? keys.reject_by_booleans(args) elsif args.integer? keys.reject_by_indices(args) else dropper = parse_args(args, n_keys) if dropper.compact.empty? return self elsif dropper.boolean? keys.reject_by_booleans(dropper) elsif dropper.symbol? keys - dropper else dropper.compact! unless dropper.integer? raise DataFrameArgumentError, "Invalid argument #{args}" end keys.reject_by_indices(dropper) end end return DataFrame.new if picker.empty? DataFrame.create(@table.select_columns(*picker)) end |
#pick(keys) ⇒ DataFrame #pick(booleans) ⇒ DataFrame #pick(indices) ⇒ DataFrame #pick {|self| ... } ⇒ DataFrame
if a single key is specified, DataFrame#pick generates a DataFrame. On the other hand, DataFrame#[] generates a Vector.
Select variables (columns) to create a new DataFrame.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/red_amber/data_frame_variable_operation.rb', line 117 def pick(*args, &block) if block unless args.empty? raise DataFrameArgumentError, 'Must not specify both arguments and block.' end args = [instance_eval(&block)] end case args in [] | [nil] return DataFrame.new in [*] if args.symbol? return DataFrame.create(@table.select_columns(*args)) in [*] if args.boolean? picker = keys.select_by_booleans(args) return DataFrame.create(@table.select_columns(*picker)) in [(Vector | Arrow::Array | Arrow::ChunkedArray) => a] picker = a.to_a else picker = parse_args(args, n_keys) end return DataFrame.new if picker.compact.empty? if picker.boolean? picker = keys.select_by_booleans(picker) return DataFrame.create(@table.select_columns(*picker)) end picker.compact! raise DataFrameArgumentError, "some keys are duplicated: #{args}" if picker.uniq! return self if picker == keys DataFrame.create(@table.select_columns(*picker)) end |
#rename(key_pairs) ⇒ DataFrame #rename(key_pairs) ⇒ DataFrame #rename {|self| ... } ⇒ DataFrame
rename keys (variable/column names) to create a updated DataFrame.
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/red_amber/data_frame_variable_operation.rb', line 370 def rename(*renamer, &block) if block unless renamer.empty? raise DataFrameArgumentError, 'Must not specify both arguments and a block' end renamer = [instance_eval(&block)] end case renamer in [] | [nil] | [{}] | [[]] return self in [Hash => key_pairs] # noop in [ (Symbol | String) => from, (Symbol | String) => to] key_pairs = { from => to } in [Array => array_in_array] key_pairs = try_convert_to_hash(array_in_array) in [Array, *] => array_in_array1 key_pairs = try_convert_to_hash(array_in_array1) else raise DataFrameArgumentError, "Invalid argument #{renamer}" end rename_by_hash(key_pairs) end |