DateOffset ========== .. cpp:class:: pandas::DateOffset pandas C++ class. Example ------- .. code-block:: cpp #include using namespace pandas; // Use DateOffset DateOffset obj; // ... operations ... Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static std::unique_ptr from_freq_string(const std::string& freq)`` - static std::unique_ptr - pd_dateoffset.h:426 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``int minutes() const`` - int - pd_dateoffset.h:210 - :ref:`View ` Aggregation ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual numpy::datetime64 apply(const numpy::datetime64& dt) const`` - virtual numpy::datetime64 - pd_dateoffset.h:254 - :ref:`View ` Arithmetic ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual int64_t add_nanos(int64_t ns) const`` - virtual int64_t - pd_dateoffset.h:392 - :ref:`View ` Comparison ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual bool equals(const DateOffset& other) const`` - virtual bool - pd_dateoffset.h:237 - :ref:`View ` * - ``virtual std::unique_ptr negate() const`` - virtual std::unique_ptr - pd_dateoffset.h:247 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual bool is_calendar_offset() const`` - virtual bool - pd_dateoffset.h:351 - * - ``virtual bool is_on_offset(const numpy::datetime64& dt) const`` - virtual bool - pd_dateoffset.h:310 - :ref:`View ` * - ``bool is_zero() const`` - bool - pd_dateoffset.h:404 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``int days() const`` - int - pd_dateoffset.h:208 - :ref:`View ` * - ``numpy::datetime64 dt(ns, numpy::DateTimeUnit::Nanosecond)`` - numpy::datetime64 - pd_dateoffset.h:380 - :ref:`View ` * - ``numpy::datetime64 dt(ns, numpy::DateTimeUnit::Nanosecond)`` - numpy::datetime64 - pd_dateoffset.h:393 - :ref:`View ` * - ``virtual std::string freqstr() const`` - virtual std::string - pd_dateoffset.h:220 - :ref:`View ` * - ``int hours() const`` - int - pd_dateoffset.h:209 - :ref:`View ` * - ``int microseconds() const`` - int - pd_dateoffset.h:212 - :ref:`View ` * - ``int months() const`` - int - pd_dateoffset.h:206 - :ref:`View ` * - ``int n() const`` - int - pd_dateoffset.h:200 - :ref:`View ` * - ``virtual std::string name() const`` - virtual std::string - pd_dateoffset.h:221 - :ref:`View ` * - ``int nanoseconds() const`` - int - pd_dateoffset.h:213 - :ref:`View ` * - ``bool normalize() const`` - bool - pd_dateoffset.h:202 - :ref:`View ` * - ``static ParsedFreq parse_freq_string(const std::string& freq)`` - static ParsedFreq - pd_dateoffset.h:427 - :ref:`View ` * - ``virtual std::string repr() const`` - virtual std::string - pd_dateoffset.h:233 - :ref:`View ` * - ``virtual numpy::datetime64 rollback(const numpy::datetime64& dt) const`` - virtual numpy::datetime64 - pd_dateoffset.h:321 - * - ``virtual numpy::datetime64 rollforward(const numpy::datetime64& dt) const`` - virtual numpy::datetime64 - pd_dateoffset.h:315 - * - ``int seconds() const`` - int - pd_dateoffset.h:211 - :ref:`View ` * - ``void set_n(int n)`` - void - pd_dateoffset.h:201 - * - ``void set_weekday(int wd, int n)`` - void - pd_dateoffset.h:218 - * - ``virtual int64_t snap_to_grid(int64_t ns) const`` - virtual int64_t - pd_dateoffset.h:372 - :ref:`View ` * - ``virtual int64_t total_nanoseconds() const`` - virtual int64_t - pd_dateoffset.h:334 - * - ``int weekday() const`` - int - pd_dateoffset.h:216 - :ref:`View ` * - ``int weekday_n() const`` - int - pd_dateoffset.h:217 - * - ``int weeks() const`` - int - pd_dateoffset.h:207 - * - ``int years() const`` - int - pd_dateoffset.h:205 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-dateoffset-from_freq_string-0: .. dropdown:: from_freq_string (pd_test_5_all.cpp:87341) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 87331 :emphasize-lines: 11 auto idx = mk_idx({"2020-01-01", "2020-01-03"}); auto up = idx.upsample(pandas::Day(1)); pandas_tests::check(up.tz().empty(), "case_31.tz_naive.tz_empty", local_fail); } void f_core_05_upsample_05f4ab_case_32_overload_hourly(int& local_fail) { std::cout << "-- case_32_overload_hourly\n"; auto idx = mk_idx({"2020-01-01", "2020-01-02"}); auto via_string = idx.upsample("H"); auto via_factory = idx.upsample(*pandas::DateOffset::from_freq_string("H")); auto a = to_ns_vec(via_string); auto b = to_ns_vec(via_factory); pandas_tests::check(a == b, "case_32.overload_hourly.ns_vec_equal", local_fail); } void f_core_05_upsample_05f4ab_case_33_overload_daily(int& local_fail) { std::cout << "-- case_33_overload_daily\n"; auto idx = mk_idx({"2020-01-01", "2020-01-31"}); auto via_string = idx.upsample("D"); .. _example-dateoffset-minutes-1: .. dropdown:: minutes (pd_test_3_all.cpp:21614) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21604 :emphasize-lines: 11 } if (d.months() != 2) { throw std::runtime_error("DateOffset: months mismatch"); } if (d.days() != 5) { throw std::runtime_error("DateOffset: days mismatch"); } if (d.hours() != 3) { throw std::runtime_error("DateOffset: hours mismatch"); } if (d.minutes() != 30) { throw std::runtime_error("DateOffset: minutes mismatch"); } if (d.seconds() != 10) { throw std::runtime_error("DateOffset: seconds mismatch"); } std::cout << " -> tests passed" << std::endl; } void test_yearend_freqstr() { .. _example-dateoffset-apply-2: .. 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-dateoffset-add_nanos-3: .. dropdown:: add_nanos (pd_test_5_all.cpp:87543) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 87533 :emphasize-lines: 11 int64_t snapped = pandas::Day(1).snap_to_grid(mid.toNanoseconds()); numpy::datetime64 expected("2020-01-01T00:00:00"); pandas_tests::check(snapped == expected.toNanoseconds(), "case_45.day_snap_to_grid.floors_to_midnight", local_fail); } void f_core_05_upsample_05f4ab_case_46_day_add_nanos(int& local_fail) { std::cout << "-- case_46_day_add_nanos\n"; numpy::datetime64 start("2020-01-01T00:00:00"); int64_t next_ns = pandas::Day(1).add_nanos(start.toNanoseconds()); numpy::datetime64 expected("2020-01-02T00:00:00"); pandas_tests::check(next_ns == expected.toNanoseconds(), "case_46.day_add_nanos.advances_one_day", local_fail); } } // namespace f_test_05_datetimeindex_upsample_9_ns void f_test_05_datetimeindex_upsample_9() { std::cout << "========= f_test_05_datetimeindex_upsample_9 ======="; .. _example-dateoffset-equals-4: .. 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-dateoffset-negate-5: .. 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-dateoffset-is_on_offset-6: .. 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-dateoffset-is_zero-7: .. dropdown:: is_zero (pd_test_1_all.cpp:4280) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4270 :emphasize-lines: 11 if (!neg[1].has_value() || !neg[1].value()) { std::cout << " [FAIL] : -5 hours should be negative" << std::endl; throw std::runtime_error("pd_test_timedelta_array_boolean_props failed: is_negative"); } if (!neg[0].has_value() || neg[0].value()) { std::cout << " [FAIL] : 1 day should not be negative" << std::endl; throw std::runtime_error("pd_test_timedelta_array_boolean_props failed: not negative"); } // is_zero auto zero = arr.is_zero(); if (!zero[2].has_value() || !zero[2].value()) { std::cout << " [FAIL] : 0 seconds should be zero" << std::endl; throw std::runtime_error("pd_test_timedelta_array_boolean_props failed: is_zero"); } // NA should propagate if (pos[3].has_value() || neg[3].has_value() || zero[3].has_value()) { std::cout << " [FAIL] : NaT should propagate to boolean props" << std::endl; throw std::runtime_error("pd_test_timedelta_array_boolean_props failed: NaT propagation"); } .. _example-dateoffset-days-8: .. dropdown:: days (pd_test_1_all.cpp:4160) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4150 :emphasize-lines: 11 void pd_test_timedelta_array_component_days() { std::cout << "========= TimedeltaArray: days component ======================= "; pandas::TimedeltaArray arr({ std::optional(numpy::timedelta64(3, numpy::DateTimeUnit::Day)), std::nullopt, std::optional(numpy::timedelta64(36, numpy::DateTimeUnit::Hour)) // 1.5 days }); auto days_arr = arr.days(); auto d0 = days_arr[0]; if (!d0.has_value() || d0.value() != 3) { std::cout << " [FAIL] : days[0] should be 3" << std::endl; throw std::runtime_error("pd_test_timedelta_array_component_days failed: days[0]"); } auto d1 = days_arr[1]; if (d1.has_value()) { std::cout << " [FAIL] : days[1] should be NA (NaT)" << std::endl; .. _example-dateoffset-dt-9: .. dropdown:: dt (pd_test_3_all.cpp:18239) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18229 :emphasize-lines: 11 if (offset.freqstr() != "D") { std::cout << " [FAIL] : Day freqstr() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: freqstr() failed"); } if (offset.name() != "Day") { std::cout << " [FAIL] : Day name() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: name() failed"); } // Test apply numpy::datetime64 dt("2020-01-15"); auto result = offset.apply(dt); std::tm tm = result.toTm(); if (tm.tm_mday != 20) { std::cout << " [FAIL] : Day apply() failed, got day " << tm.tm_mday << std::endl; throw std::runtime_error("pd_test_day_offset: apply() failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-dateoffset-dt-10: .. dropdown:: dt (pd_test_3_all.cpp:18239) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18229 :emphasize-lines: 11 if (offset.freqstr() != "D") { std::cout << " [FAIL] : Day freqstr() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: freqstr() failed"); } if (offset.name() != "Day") { std::cout << " [FAIL] : Day name() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: name() failed"); } // Test apply numpy::datetime64 dt("2020-01-15"); auto result = offset.apply(dt); std::tm tm = result.toTm(); if (tm.tm_mday != 20) { std::cout << " [FAIL] : Day apply() failed, got day " << tm.tm_mday << std::endl; throw std::runtime_error("pd_test_day_offset: apply() failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-dateoffset-freqstr-11: .. 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-dateoffset-hours-12: .. dropdown:: hours (pd_test_1_all.cpp:9567) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9557 :emphasize-lines: 11 std::cout << std::endl << " [FAIL] : floor result incorrect"; passed = false; } // ceil should give 2 hours if (ceiled.total_seconds() != 7200.0) { std::cout << std::endl << " [FAIL] : ceil result incorrect"; passed = false; } // round should give 2 hours (30m45s > 30m) if (rounded.total_seconds() != 7200.0) { std::cout << std::endl << " [FAIL] : round result incorrect"; passed = false; } if (!passed) { throw std::runtime_error("pd_test_timedelta_rounding_params failed"); } std::cout << " -> tests passed" << std::endl; .. _example-dateoffset-microseconds-13: .. dropdown:: microseconds (pd_test_1_all.cpp:19701) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19691 :emphasize-lines: 11 constexpr int64_t NS_PER_US = 1000LL; std::vector> values = { make_td(0), // 0 us make_td(500 * NS_PER_US), // 500 us make_td(NS_PER_SEC + 100 * NS_PER_US) // 1 sec + 100 us }; pandas::TimedeltaArray arr(values); pandas::TimedeltaIndex idx(arr); auto microseconds = idx.microseconds(); bool passed = (microseconds.size() == 3); if (!passed) { std::cout << " [FAIL] : in pd_test_timedelta_index_microseconds()" << std::endl; throw std::runtime_error("pd_test_timedelta_index_microseconds failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-dateoffset-months-14: .. dropdown:: months (pd_test_3_all.cpp:21605) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21595 :emphasize-lines: 11 namespace dataframe_tests { namespace dataframe_tests_dateoffset_sig { void test_dateoffset_extra_params() { std::cout << "========= DateOffset extra parameters ============="; pandas::DateOffset d(1, false, 1, 2, 0, 5, 3, 30, 10, 0, 0); if (d.years() != 1) { throw std::runtime_error("DateOffset: years mismatch"); } if (d.months() != 2) { throw std::runtime_error("DateOffset: months mismatch"); } if (d.days() != 5) { throw std::runtime_error("DateOffset: days mismatch"); } if (d.hours() != 3) { throw std::runtime_error("DateOffset: hours mismatch"); } if (d.minutes() != 30) { throw std::runtime_error("DateOffset: minutes mismatch"); .. _example-dateoffset-n-15: .. dropdown:: n (pd_test_3_all.cpp:18225) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18215 :emphasize-lines: 11 namespace dataframe_tests_dateoffset { // ============================================================================ // Test Day offset // ============================================================================ void pd_test_day_offset() { std::cout << "========= Day offset ==================================="; pandas::Day offset(5); if (offset.n() != 5) { std::cout << " [FAIL] : Day n() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: n() failed"); } if (offset.freqstr() != "D") { std::cout << " [FAIL] : Day freqstr() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: freqstr() failed"); } if (offset.name() != "Day") { std::cout << " [FAIL] : Day name() failed" << std::endl; throw std::runtime_error("pd_test_day_offset: name() failed"); .. _example-dateoffset-name-16: .. 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-dateoffset-nanoseconds-17: .. dropdown:: nanoseconds (pd_test_1_all.cpp:9379) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9369 :emphasize-lines: 11 std::cout << " [FAIL] : in pd_test_datetime_as_unit() : as_unit check failed" << std::endl; throw std::runtime_error("pd_test_datetime_as_unit failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_timedelta_as_unit() { std::cout << "========= TimedeltaTDMixin as_unit ========================="; // Create index in nanoseconds (1 hour, 2 hours) numpy::NDArray data(std::vector{2}); data.setElementAt({0}, numpy::timedelta64(3600000000000LL, numpy::DateTimeUnit::Nanosecond)); // 1 hour data.setElementAt({1}, numpy::timedelta64(7200000000000LL, numpy::DateTimeUnit::Nanosecond)); // 2 hours numpy::NDArray mask(std::vector{2}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); pandas::TimedeltaArray arr(data, mask); pandas::TimedeltaTDMixin idx(arr, "durations"); .. _example-dateoffset-normalize-18: .. dropdown:: normalize (pd_test_1_all.cpp:8723) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8713 :emphasize-lines: 11 void pd_test_datetime_mixin_normalize() { std::cout << "========= normalize ==================================="; // Create datetime with time component std::vector> values = { numpy::datetime64(86400000000000LL + 3600000000000LL, numpy::DateTimeUnit::Nanosecond) // 1 day + 1 hour }; pandas::DatetimeArray arr(values); pandas::DatetimeMixinIndex idx(arr); pandas::DatetimeMixinIndex normalized = idx.normalize(); bool passed = (normalized.size() == 1); if (!passed) { std::cout << " [FAIL] : in pd_test_datetime_mixin_normalize()" << std::endl; throw std::runtime_error("pd_test_datetime_mixin_normalize failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-dateoffset-parse_freq_string-19: .. dropdown:: parse_freq_string (pd_test_5_all.cpp:101264) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 101254 :emphasize-lines: 11 void f_core_02_freq_factory_c02f7a_case_54_yearbegin_YS_MAR(int& local_fail) { std::cout << "-- case_54_yearbegin_YS_MAR\n"; check_factory_year_anchored( "f_core_02_freq_factory_c02f7a_case_54.year_YS_MAR", "YS-MAR", 1, 3, local_fail); } void f_core_02_freq_factory_c02f7a_case_55_parse_D(int& local_fail) { std::cout << "-- case_55_parse_D\n"; pandas::DateOffset::ParsedFreq p = pandas::DateOffset::parse_freq_string("D"); pandas_tests::check(p.n == 1, "f_core_02_freq_factory_c02f7a_case_55.parse_D.n", local_fail); pandas_tests::check(p.base == "D", "f_core_02_freq_factory_c02f7a_case_55.parse_D.base", local_fail); pandas_tests::check(p.anchor.empty(), "f_core_02_freq_factory_c02f7a_case_55.parse_D.anchor_empty", local_fail); } void f_core_02_freq_factory_c02f7a_case_56_parse_5H(int& local_fail) { std::cout << "-- case_56_parse_5H\n"; .. _example-dateoffset-repr-20: .. 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-dateoffset-seconds-21: .. dropdown:: seconds (pd_test_1_all.cpp:4192) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4182 :emphasize-lines: 11 void pd_test_timedelta_array_component_seconds() { std::cout << "========= TimedeltaArray: seconds component ======================= "; pandas::TimedeltaArray arr({ std::optional(numpy::timedelta64(90, numpy::DateTimeUnit::Second)), // 90 secs std::optional(numpy::timedelta64(3700, numpy::DateTimeUnit::Second)), // 1h + 100s std::nullopt }); auto secs = arr.seconds(); auto s0 = secs[0]; if (!s0.has_value() || s0.value() != 90) { std::cout << " [FAIL] : seconds[0] should be 90" << std::endl; throw std::runtime_error("pd_test_timedelta_array_component_seconds failed: seconds[0]"); } auto s1 = secs[1]; if (!s1.has_value() || s1.value() != 3700) { std::cout << " [FAIL] : seconds[1] should be 3700" << std::endl; .. _example-dateoffset-snap_to_grid-22: .. dropdown:: snap_to_grid (pd_test_5_all.cpp:87533) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 87523 :emphasize-lines: 11 "case_44.timedeltaindex_zero_freq.value_error", local_fail); pandas_tests::check(got_msg == pandas::error_messages::kFreqZero, "case_44.timedeltaindex_zero_freq.message", local_fail); } void f_core_05_upsample_05f4ab_case_45_day_snap_to_grid(int& local_fail) { std::cout << "-- case_45_day_snap_to_grid\n"; numpy::datetime64 mid("2020-01-01T05:00:00"); int64_t snapped = pandas::Day(1).snap_to_grid(mid.toNanoseconds()); numpy::datetime64 expected("2020-01-01T00:00:00"); pandas_tests::check(snapped == expected.toNanoseconds(), "case_45.day_snap_to_grid.floors_to_midnight", local_fail); } void f_core_05_upsample_05f4ab_case_46_day_add_nanos(int& local_fail) { std::cout << "-- case_46_day_add_nanos\n"; numpy::datetime64 start("2020-01-01T00:00:00"); int64_t next_ns = pandas::Day(1).add_nanos(start.toNanoseconds()); .. _example-dateoffset-weekday-23: .. dropdown:: weekday (pd_test_3_all.cpp:1471) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1461 :emphasize-lines: 11 std::cout << " [FAIL] date_range 10-arg form: expected size 10, got " << idx.size() << std::endl; throw std::runtime_error("date_range 10-arg form regressed"); } } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_period_weekday() { std::cout << "========= PeriodArray.weekday() ======================"; // Create a PeriodArray with some dates std::vector> ordinals = {0, 1, 2, 3, 4}; // Days from epoch pandas::PeriodArray arr(ordinals, "D"); pandas::IntegerArray weekdays = arr.weekday(); if (weekdays.size() != 5) { std::cout << " [FAIL] : in pd_test_3_all_period_weekday() : size should be 5" << std::endl; throw std::runtime_error("pd_test_3_all_period_weekday failed: size"); .. _example-dateoffset-years-24: .. dropdown:: years (pd_test_3_all.cpp:21602) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21592 :emphasize-lines: 11 #include #include namespace dataframe_tests { namespace dataframe_tests_dateoffset_sig { void test_dateoffset_extra_params() { std::cout << "========= DateOffset extra parameters ============="; pandas::DateOffset d(1, false, 1, 2, 0, 5, 3, 30, 10, 0, 0); if (d.years() != 1) { throw std::runtime_error("DateOffset: years mismatch"); } if (d.months() != 2) { throw std::runtime_error("DateOffset: months mismatch"); } if (d.days() != 5) { throw std::runtime_error("DateOffset: days mismatch"); } if (d.hours() != 3) { throw std::runtime_error("DateOffset: hours mismatch");