📦 SQA::DataFrame¶
Description
High-performance DataFrame wrapper around Polars for time series data manipulation. Provides convenience methods for stock market data while leveraging Polars' Rust-backed performance for vectorized operations.
🏭 Class Methods¶
.is_date?(value)¶
Checks if a value appears to be a date string.
Parameters
| Name | Type | Description |
|---|---|---|
value |
Object |
Value to check |
Returns
Type: Boolean
true if value matches YYYY-MM-DD format
Source Location
.load(source:, transformers: = {}, mapping: = {})¶
Load a DataFrame from a file source This is the primary method for loading persisted DataFrames
Note: For cached CSV files, transformers and mapping should typically be empty since transformations were already applied when the data was first fetched. We only apply them if the CSV has old-format column names that need migration.
Parameters
| Name | Type | Description |
|---|---|---|
source |
String, Pathname |
Path to CSV file |
transformers |
Hash |
Column transformations to apply (usually not needed for cached data) |
mapping |
Hash |
Column name mappings (usually not needed for cached data) |
Returns
Type: SQA::DataFrame
Loaded DataFrame
Source Location
.from_aofh(aofh, mapping: = {}, transformers: = {})¶
Creates a DataFrame from an array of hashes.
Parameters
| Name | Type | Description |
|---|---|---|
aofh |
Array<Hash> |
Array of hash records |
mapping |
Hash |
Column name mappings to apply |
transformers |
Hash |
Column transformers to apply |
Returns
Type: SQA::DataFrame
New DataFrame instance
Usage Examples
Source Location
.from_csv_file(source, mapping: = {}, transformers: = {})¶
Creates a DataFrame from a CSV file.
Parameters
| Name | Type | Description |
|---|---|---|
source |
String, Pathname |
Path to CSV file |
mapping |
Hash |
Column name mappings to apply |
transformers |
Hash |
Column transformers to apply |
Returns
Type: SQA::DataFrame
New DataFrame instance
Source Location
.from_json_file(source, mapping: = {}, transformers: = {})¶
Creates a DataFrame from a JSON file.
Parameters
| Name | Type | Description |
|---|---|---|
source |
String, Pathname |
Path to JSON file containing array of objects |
mapping |
Hash |
Column name mappings to apply |
transformers |
Hash |
Column transformers to apply |
Returns
Type: SQA::DataFrame
New DataFrame instance
Source Location
.generate_mapping(keys)¶
Generates a mapping of original keys to underscored keys.
Parameters
| Name | Type | Description |
|---|---|---|
keys |
Array<String> |
Original key names |
Returns
Type: Hash{String => Symbol}
Mapping from original to underscored keys
Source Location
.underscore_key(key)¶
Converts a key string to underscored snake_case format.
Parameters
| Name | Type | Description |
|---|---|---|
key |
String |
Key to convert |
Returns
Type: Symbol
Underscored key as symbol
Usage Examples
Source Location
.sanitize_key()¶
Converts a key string to underscored snake_case format.
Parameters
| Name | Type | Description |
|---|---|---|
key |
String |
Key to convert |
Returns
Type: Symbol
Underscored key as symbol
Usage Examples
Source Location
.normalize_keys(hash, adapter_mapping: = {})¶
Normalizes all keys in a hash to snake_case format.
Parameters
| Name | Type | Description |
|---|---|---|
hash |
Hash |
Hash with keys to normalize |
adapter_mapping |
Hash |
Optional pre-mapping to apply first |
Returns
Type: Hash
Hash with normalized keys
Source Location
.rename(hash, mapping)¶
Renames keys in a hash according to a mapping.
Parameters
| Name | Type | Description |
|---|---|---|
hash |
Hash |
Hash to modify |
mapping |
Hash |
Old key to new key mapping |
Returns
Type: Hash
Modified hash
Source Location
.aofh_to_hofa(aofh, mapping: = {}, transformers: = {})¶
Converts array of hashes to hash of arrays format.
Parameters
| Name | Type | Description |
|---|---|---|
aofh |
Array<Hash> |
Array of hash records |
mapping |
Hash |
Column name mappings (unused, for API compatibility) |
transformers |
Hash |
Column transformers (unused, for API compatibility) |
Returns
Type: Hash{String => Array}
Hash with column names as keys and arrays as values
Source Location
🔨 Instance Methods¶
#data()¶
Returns
Type: Polars::DataFrame
The underlying Polars DataFrame
Source Location
#data=(value)¶
Sets the attribute data
Parameters
| Name | Type | Description |
|---|---|---|
value |
Any |
the value to set the attribute data to. |
Source Location
#initialize(raw_data = nil, mapping: = {}, transformers: = {})¶
Creates a new DataFrame instance.
Parameters
| Name | Type | Description |
|---|---|---|
raw_data |
Hash, Array, Polars::DataFrame, nil |
Initial data for the DataFrame |
mapping |
Hash |
Column name mappings to apply (old_name => new_name) |
transformers |
Hash |
Column transformers to apply (column => lambda) |
Returns
Type: DataFrame
a new instance of DataFrame
Usage Examples
Source Location
#apply_transformers!(transformers)¶
Applies transformer functions to specified columns in place.
Parameters
| Name | Type | Description |
|---|---|---|
transformers |
Hash{String, Symbol => Proc} |
Column name to transformer mapping |
Returns
Type: void
Usage Examples
Source Location
#rename_columns!(mapping)¶
Renames columns according to the provided mapping in place.
Parameters
| Name | Type | Description |
|---|---|---|
mapping |
Hash{String, Symbol => String} |
Old column name to new column name mapping |
Returns
Type: void
Source Location
#append!(other_df)¶
Appends another DataFrame to this one in place.
Parameters
| Name | Type | Description |
|---|---|---|
other_df |
SQA::DataFrame |
DataFrame to append |
Returns
Type: void
Source Location
#concat!()¶
Appends another DataFrame to this one in place.
Parameters
| Name | Type | Description |
|---|---|---|
other_df |
SQA::DataFrame |
DataFrame to append |
Returns
Type: void
Source Location
#concat_and_deduplicate!(other_df, sort_column: = "timestamp", descending: = false)¶
Concatenate another DataFrame, remove duplicates, and sort This is the preferred method for updating CSV data to prevent duplicates
NOTE: TA-Lib requires data in ascending (oldest-first) order. Using descending: true will produce a warning and force ascending order to prevent silent calculation errors.
Parameters
| Name | Type | Description |
|---|---|---|
other_df |
SQA::DataFrame |
DataFrame to append |
sort_column |
String |
Column to use for deduplication and sorting (default: "timestamp") |
descending |
Boolean |
Sort order - false for ascending (oldest first, TA-Lib compatible), true for descending |
Usage Examples
stock = SQA::Stock.new(ticker: 'AAPL')
df = stock.df
df.size # => 252
# Fetch recent data (may have overlapping dates)
new_df = SQA::DataFrame::AlphaVantage.recent('AAPL', from_date: Date.today - 7)
df.concat_and_deduplicate!(new_df)
# Duplicates removed, data sorted ascending (oldest first)
df.size # => 255 (only 3 new unique dates added)
Source Location
#columns()¶
Returns the column names of the DataFrame.
Returns
Type: Array<String>
List of column names
Source Location
#keys()¶
Returns the column names of the DataFrame. Alias for {#columns}.
Returns
Type: Array<String>
List of column names
Source Location
#vectors()¶
Returns the column names of the DataFrame. Alias for {#columns}.
Returns
Type: Array<String>
List of column names
Source Location
#to_h()¶
Converts the DataFrame to a Ruby Hash.
Returns
Type: Hash{Symbol => Array}
Hash with column names as keys and column data as arrays
Source Location
#to_csv(path_to_file)¶
Writes the DataFrame to a CSV file.
Parameters
| Name | Type | Description |
|---|---|---|
path_to_file |
String, Pathname |
Path to output CSV file |
Returns
Type: void
Usage Examples
Source Location
#size()¶
Returns the number of rows in the DataFrame.
Returns
Type: Integer
Row count
Source Location
#nrows()¶
Returns the number of rows in the DataFrame.
Returns
Type: Integer
Row count
Source Location
#length()¶
Returns the number of rows in the DataFrame.
Returns
Type: Integer
Row count
Source Location
#ncols()¶
Returns the number of columns in the DataFrame.
Returns
Type: Integer
Column count
Source Location
#fpl(column: = 'adj_close_price', fpop: = 14)¶
FPL Analysis - Calculate Future Period Loss/Profit
Parameters
| Name | Type | Description |
|---|---|---|
column |
String, Symbol |
Column name containing prices (default: "adj_close_price") |
fpop |
Integer |
Future Period of Performance (days to look ahead) |
Returns
Type: Array<Array<Float, Float>>
Array of [min_delta, max_delta] pairs
Source Location
#fpl_analysis(column: = 'adj_close_price', fpop: = 14)¶
FPL Analysis with risk metrics and classification
Parameters
| Name | Type | Description |
|---|---|---|
column |
String, Symbol |
Column name containing prices (default: "adj_close_price") |
fpop |
Integer |
Future Period of Performance |
Returns
Type: Array<Hash>
Array of analysis hashes
Usage Examples
Source Location
#method_missing(method_name, *args, &block)¶
Delegates unknown methods to the underlying Polars DataFrame. This allows direct access to Polars methods like filter, select, etc.
Parameters
| Name | Type | Description |
|---|---|---|
method_name |
Symbol |
Method name being called |
args |
Array |
Method arguments |
block |
Proc |
Optional block |
Returns
Type: Object
Result from Polars DataFrame method
Source Location
#respond_to_missing?(method_name, include_private = false)¶
Checks if the DataFrame responds to a method.
Parameters
| Name | Type | Description |
|---|---|---|
method_name |
Symbol |
Method name to check |
include_private |
Boolean |
Include private methods |
Returns
Type: Boolean
true if method is available