Class used to handle data read from csv files. More...

#include <dataFrame.hpp>

Public Member Functions

 DataFrame (std::string file_name, char delimiter, bool first_row_is_column_names, int primery_key=0)
 Constructor. More...
 
std::string operator() (const int col, const int row)
 Reads cell in c-th column and r-th row. More...
 
std::string operator() (const std::string col, const int row)
 Reads cell in column named c and r-th row. More...
 
std::string operator() (const std::string col, const std::string row)
 Reads cell in column name and row holding primary key r. More...
 
std::vector< std::string > operator() (int col)
 Reads column basic on column number. More...
 
std::vector< std::string > operator() (std::string col)
 Reads column basic on column number. More...
 
double getDouble (const int col, const int row)
 Reads cell in c-th column and r-th row. More...
 
double getDouble (const std::string col, const int row)
 Reads cell in column named c and r-th row. More...
 
double getDouble (const std::string col, const std::string row)
 Reads cell in column name and row holding primary key r. More...
 
Date getDate (const int col, const int row, Date::Format fmt)
 Reads cell in c-th column and r-th row. More...
 
Date getDate (const std::string col, const int row, Date::Format fmt)
 Reads cell in column named c and r-th row. More...
 
Date getDate (const std::string col, const std::string row, Date::Format fmt)
 Reads cell in column name and row holding primary key r. More...
 
std::vector< std::string > getColumnNames () const
 Returns column names as vector of strings. More...
 
std::vector< std::string > getPrimaryKeys () const
 Returns primary keys as vector of strings. More...
 
std::map< std::string, std::string > getRow (int row) const
 Returns row in form of std::map. More...
 
void append (const DataEntryClerk &)
 adds data stored by DataEntryClerk More...
 
void append (const DataFrame &)
 Appends one data frame to another. More...
 
void print (int n_rows=-1)
 Prints DataFrame. More...
 
void printToCsv (std::string file_name, char delimiter= ';')
 prints DataFrame to csv file More...
 
int getNumberOfRows () const
 Returns number of rows. More...
 
int getNumberOfColumns () const
 Returns number of columns. More...
 
template<class T >
SmartPointer< T > getObject (const int col, const int row)
 Reads cell in c-th column and r-th row. More...
 
template<class T >
SmartPointer< T > getObject (const std::string col, const int row)
 Reads cell in column named c and r-th row. More...
 
template<class T >
SmartPointer< T > getObject (const std::string col, const std::string row)
 Reads cell in column name and row holding primary key r. More...
 
template<class T >
getValue (const int col, const int row)
 Reads cell in c-th column and r-th row. More...
 
template<class T >
getValue (const std::string col, const int row)
 Reads cell in column named c and r-th row. More...
 
template<class T >
getValue (const std::string col, const std::string row)
 Reads cell in column name and row holding primary key r. More...
 
template<class T >
DataFrame filter (int col, T filter)
 Filter the data frame basing on data hold in column. More...
 
template<class T >
DataFrame filter (std::string col, T filter)
 Filter the data frame basing on data hold in column. More...
 

Private Member Functions

void parseFile (std::string file_name, char delimiter)
 parse csv file, result of method is saved in DataFrame::data_ More...
 
void createColumnNames (bool first_row)
 creates columns names basing on the first row of csv file More...
 

Private Attributes

std::vector< std::vector< std::string > > data_
 Holds data as 2d table of strings. More...
 
std::map< std::string, int > column_names_
 Maps column name to column number. More...
 
std::map< std::string, int > primary_key_
 Maps row name to row number. More...
 
int ncols_ = 0
 number of columns More...
 
int nrows_ = 0
 number of rows More...
 

Friends

std::ostream & operator<< (std::ostream &, DataFrame &)
 Overloads stream operator. More...
 

Detailed Description

Class used to handle data read from csv files.

Class enables to load the csv file and read it contents. One can access the single cell or read whole columns. Class allows to choose cell using column and row numbers or, if defined, column names and primary keys.

Examples:
algebraicBootstrapperExample.cpp, constrainedBootstrapperExample.cpp, DataFrameExample.cpp, interpolationExample.cpp, optionPricingExample.cpp, rootFindingBootstrapperExample.cpp, and unconstrainedBootstrapperExample.cpp.

Constructor & Destructor Documentation

julian::DataFrame::DataFrame ( std::string  file_name,
char  delimiter,
bool  first_row_is_column_names,
int  primery_key = 0 
)

Constructor.

Parameters
file_nameFile name that will be read.
delimiterSeparation sign in csv file.
first_row_is_column_namesTrue if first row contains column names.
primery_keyDefines which column contains primary key of the table.

Create DataFrame on the basis of provided file.

Member Function Documentation

void julian::DataFrame::append ( const DataEntryClerk entry)

adds data stored by DataEntryClerk

Data stored in DataEntryClerK:data_ are added. If new columns are added, old rows are filled with "." representing missing.

Examples:
algebraicBootstrapperExample.cpp, constrainedBootstrapperExample.cpp, DataFrameExample.cpp, interpolationExample.cpp, optionPricingExample.cpp, rootFindingBootstrapperExample.cpp, and unconstrainedBootstrapperExample.cpp.
void julian::DataFrame::append ( const DataFrame appended)

Appends one data frame to another.

void julian::DataFrame::createColumnNames ( bool  first_row)
private

creates columns names basing on the first row of csv file

Remarks
Method used by constructor. Bad practice, so better keep the method non-virtual.
template<class T >
DataFrame julian::DataFrame::filter ( int  col,
filter 
)

Filter the data frame basing on data hold in column.

Parameters
colcolumn identification
filterFunctor with overloaded operator bool operator()(std::string), if functor returns true row is added to DataFrame that is returned by the method
Examples:
algebraicBootstrapperExample.cpp, constrainedBootstrapperExample.cpp, DataFrameExample.cpp, rootFindingBootstrapperExample.cpp, and unconstrainedBootstrapperExample.cpp.
template<class T >
DataFrame julian::DataFrame::filter ( std::string  col,
filter 
)

Filter the data frame basing on data hold in column.

Parameters
colcolumn identification
filterFunctor with overloaded operator bool operator()(std::string), if functor returns true row is added to DataFrame that is returned by the method
Note
May be used only when column names were defined.
std::vector< std::string > julian::DataFrame::getColumnNames ( ) const

Returns column names as vector of strings.

Date julian::DataFrame::getDate ( const int  col,
const int  row,
Date::Format  fmt 
)

Reads cell in c-th column and r-th row.

Parameters
colcolumn name
rowrow number
fmtDate format (compare Date::Format)
Returns
Returns content of cell as a date formatted according to fmt
Date julian::DataFrame::getDate ( const std::string  col,
const int  row,
Date::Format  fmt 
)

Reads cell in column named c and r-th row.

Parameters
colcolumn name
rowrow number
fmtDate format (compare Date::Format)
Returns
Returns content of cell as a date formatted according to fmt
Note
May be used only when column names were defined.
Date julian::DataFrame::getDate ( const std::string  col,
const std::string  row,
Date::Format  fmt 
)

Reads cell in column name and row holding primary key r.

Parameters
colcolumn name
rowrow number
fmtDate format (compare Date::Format)
Returns
Returns content of cell as a date formatted according to fmt
Note
May be used only when column names and primary keys were defined.
double julian::DataFrame::getDouble ( const int  col,
const int  row 
)

Reads cell in c-th column and r-th row.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell as double
Examples:
constrainedBootstrapperExample.cpp, and unconstrainedBootstrapperExample.cpp.
double julian::DataFrame::getDouble ( const std::string  col,
const int  row 
)

Reads cell in column named c and r-th row.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell as double
Note
May be used only when column names were defined.
double julian::DataFrame::getDouble ( const std::string  col,
const std::string  row 
)

Reads cell in column name and row holding primary key r.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell as double
Note
May be used only when column names and primary keys were defined.
int julian::DataFrame::getNumberOfColumns ( ) const

Returns number of columns.

int julian::DataFrame::getNumberOfRows ( ) const
template<class T >
SmartPointer< T > julian::DataFrame::getObject ( const int  col,
const int  row 
)

Reads cell in c-th column and r-th row.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell. Method uses ObjectFactory to transform string into object.
Examples:
algebraicBootstrapperExample.cpp, constrainedBootstrapperExample.cpp, rootFindingBootstrapperExample.cpp, and unconstrainedBootstrapperExample.cpp.
template<class T >
SmartPointer< T > julian::DataFrame::getObject ( const std::string  col,
const int  row 
)

Reads cell in column named c and r-th row.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell. Method uses ObjectFactory to transform string into object.
Note
May be used only when column names were defined.
template<class T >
SmartPointer< T > julian::DataFrame::getObject ( const std::string  col,
const std::string  row 
)

Reads cell in column name and row holding primary key r.

Parameters
colcolumn name
rowrow name
Returns
Returns content of cell as string Method uses ObjectFactory to transform string into object.
Note
May be used only when column names and primary keys were defined.
std::vector< std::string > julian::DataFrame::getPrimaryKeys ( ) const

Returns primary keys as vector of strings.

std::map< std::string, std::string > julian::DataFrame::getRow ( int  row) const

Returns row in form of std::map.

template<class T >
T julian::DataFrame::getValue ( const int  col,
const int  row 
)

Reads cell in c-th column and r-th row.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell. Method uses ValueFactory to transform string into enumeration.
template<class T >
T julian::DataFrame::getValue ( const std::string  col,
const int  row 
)

Reads cell in column named c and r-th row.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell. Method uses ValueFactory to transform string into enumeration.
Note
May be used only when column names were defined.
template<class T >
T julian::DataFrame::getValue ( const std::string  col,
const std::string  row 
)

Reads cell in column name and row holding primary key r.

Parameters
colcolumn name
rowrow name
Returns
Returns content of cell as string Method uses ValueFactory to transform string into enumeration.
Note
May be used only when column names and primary keys were defined.
std::string julian::DataFrame::operator() ( const int  col,
const int  row 
)

Reads cell in c-th column and r-th row.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell as string
std::string julian::DataFrame::operator() ( const std::string  col,
const int  row 
)

Reads cell in column named c and r-th row.

Parameters
colcolumn name
rowrow number
Returns
Returns content of cell as string
Note
May be used only when column names were defined.
std::string julian::DataFrame::operator() ( const std::string  col,
const std::string  row 
)

Reads cell in column name and row holding primary key r.

Parameters
colcolumn name
rowrow name
Returns
Returns content of cell as string
Note
May be used only when column names and primary keys were defined.
std::vector< std::string > julian::DataFrame::operator() ( int  col)

Reads column basic on column number.

Parameters
colcolumn number
Returns
Returns column as vector of string
std::vector< std::string > julian::DataFrame::operator() ( std::string  col)

Reads column basic on column number.

Parameters
colcolumn number
Returns
Returns column as vector of string
Note
May be used only when column names were defined.
void julian::DataFrame::parseFile ( std::string  file_name,
char  delimiter 
)
private

parse csv file, result of method is saved in DataFrame::data_

Remarks
Method used by constructor. Bad practice, so better keep the method non-virtual.
Bug:
Function reads EOL sign All but last rows are read with EOL sign. This cause the problem with comparing signs. Below if statement solves the problem by cutting of the last char if it is a EOL. Not sure if this is a save solution.
void julian::DataFrame::print ( int  n_rows = -1)

Prints DataFrame.

Examples:
DataFrameExample.cpp.
void julian::DataFrame::printToCsv ( std::string  file_name,
char  delimiter = ';' 
)

prints DataFrame to csv file

Parameters
file_name
delimiterseparator used in csv file, by default set to semicolon
Examples:
algebraicBootstrapperExample.cpp, constrainedBootstrapperExample.cpp, interpolationExample.cpp, optionPricingExample.cpp, rootFindingBootstrapperExample.cpp, and unconstrainedBootstrapperExample.cpp.

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  s,
DataFrame df 
)
friend

Overloads stream operator.

This overloaded operator enables to print the curve on the console.

Member Data Documentation

std::map<std::string,int> julian::DataFrame::column_names_
private

Maps column name to column number.

std::vector<std::vector<std::string> > julian::DataFrame::data_
private

Holds data as 2d table of strings.

int julian::DataFrame::ncols_ = 0
private

number of columns

int julian::DataFrame::nrows_ = 0
private

number of rows

std::map<std::string,int> julian::DataFrame::primary_key_
private

Maps row name to row number.


The documentation for this class was generated from the following files:
  • C:/Unix/home/OEM/jULIAN/src/utils/dataFrame.hpp
  • C:/Unix/home/OEM/jULIAN/src/utils/dataFrame.cpp