Back

Serie<T>

Overview

The Serie class is a column of data with elements of the same type, providing the fundamental building block of the DataFrame library. It supports a wide range of operations through a functional programming interface.

Declaration


          template <typename T> class Serie : public SerieBase {
            // Members and methods...
          };
        

Constructors

Constructors

Serie(); // Default constructor
Serie(const ArrayType &values); // From vector
Serie(const std::initializer_list<T> &values); // From initializer list
explicit Serie(size_t size); // With size
Serie(size_t size, const T &value); // With size and default value

Key Methods

Method Description
size() Returns the number of elements in the Serie.
empty() Checks if the Serie is empty.
operator[](size_t) Access element at the given index.
add(const T&) Add a value to the end of the Serie.
data() Get the underlying array of values.
asArray() Get the underlying array of values (alias for data()).
forEach(F&&) Apply a function to each element.
map(F&&) Apply a function to each element and return a new Serie.
reduce(F&&, AccT) Reduce the Serie to a single value using an accumulator.
type() Get the type of the Serie as a string.

Example Usage

Basic Example

// Create a Serie from an initializer list
df::Serie<double> values{1.0, 2.0, 3.0, 4.0, 5.0};

// Create an empty Serie and add values
df::Serie<std::string> names;
names.add("Alice");
names.add("Bob");
names.add("Charlie");

// Create a Serie of a specific size with default values
df::Serie<int> zeros(10, 0);

// Access elements
double first = values[0];  // 1.0
std::string last = names[2];  // "Charlie"

// Get the size
size_t count = values.size();  // 5

// Check if empty
bool is_empty = values.empty();  // false
Functional Operations

// Create a sample Serie
df::Serie<int> numbers{1, 2, 3, 4, 5};

// Apply forEach to print each element
numbers.forEach([](int value, size_t index) {
    std::cout << "Element " << index << ": " << value << std::endl;
});

// Map to create a new Serie with doubled values
auto doubled = numbers.map([](int value, size_t) {
    return value * 2;
});  // {2, 4, 6, 8, 10}

// Reduce to calculate the sum
int sum = numbers.reduce([](int acc, int value, size_t) {
    return acc + value;
}, 0);  // 15
Working with Operators

// Create two Series
df::Serie<double> a{1.0, 2.0, 3.0, 4.0, 5.0};
df::Serie<double> b{5.0, 4.0, 3.0, 2.0, 1.0};

// Element-wise addition
auto sum = a + b;  // {6.0, 6.0, 6.0, 6.0, 6.0}

// Using with mathematical functions
auto squared = a.map([](double x, size_t) { return x * x; });
auto sqrt_values = squared.map([](double x, size_t) { return std::sqrt(x); });