Signature | Description | Parameters |
---|---|---|
template<typename S, typename ... Ts> bool write(S &o, bool values_only = false, io_format iof = io_format::csv) const; |
It outputs the content of DataFrame into the stream o. Currently two formats (i.e. csv, json) are supported specified by the iof parameter. The csv file format must be: INDEX:<Number of data points>:<Comma delimited list of values> <Column1 name>:<Number of data points>:<Column1 type>:<Comma delimited list of values> <Column2 name>:<Number of data points>:<Column2 type>:<Comma delimited list of values> . . .All empty lines or lines starting with # will be skipped. The JSON file format looks like this: { "INDEX":{"N":3,"T":"ulong","D":[123450,123451,123452]}, "col_3":{"N":3,"T":"double","D":[15.2,16.34,17.764]}, "col_4":{"N":3,"T":"int","D":[22,23,24]}, "col_str":{"N":3,"T":"string","D":["11","22","33"]}, "col_2":{"N":3,"T":"double","D":[8,9.001,10]}, "col_1":{"N":3,"T":"double","D":[1,2,3.456]} }Please note DataFrame json does not follow json spec 100%. In json, there is no particular order in dictionary fields. But in DataFrame json:
|
S: Output stream type Ts: The list of types for all columns. A type should be specified only once. o: Reference to an streamable object (e.g. cout) values_only: If true, the name and type of each column is not written iof: Specifies the I/O format. The default is CSV |
template<typename S, typename ... Ts> std::future<bool> write_async(S &o, bool values_only = false, io_format iof = io_format::csv) const; |
Same as write() above, but executed asynchronously |
static void test_write_json() { std::cout << "\nTesting write(json) ..." << std::endl; std::vector<unsigned long> idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460 }; std::vector<double> d1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; std::vector<double> d2 = { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }; std::vector<double> d3 = { 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }; std::vector<double> d4 = { 22, 23, 24, 25, 26, 27 }; std::vector<std::string> s1 = { "11", "22", "33", "aa", "bb", "cc", "dd", "tt", "uu", "ii", "88" }; MyDataFrame df; df.load_data(std::move(idx), std::make_pair("col_1", d1), std::make_pair("col_2", d2), std::make_pair("col_3", d3), std::make_pair("col_str", s1)); df.load_column("col_4", std::move(d4), nan_policy::dont_pad_with_nans); std::cout << "Writing in JSON:" << std::endl; df.write<std::ostream, int, double, std::string>(std::cout, false, io_format::json); }