opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
data_frame.h
Go to the documentation of this file.
1#pragma once
2
3#include <libopynsim/series.h>
4
5#include <cstddef>
6#include <iosfwd>
7#include <optional>
8#include <string>
9#include <tuple>
10#include <unordered_map>
11#include <vector>
12
13namespace opyn
14{
16 class DataFrame final {
17 public:
19 using size_type = size_t;
21 using const_iterator = std::vector<Series>::const_iterator;
22
24 DataFrame() = default;
25
30 explicit DataFrame(
31 std::vector<std::string> column_names,
32 std::vector<std::vector<double>> column_data,
33 std::unordered_map<std::string, std::string> attrs = {}
34 );
35
37 explicit DataFrame(
38 std::vector<Series> series,
39 std::unordered_map<std::string, std::string> attrs = {}
40 );
41
48 bool operator==(const DataFrame&) const;
49
51 std::vector<std::string> columns() const;
52
54 std::tuple<size_t, size_t> shape() const;
55
57 size_t height() const;
58
60 size_t width() const;
61
63 std::unordered_map<std::string, std::string> attrs() const;
64
66 void set_attrs(std::unordered_map<std::string, std::string> new_attrs);
67
69 bool has_attr(const std::string& key) const;
70
73 std::optional<std::string> get_attr(const std::string& key) const;
74
78 const_reference operator[](std::string_view name) const;
79
81 const_reference operator[](size_type pos) const { return series_[pos]; }
82
84 size_type size() const { return series_.size(); }
85
87 [[nodiscard]] bool empty() const { return series_.empty(); }
88
90 const_iterator begin() const { return series_.begin(); }
91
93 const_iterator end() const { return series_.end(); }
94
98 const_iterator find(std::string_view name) const;
99
112 private:
113 std::vector<Series> series_;
114 std::unordered_map<std::string, size_t> column_to_index_lookup_;
115 std::unordered_map<std::string, std::string> attrs_;
116 };
117
119 std::ostream& operator<<(std::ostream& out, const DataFrame& data_frame);
120}
Represents a sequence of Series with optional associated metadata.
Definition data_frame.h:16
size_t height() const
Returns the number of rows in *this.
DataFrame(std::vector< std::string > column_names, std::vector< std::vector< double > > column_data, std::unordered_map< std::string, std::string > attrs={})
const_iterator find(std::string_view name) const
size_t size_type
Definition data_frame.h:19
bool operator==(const DataFrame &) const
DataFrame()=default
Constructs an empty DataFrame.
size_type size() const
Returns the number of Series (columns) in *this.
Definition data_frame.h:84
const_reference operator[](std::string_view name) const
const_iterator begin() const
Returns an iterator to the first Series of *this.
Definition data_frame.h:90
bool empty() const
Returns true if *this contains no Series, false otherwise.
Definition data_frame.h:87
std::optional< std::string > get_attr(const std::string &key) const
std::unordered_map< std::string, std::string > attrs() const
Returns this DataFrame's metadata (e.g. header key-values).
std::vector< std::string > columns() const
Returns this DataFrame's column labels.
void set_attrs(std::unordered_map< std::string, std::string > new_attrs)
Sets the attrs of this DataFrame to new_attrs.
bool has_attr(const std::string &key) const
Returns true if *this has a metadata entry with a key of key.
size_t width() const
Returns the number of Series (columns) in *this. Equivalent to size().
DataFrame(std::vector< Series > series, std::unordered_map< std::string, std::string > attrs={})
Constructs a DataFrame with the given series.
const_reference operator[](size_type pos) const
Returns a reference to the nth series at pos in *this (by-column).
Definition data_frame.h:81
std::vector< Series >::const_iterator const_iterator
Definition data_frame.h:21
DataFrame with_series(Series series) const
const_iterator end() const
Returns an iterator past the last Series of *this.
Definition data_frame.h:93
std::tuple< size_t, size_t > shape() const
Returns the shape (rows, columns) of this DataFrame.
Represents a single column of a DataFrame.
Definition series.h:13
Definition component_registry.h:14
std::ostream & operator<<(std::ostream &out, const DataFrame &data_frame)
Writes a pretty-printed representation of data_frame to out.