MonthEnd ======== .. cpp:class:: pandas::MonthEnd pandas C++ class. Example ------- .. code-block:: cpp #include using namespace pandas; // Use MonthEnd MonthEnd obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``MonthEnd(int n = 1, bool normalize = false) : DateOffset(n, normalize)`` - pd_dateoffset.h:633 - :ref:`View ` 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:644 - :ref:`View ` Comparison ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::unique_ptr negate() const override`` - std::unique_ptr - pd_dateoffset.h:640 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_calendar_offset() const override`` - bool - pd_dateoffset.h:637 - * - ``bool is_on_offset(const numpy::datetime64& dt) const override`` - bool - pd_dateoffset.h:671 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string freqstr() const override`` - std::string - pd_dateoffset.h:635 - :ref:`View ` * - ``std::string name() const override`` - std::string - pd_dateoffset.h:636 - :ref:`View ` * - ``int64_t total_nanoseconds() const override`` - int64_t - pd_dateoffset.h:638 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-monthend-monthend-0: .. dropdown:: MonthEnd (pd_test_5_all.cpp:87093) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 87083 :emphasize-lines: 11 std::cout << "-- case_4_halfhour_of_hourly\n"; auto idx = mk_idx({"2020-01-01T00:00:00", "2020-01-01T02:00:00"}); auto up = idx.upsample(pandas::Minute(30)); pandas_tests::check(up.size() == 5, "case_4.halfhour_of_hourly.size==5", local_fail); } void f_core_05_upsample_05f4ab_case_5_monthend_of_yearly(int& local_fail) { std::cout << "-- case_5_monthend_of_yearly\n"; auto idx = mk_idx({"2020-12-31", "2021-12-31", "2022-12-31"}); auto up = idx.upsample(pandas::MonthEnd(1)); pandas_tests::check(up.size() == 25, "case_5.monthend_of_yearly.size==25", local_fail); } void f_core_05_upsample_05f4ab_case_6_monthbegin_of_yearly(int& local_fail) { std::cout << "-- case_6_monthbegin_of_yearly\n"; auto idx = mk_idx({"2020-01-01", "2021-01-01", "2022-01-01"}); auto up = idx.upsample(pandas::MonthBegin(1)); pandas_tests::check(up.size() == 25, "case_6.monthbegin_of_yearly.size==25", local_fail); .. _example-monthend-apply-1: .. 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-monthend-negate-2: .. dropdown:: negate (pd_test_4_all.cpp:6343) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6333 :emphasize-lines: 11 EXPECT(static_cast(out[1]) == 6LL * 86400000000000LL); EXPECT(out.dtype_name() == "datetime64[ns]"); } void test_sub_dateoffset_calendar_monthend() { // 2024-01-31 in ns int64_t jan31 = 1706659200LL * 1000000000LL; auto s = make_dt_series({jan31}); pandas::MonthEnd me(1); auto out = s.sub_dateoffset(me); auto neg = me.negate(); auto ref = s.add_dateoffset(*neg); EXPECT(out.size() == 1); EXPECT(static_cast(out[0]) == static_cast(ref[0])); EXPECT(out.dtype_name() == "datetime64[ns]"); } void test_sub_dateoffset_equals_add_negated() { int64_t jan31 = 1706659200LL * 1000000000LL; auto s = make_dt_series({jan31, jan31 + 86400000000000LL}); pandas::MonthEnd me(2); .. _example-monthend-is_on_offset-3: .. 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-monthend-freqstr-4: .. 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-monthend-name-5: .. 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;