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



75
76
77
78
79
80
81
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 75

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

#column_namesObject



62
63
64
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 62

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



66
67
68
69
70
71
72
73
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 66

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

#drop_naObject



83
84
85
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 83

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

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



96
97
98
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 96

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

#indexObject



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

def index
  PandasIndex.new(data.index)
end

#index=(new_index) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 49

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

#reset_indexObject



100
101
102
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 100

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

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



87
88
89
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 87

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