CustomBusinessHour ================== .. cpp:class:: pandas::CustomBusinessHour pandas C++ class. Example ------- .. code-block:: cpp #include using namespace pandas; // Use CustomBusinessHour CustomBusinessHour obj; // ... operations ... Aggregation ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::datetime64 apply(const numpy::datetime64& dt) const override`` - numpy::datetime64 - pd_dateoffset.h:1886 - :ref:`View ` Comparison ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool equals(const DateOffset& other) const override`` - bool - pd_dateoffset.h:1863 - :ref:`View ` Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``int end_hour() const`` - int - pd_dateoffset.h:1871 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_business_day(int64_t epoch_seconds) const`` - bool - pd_dateoffset.h:1805 - * - ``bool is_calendar_offset() const override`` - bool - pd_dateoffset.h:1861 - * - ``bool is_on_offset(const numpy::datetime64& dt) const override`` - bool - pd_dateoffset.h:1926 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static int64_t epoch_day(int64_t epoch_seconds)`` - static int64_t - pd_dateoffset.h:1799 - * - ``std::string freqstr() const override`` - std::string - pd_dateoffset.h:1859 - :ref:`View ` * - ``const std::set& holidays() const`` - const std::set& - pd_dateoffset.h:1873 - * - ``std::string name() const override`` - std::string - pd_dateoffset.h:1860 - :ref:`View ` * - ``std::string repr() const override`` - std::string - pd_dateoffset.h:1875 - :ref:`View ` * - ``numpy::datetime64 rollback(const numpy::datetime64& dt) const override`` - numpy::datetime64 - pd_dateoffset.h:1941 - * - ``numpy::datetime64 rollforward(const numpy::datetime64& dt) const override`` - numpy::datetime64 - pd_dateoffset.h:1935 - * - ``int64_t snap_to_open(int64_t epoch, int direction) const`` - int64_t - pd_dateoffset.h:1811 - * - ``int start_hour() const`` - int - pd_dateoffset.h:1870 - * - ``uint8_t weekmask() const`` - uint8_t - pd_dateoffset.h:1872 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-custombusinesshour-apply-0: .. dropdown:: apply (pd_test_1_all.cpp:11244) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11234 :emphasize-lines: 11 void pd_test_func_apply_dataframe_apply_axis0() { std::cout << "========= DataFrame apply axis=0 ======================"; std::map> data = { {"A", {1.0, 2.0, 3.0}}, {"B", {4.0, 5.0, 6.0}} }; pandas::DataFrame df(data); // apply axis=0 applies function to each column auto result = df.apply([](const std::vector& col) { return std::accumulate(col.begin(), col.end(), 0.0); }, 0); bool passed = true; // Plan F·dtype: axis=0 reduce now returns a single "result" column // with the original column names ("A", "B") as the row index. // Sum of A: 1+2+3=6, Sum of B: 4+5+6=15 const auto& result_col = result["result"]; double sum_a = std::stod(result_col.get_value_str(0)); .. _example-custombusinesshour-equals-1: .. dropdown:: equals (pd_test_1_all.cpp:5866) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5856 :emphasize-lines: 11 std::cout << "========= equals ======================================"; pandas::CategoricalArray arr1({"a", "b", "a"}); pandas::CategoricalArray arr2({"a", "b", "a"}); pandas::CategoricalArray arr3({"a", "b", "c"}); pandas::CategoricalIndex idx1(arr1); pandas::CategoricalIndex idx2(arr2); pandas::CategoricalIndex idx3(arr3); bool passed = (idx1.equals(idx2) && !idx1.equals(idx3)); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_equals()" << std::endl; throw std::runtime_error("pd_test_categorical_index_equals failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_identical() { std::cout << "========= identical ==================================="; .. _example-custombusinesshour-is_on_offset-2: .. dropdown:: is_on_offset (pd_test_3_all.cpp:18263) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18253 :emphasize-lines: 11 void pd_test_business_day_offset() { std::cout << "========= BusinessDay offset ==========================="; pandas::BusinessDay offset(1); if (offset.freqstr() != "B") { std::cout << " [FAIL] : BusinessDay freqstr() failed" << std::endl; throw std::runtime_error("pd_test_business_day_offset: freqstr() failed"); } // Test is_on_offset (Friday = weekday) numpy::datetime64 friday("2020-01-17"); // Friday if (!offset.is_on_offset(friday)) { std::cout << " [FAIL] : BusinessDay is_on_offset(Friday) failed" << std::endl; throw std::runtime_error("pd_test_business_day_offset: is_on_offset(Friday) failed"); } // Test is_on_offset (Saturday = weekend) numpy::datetime64 saturday("2020-01-18"); // Saturday if (offset.is_on_offset(saturday)) { std::cout << " [FAIL] : BusinessDay is_on_offset(Saturday) should be false" << std::endl; .. _example-custombusinesshour-freqstr-3: .. dropdown:: freqstr (pd_test_1_all.cpp:2671) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2661 :emphasize-lines: 11 } pandas::PeriodDtype dtype_y("Y"); if (dtype_y.name() != "period[Y]") { std::cout << " [FAIL] : dtype_y.name() should be 'period[Y]'" << std::endl; throw std::runtime_error("pd_test_period_array_freq_validation failed: dtype name Y"); } // Test frequency string pandas::PeriodArray arr(std::vector{"2024-01-15"}, "D"); if (arr.freqstr() != "D") { std::cout << " [FAIL] : arr.freqstr() should be 'D'" << std::endl; throw std::runtime_error("pd_test_period_array_freq_validation failed: freqstr"); } std::cout << " -> tests passed" << std::endl; } void pd_test_period_array_year_month_quarter() { std::cout << "========= PeriodArray: year/month/quarter components ======================= "; .. _example-custombusinesshour-name-4: .. dropdown:: name (pd_test_1_all.cpp:295) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 285 :emphasize-lines: 11 throw std::runtime_error("pd_test_boolean_array_reductions failed: mean"); } std::cout << " -> tests passed" << std::endl; } void pd_test_boolean_array_dtype() { std::cout << "========= BooleanArray: dtype ======================= "; pandas::BooleanArray arr; if (arr.dtype().name() != "boolean") { std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl; throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name"); } if (arr.dtype().kind() != "b") { std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl; throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind"); } std::cout << " -> tests passed" << std::endl; .. _example-custombusinesshour-repr-5: .. dropdown:: repr (pd_test_1_all.cpp:10906) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10896 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_extension_index_repr() { std::cout << "========= repr ========================="; pandas::CategoricalArray arr({"a", "b", "c"}); // Use ExtensionIndex directly to test base class repr pandas::ExtensionIndex idx(arr, "test"); std::string repr_str = idx.repr(); bool passed = (!repr_str.empty() && repr_str.find("ExtensionIndex") != std::string::npos); if (!passed) { std::cout << " [FAIL] : in pd_test_extension_index_repr() : repr check failed" << std::endl; throw std::runtime_error("pd_test_extension_index_repr failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-custombusinesshour-weekmask-6: .. dropdown:: weekmask (pd_test_3_all.cpp:1377) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1367 :emphasize-lines: 11 true, "business_dates" ); // Should have business days (Mon-Fri = 5 days in this range) if (result.size() < 1) { std::cout << " [FAIL] : in pd_test_3_all_bdate_range() : expected at least 1 business day" << std::endl; throw std::runtime_error("pd_test_3_all_bdate_range failed: no days returned"); } // Test with custom weekmask (exclude Fridays) pandas::DatetimeIndex result2 = pandas::bdate_range( "2024-01-01", "2024-01-07", // Full week std::nullopt, "B", std::nullopt, true, "custom_days", "Mon Tue Wed Thu" // No Fri, Sat, Sun );