Class: Charty::TableAdapters::PandasDataFrameAdapter

Inherits:
BaseAdapter
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/charty/table_adapters/pandas_adapter.rb

Defined Under Namespace

Classes: GroupBy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

#==

Constructor Details

#initialize(data, columns: nil, index: nil) ⇒ PandasDataFrameAdapter

Returns a new instance of PandasDataFrameAdapter.



15
16
17
18
19
20
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 15

def initialize(data, columns: nil, index: nil)
  @data = check_type(Pandas::DataFrame, data, :data)

  self.columns = columns unless columns.nil?
  self.index = index unless index.nil?
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



22
23
24
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 22

def data
  @data
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 11

def self.supported?(data)
  defined?(Pandas::DataFrame) && data.is_a?(Pandas::DataFrame)
end

Instance Method Details

#[](row, column) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 79

def [](row, column)
  if row
    @data[column][row]
  else
    case column
    when Array
      Table.new(@data[column])
    else
      Vector.new(@data[column])
    end
  end
end

#[]=(key, values) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 92

def []=(key, values)
  case values
  when Charty::Vector
    case values.adapter
    when Charty::VectorAdapters::PandasSeriesAdapter
      @data[key] = values.adapter.data
    else
      @data[key] = values.to_a
    end
  else
    orig_values = values
    values = Array.try_convert(values)
    if values.nil?
      raise ArgumentError, "`values` must be convertible to Array"
    end
    @data[key] = values
  end
  return values
end

#column?(name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 45

def column?(name)
  data.__contains__(name)
end

#column_namesObject



66
67
68
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 66

def column_names
  @data.columns.to_a
end

#columnsObject



28
29
30
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 28

def columns
  PandasIndex.new(data.columns)
end

#columns=(new_columns) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 32

def columns=(new_columns)
  case new_columns
  when PandasIndex
    data.columns = new_columns.values
    data.columns.name = new_columns.name
  when Index
    data.columns = new_columns.to_a
    data.columns.name = new_columns.name
  else
    data.columns = new_columns
  end
end

#compare_data_equality(other) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 70

def compare_data_equality(other)
  case other
  when PandasDataFrameAdapter
    data.equals(other.data)
  else
    super
  end
end

#drop_naObject



112
113
114
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 112

def drop_na
  Charty::Table.new(@data.dropna)
end

#group_by(_table, grouper, sort, drop_na) ⇒ Object



125
126
127
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 125

def group_by(_table, grouper, sort, drop_na)
  GroupBy.new(@data.groupby(by: grouper, sort: sort, dropna: drop_na))
end

#indexObject



49
50
51
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 49

def index
  PandasIndex.new(data.index)
end

#index=(new_index) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 53

def index=(new_index)
  case new_index
  when PandasIndex
    data.index = new_index.values
    data.index.name = new_index.name
  when Index
    data.index = new_index.to_a
    data.index.name = new_index.name
  else
    data.index = new_index
  end
end

#lengthObject



24
25
26
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 24

def length
  data.shape[0]
end

#melt(id_vars: nil, value_vars: nil, var_name: nil, value_name: :value) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 133

def melt(id_vars: nil, value_vars: nil, var_name: nil, value_name: :value)
  id_vars = check_melt_vars(id_vars, :id_vars) { nil }
  value_vars = check_melt_vars(value_vars, :value_vars) { nil }

  Charty::Table.new(data.melt(id_vars: id_vars, value_vars: value_vars,
                              var_name: var_name, value_name: value_name,
                              ignore_index: true))
end

#reset_indexObject



129
130
131
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 129

def reset_index
  Charty::Table.new(data.reset_index)
end

#sort_values(by, na_position: :last) ⇒ Object



116
117
118
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 116

def sort_values(by, na_position: :last)
  Charty::Table.new(@data.sort_values(by, na_position: na_position, kind: :mergesort))
end