Module: RedAmber::DataFrameIndexable

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

Overview

Mix-ins for the class DataFrame

Instance Method Summary collapse

Instance Method Details

#indicesVector #indices(start) ⇒ Vector Also known as: indexes

Returns row index Vector.

Overloads:

  • #indicesVector

    return @indices as row indices (0…size).

    Examples:

    When ‘dataframe.size == 5`;

    dataframe.indices
    
    # =>
    #<RedAmber::Vector(:uint8, size=5):0x000000000000fb54>
    [0, 1, 2, 3, 4]

    Returns:

    • (Vector)

      a Vector of row indices.

  • #indices(start) ⇒ Vector

    return customized index Vector ‘(start..).take(size)`.

    Examples:

    When ‘dataframe.size == 5`;

    dataframe.indices(1)
    
    # =>
    #<RedAmber::Vector(:uint8, size=5):0x000000000000fba4>
    [1, 2, 3, 4, 5]
    
    dataframe.indices('a')
    # =>
    #<RedAmber::Vector(:string, size=5):0x000000000000fbb8>
    ["a", "b", "c", "d", "e"]

    Parameters:

    • start (#succ)

      element of start which have ‘#succ` method.

    Returns:

    • (Vector)

      a Vector of row indices.



39
40
41
42
43
44
45
# File 'lib/red_amber/data_frame_indexable.rb', line 39

def indices(start = 0)
  if start == 0 # rubocop:disable Style/NumericPredicate
    @indices ||= Vector.new(0...size)
  else
    Vector.new((start..).take(size))
  end
end

#sort(*sort_keys) ⇒ RedAmber::DataFrame

Sort the contents of self.

Examples:

Sort by a key

df

# =>
        x y
  <uint8> <string>
  0       3 B
  1       5 A
  2       1 B
  3       4 A
  4       2 C

df.sort('y')

# =>
#<RedAmber::DataFrame : 5 x 2 Vectors, 0x000000000000382c>
        x y
  <uint8> <string>
0       5 A
1       4 A
2       3 B
3       1 B
4       2 C

Sort by two keys

df.sort('y', 'x')

# =>
#<RedAmber::DataFrame : 5 x 2 Vectors, 0x0000000000003890>
        x y
  <uint8> <string>
0       4 A
1       5 A
2       1 B
3       3 B
4       2 C

Sort in descending order

df.sort('-x')

# =>
#<RedAmber::DataFrame : 5 x 2 Vectors, 0x0000000000003840>
        x y
  <uint8> <string>
0       5 A
1       4 A
2       3 B
3       2 C
4       1 B

Parameters:

  • sort_keys (Arrow::SortKey)

    :key, “key” or “+key” denotes ascending, :“-key” or “-key” denotes descending order.

Returns:



135
136
137
138
139
# File 'lib/red_amber/data_frame_indexable.rb', line 135

def sort(*sort_keys)
  indices = @table.sort_indices(sort_keys.flatten)

  take(indices)
end

#sort_indices(*sort_keys) ⇒ RedAmber::Vector

Return sorted indexes of self by a Vector.

Examples:

df

# =>
        x y
  <uint8> <string>
  0       3 B
  1       5 A
  2       1 B
  3       4 A
  4       2 C

df.sort_indices('x')

# =>
#<RedAmber::Vector(:uint64, size=5):0x0000000000003854>
[2, 4, 0, 3, 1]

Parameters:

  • sort_keys (Arrow::SortKey)

    :key, “key” or “+key” denotes ascending, :“-key” or “-key” denotes descending order.

Returns:



73
74
75
76
# File 'lib/red_amber/data_frame_indexable.rb', line 73

def sort_indices(*sort_keys)
  indices = @table.sort_indices(sort_keys.flatten)
  Vector.create(indices)
end