Parser ====== .. cpp:class:: pandas::Parser Query engine class for expression evaluation. Example ------- .. code-block:: cpp #include using namespace pandas; // Use Parser Parser obj; // ... operations ... Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static bool is_comparison_op(TokenType tok_type)`` - static bool - pd_query.h:422 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void advance()`` - void - pd_query.h:407 - * - ``Token& current()`` - Token& - pd_query.h:398 - :ref:`View ` * - ``void expect(TokenType expected_type, const std::string& error_msg)`` - void - pd_query.h:415 - * - ``expect(TokenType::RPAREN, "Expected ')' after expression")`` - - pd_query.h:477 - * - ``expect(TokenType::RBRACKET, "Expected ']' to close list literal")`` - - pd_query.h:509 - * - ``bool match(TokenType expected_type)`` - bool - pd_query.h:411 - :ref:`View ` * - ``ListLiteral parse_list_literal()`` - ListLiteral - pd_query.h:490 - * - ``Token& peek(size_t ahead = 1)`` - Token& - pd_query.h:402 - :ref:`View ` * - ``explicit Parser(std::vector tokens) : tokens_(std::move(tokens))`` - explicit Parser(std::vector tokens) : - pd_query.h:705 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-parser-current-0: .. dropdown:: current (pd_test_4_all.cpp:1140) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1130 :emphasize-lines: 11 const std::string& actual) { int _f = 0; pandas_tests::check_str_ws(label, expected, actual, _f); if (_f > 0) throw std::runtime_error(label + ": str mismatch"); } // ---------------------------------------------------------------------------- // Case 1 — dtype.int32_df_nsmallest // ---------------------------------------------------------------------------- void dtype_int32_df_nsmallest() { // Strategy B: synthesize the current (buggy) post-nsmallest state. // Column A is double because int32 is silently promoted inside // pandas::DataFrame::nsmallest today. Column B (from range(10)) stays // int64. Row index labels "2","6","4" are the original positions of the // 3 smallest A values, ties broken by first-occurrence. pandas::DataFrame df; df.add_column("A", std::vector{1.0, 2.0, 3.0}); df.add_column("B", std::vector{2, 6, 4}); df.set_index(std::make_unique>( std::vector{"2", "6", "4"})); apply_default_display(df); .. _example-parser-match-1: .. dropdown:: match (pd_test_2_all.cpp:1467) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1457 :emphasize-lines: 11 void pd_test_between_time_overnight() { std::cout << "========= DataFrame between_time: overnight range ======"; // Test overnight range (e.g., 23:00 to 01:00) std::map> data = { {"A", {1.0, 2.0, 3.0, 4.0, 5.0}} }; pandas::DataFrame df(data); std::vector datetime_index = { "2018-04-09 00:30:00", // Should match (before 01:00) "2018-04-09 12:00:00", // Should NOT match "2018-04-09 22:00:00", // Should NOT match "2018-04-09 23:30:00", // Should match (after 23:00) "2018-04-10 00:00:00" // Should match (at midnight, before 01:00) }; df.set_index(std::make_unique>(datetime_index)); // Overnight range: 23:00 to 01:00 auto result = df.between_time("23:00:00", "01:00:00"); .. _example-parser-peek-2: .. dropdown:: peek (pd_test_5_all.cpp:123633) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 123623 :emphasize-lines: 11 if (!in.good()) return false; std::string field; bool in_quotes = false; char ch; bool any = false; while (in.get(ch)) { any = true; if (in_quotes) { if (ch == '"') { // Lookahead: doubled quote = literal quote. if (in.peek() == '"') { in.get(ch); field.push_back('"'); } else { in_quotes = false; } } else if (ch == '\r') { // Strip CR even inside quotes: the oracle CSV uses \r\n for // newlines inside quoted multiline cells, but `format_*` only // emits \n. Normalise here so byte-equality holds. } else { field.push_back(ch); } } else { if (ch == '"') {