Module: Pandas::IO::Helpers
- Defined in:
- lib/pandas/io/active_record.rb
Class Method Summary collapse
- .data_frame_from_query_result(result, index_col, coerce_float, parse_dates) ⇒ Object
- .parse_read_sql_query_args(*args) ⇒ Object
- .parse_read_sql_table_args(*args) ⇒ Object
- .read_sql_query_from_active_record(query, conn, *args) ⇒ Object
- .read_sql_query_from_active_record_connection(query, conn, *args) ⇒ Object
- .read_sql_table_from_active_record(table_name, conn, *args) ⇒ Object
- .read_sql_table_from_active_record_connection(table_name, conn, *args) ⇒ Object
Class Method Details
.data_frame_from_query_result(result, index_col, coerce_float, parse_dates) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pandas/io/active_record.rb', line 54 def data_frame_from_query_result(result, index_col, coerce_float, parse_dates) records = result.map {|row| row.values } df = Pandas::DataFrame.from_records( records, columns: result.columns, coerce_float: coerce_float ) # TODO: self.sql._harmonize_columns(parse_dates: parse_dates) df.set_index(index_col, inplace: true) if index_col df end |
.parse_read_sql_query_args(*args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pandas/io/active_record.rb', line 83 def parse_read_sql_query_args(*args) kwargs = args.pop if args.last.is_a? Hash if kwargs names = [:index_col, :coerce_float, :parse_dates, :chunksize] names.each_with_index do |name, index| if kwargs.has_key? name if args[index] warn "#{name} is given as both positional and keyword arguments" else args[index] = kwargs[name] end end end end args end |
.parse_read_sql_table_args(*args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/pandas/io/active_record.rb', line 66 def parse_read_sql_table_args(*args) kwargs = args.pop if args.last.is_a? Hash if kwargs names = [:index_col, :coerce_float, :parse_dates, :columns, :schema, :chunksize] names.each_with_index do |name, index| if kwargs.has_key? name if args[index] warn "#{name} is given as both positional and keyword arguments" else args[index] = kwargs[name] end end end end args end |
.read_sql_query_from_active_record(query, conn, *args) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/pandas/io/active_record.rb', line 18 def read_sql_query_from_active_record(query, conn, *args) case conn when ActiveRecord::ConnectionAdapters::AbstractAdapter read_sql_query_from_active_record_connection(query, conn, *args) else raise TypeError, "unexpected type of argument #{conn.class}" end end |
.read_sql_query_from_active_record_connection(query, conn, *args) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/pandas/io/active_record.rb', line 46 def read_sql_query_from_active_record_connection(query, conn, *args) args = parse_read_sql_query_args(*args) index_col, coerce_float, parse_dates, chunksize = *args # TODO: chunksize result = conn.exec_query(query, 'pandas_sql') data_frame_from_query_result(result, index_col, coerce_float, parse_dates) end |
.read_sql_table_from_active_record(table_name, conn, *args) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/pandas/io/active_record.rb', line 9 def read_sql_table_from_active_record(table_name, conn, *args) case conn when ActiveRecord::ConnectionAdapters::AbstractAdapter read_sql_table_from_active_record_connection(table_name, conn, *args) else raise TypeError, "unexpected type of argument #{conn.class}" end end |
.read_sql_table_from_active_record_connection(table_name, conn, *args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pandas/io/active_record.rb', line 27 def read_sql_table_from_active_record_connection(table_name, conn, *args) args = parse_read_sql_table_args(*args) index_col, coerce_float, parse_dates, columns, schema, chunksize = *args if columns table_columns = conn.columns(table_name) column_names = columns.select {|c| table_columns.include?(c) }.map do |c| conn.quote_column_name(c.to_s) end else column_names = '*' end query = <<-SQL select #{column_names} from #{conn.quote_table_name(table_name)}; SQL # TODO: chunksize result = conn.exec_query(query, 'pandas_sql') data_frame_from_query_result(result, index_col, coerce_float, parse_dates) end |