Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
DataFrame Class Reference

Simple column-oriented data structure with basic analysis utilities. More...

#include <DataFrame.h>

Public Member Functions

 DataFrame ()
 Default constructor.
 
template<typename T >
void addColumn (const std::string &colName)
 Adds a new empty column of type T.
 
template<typename T >
void addValueToColumn (const std::string &colName, const T &value)
 Appends a value to an existing column.
 
template<typename T >
T iat (size_t row, const std::string &colName) const
 Positional access to a cell (row index, column name).
 
template<typename T >
T at (const std::string &idxValue, const std::string &colName) const
 Label-based access to a cell (index label, column name).
 
template<typename T >
Series< T > & operator[] (const std::string &colName)
 Accessor for a column as a modifiable Series<T>.
 
void print () const
 Prints the DataFrame to std::cout in a tabular format.
 
const std::vector< std::string > & getColumnNames () const
 Returns the list of column names.
 
size_t getRowCount () const
 Returns the number of rows in the DataFrame.
 
DataFrame head (size_t n=5) const
 Returns a new DataFrame with the first n rows.
 
DataFrame tail (size_t n=5) const
 Returns a new DataFrame with the last n rows.
 
template<typename T >
void describeColumn (const std::string &colName) const
 Prints a statistical summary for a single numeric column.
 
void describe () const
 Prints a summary for all numeric columns.
 
template<typename T >
Series< TgetColumn (const std::string &colName) const
 Returns a copy of a column as a Series<T>.
 
void setIndex (const std::vector< std::string > &newIndex)
 Sets the index labels for all rows.
 
const std::vector< std::string > & getIndex () const
 Returns the current index labels.
 
void to_csv (const std::string &filename)
 Serializes the DataFrame to a CSV file.
 
void _set_csv_options (const CSVOptions &options)
 Internal helper to set CSV options from an external source.
 

Public Attributes

std::vector< std::string > columns
 List of column names, in insertion order.
 
std::vector< std::string > index
 Optional index labels for the rows (if set).
 
std::array< int, 2 > shape = {0,0}
 Shape of the DataFrame: {nRows, nCols}.
 

Detailed Description

Simple column-oriented data structure with basic analysis utilities.

The DataFrame class stores columns as Series<T>, where T is typically int, double or std::string. Internally, columns are held in a map:

  • key : column name (std::string),
  • value : std::shared_ptr<void> that actually points to Series<T>.

Column types are tracked via a CSVOptions instance, which also controls CSV export behavior (presence of index, etc.).

Supported features include:

  • Adding columns and values dynamically,
  • Row-wise and label-based access (iat / at),
  • Simple statistical description for numeric columns,
  • Head / tail extraction,
  • CSV export via to_csv().

Definition at line 61 of file DataFrame.h.

Constructor & Destructor Documentation

◆ DataFrame()

DataFrame::DataFrame ( )
inline

Default constructor.

Constructs an empty DataFrame with an empty index.

Definition at line 93 of file DataFrame.h.

Member Function Documentation

◆ _set_csv_options()

void DataFrame::_set_csv_options ( const CSVOptions options)

Internal helper to set CSV options from an external source.

This copies the hasIndex flag and the columnTypes mapping, if provided. Intended for use by CSV readers or DataFrame methods that construct new DataFrames (e.g. head/tail).

Parameters
optionsOptions to copy into this DataFrame.

Definition at line 238 of file DataFrame.cpp.

◆ addColumn()

template<typename T >
void DataFrame::addColumn ( const std::string &  colName)

Adds a new empty column of type T.

Creates a new Series<T> with the given name and registers it in the internal column map. The column type is also recorded in csvOptions.columnTypes.

Template Parameters
TValue type of the column.
Parameters
colNameName of the new column.

◆ addValueToColumn()

template<typename T >
void DataFrame::addValueToColumn ( const std::string &  colName,
const T value 
)

Appends a value to an existing column.

The column must already exist and be of type T. If the column's size exceeds the current number of rows, the DataFrame row count and shape are updated accordingly.

Template Parameters
TValue type of the column.
Parameters
colNameName of the target column.
valueValue to append.
Exceptions
std::invalid_argumentif the column does not exist.

◆ at()

template<typename T >
T DataFrame::at ( const std::string &  idxValue,
const std::string &  colName 
) const

Label-based access to a cell (index label, column name).

The DataFrame must have an index set (via setIndex). The method searches the index for idxValue and returns the cell at the corresponding row and column.

Template Parameters
TExpected type of the column.
Parameters
idxValueRow label to look up in the index.
colNameName of the column.
Returns
Value at the matching row and column.
Exceptions
std::invalid_argumentif the index is not set or if the label or column is not found.

◆ describe()

void DataFrame::describe ( ) const

Prints a summary for all numeric columns.

For each column whose type in csvOptions is int or double, describeColumn() is called. Non-numeric columns are reported as ignored.

Definition at line 141 of file DataFrame.cpp.

◆ describeColumn()

template<typename T >
void DataFrame::describeColumn ( const std::string &  colName) const

Prints a statistical summary for a single numeric column.

The summary includes:

  • count, mean, standard deviation,
  • min, quartiles (25%, 50%, 75%), and max.
Template Parameters
TNumeric type of the column (e.g. int or double).
Parameters
colNameName of the column to describe.
Exceptions
std::invalid_argumentif the column does not exist.
std::runtime_errorif the series is empty.

◆ getColumn()

template<typename T >
Series< T > DataFrame::getColumn ( const std::string &  colName) const

Returns a copy of a column as a Series<T>.

Template Parameters
TValue type of the column.
Parameters
colNameName of the column.
Returns
Copy of the Series<T> representing this column.
Exceptions
std::invalid_argumentif the column does not exist or is not of type T.

◆ getColumnNames()

const std::vector< std::string > & DataFrame::getColumnNames ( ) const

Returns the list of column names.

Definition at line 66 of file DataFrame.cpp.

◆ getIndex()

const std::vector< std::string > & DataFrame::getIndex ( ) const

Returns the current index labels.

Returns
Reference to the index vector.
Exceptions
std::runtime_errorif the index is not set.

Definition at line 206 of file DataFrame.cpp.

◆ getRowCount()

size_t DataFrame::getRowCount ( ) const

Returns the number of rows in the DataFrame.

Definition at line 70 of file DataFrame.cpp.

◆ head()

DataFrame DataFrame::head ( size_t  n = 5) const

Returns a new DataFrame with the first n rows.

Column metadata and csvOptions are propagated; index is truncated consistently if present.

Parameters
nNumber of rows to keep (clamped to available rows).
Returns
A new DataFrame containing the top n rows.

Definition at line 74 of file DataFrame.cpp.

◆ iat()

template<typename T >
T DataFrame::iat ( size_t  row,
const std::string &  colName 
) const

Positional access to a cell (row index, column name).

Template Parameters
TExpected type of the column.
Parameters
rowZero-based row index.
colNameName of the column.
Returns
Value at (row, colName).
Exceptions
std::invalid_argumentif the column does not exist.
std::out_of_rangeif the row index is invalid.

◆ operator[]()

template<typename T >
Series< T > & DataFrame::operator[] ( const std::string &  colName)

Accessor for a column as a modifiable Series<T>.

If the column does not exist yet, it is created as an empty Series<T> with the given name.

Template Parameters
TColumn value type.
Parameters
colNameName of the column.
Returns
Reference to the underlying Series<T>.

◆ print()

void DataFrame::print ( ) const

Prints the DataFrame to std::cout in a tabular format.

Uses csvOptions.columnTypes to determine how to print each column (int, double, std::string). Index labels are printed in the first column if present.

Definition at line 27 of file DataFrame.cpp.

◆ setIndex()

void DataFrame::setIndex ( const std::vector< std::string > &  newIndex)

Sets the index labels for all rows.

The size of newIndex must match the current number of rows (unless the DataFrame is still empty). The underlying Series<T> of each column is updated to share the same index.

Parameters
newIndexVector of labels to use as row index.
Exceptions
std::invalid_argumentif the size does not match nRows.

Definition at line 214 of file DataFrame.cpp.

◆ tail()

DataFrame DataFrame::tail ( size_t  n = 5) const

Returns a new DataFrame with the last n rows.

Column metadata and csvOptions are propagated; index is truncated consistently if present.

Parameters
nNumber of rows to keep (clamped to available rows).
Returns
A new DataFrame containing the bottom n rows.

Definition at line 109 of file DataFrame.cpp.

◆ to_csv()

void DataFrame::to_csv ( const std::string &  filename)

Serializes the DataFrame to a CSV file.

The header line contains the column names. If csvOptions.hasIndex is true, an "Index" column is written first and each row begins with its index (here: the integer row position).

Column types come from csvOptions.columnTypes and must be known (int, double, std::string) for all columns.

Parameters
filenamePath to the target CSV file.
Exceptions
std::runtime_erroron I/O errors or missing type info.

Definition at line 156 of file DataFrame.cpp.

Member Data Documentation

◆ columns

std::vector<std::string> DataFrame::columns

List of column names, in insertion order.

Definition at line 76 of file DataFrame.h.

◆ index

std::vector<std::string> DataFrame::index

Optional index labels for the rows (if set).

Definition at line 79 of file DataFrame.h.

◆ shape

std::array<int, 2> DataFrame::shape = {0,0}

Shape of the DataFrame: {nRows, nCols}.

shape[0] is the number of rows, shape[1] the number of columns.

Definition at line 86 of file DataFrame.h.


The documentation for this class was generated from the following files: