DatetimeTimedeltaMixin ====================== .. cpp:class:: numpy::DatetimeTimedeltaMixin numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use DatetimeTimedeltaMixin DatetimeTimedeltaMixin obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit DatetimeTimedeltaMixin(const ArrayType& array, const std::optional& name = std::nullopt)`` - df_datetime_timedelta_mixin.h:104 - * - ``explicit DatetimeTimedeltaMixin(ArrayType&& array, const std::optional& name = std::nullopt)`` - df_datetime_timedelta_mixin.h:112 - * - ``DatetimeTimedeltaMixin(const DatetimeTimedeltaMixin& other)`` - df_datetime_timedelta_mixin.h:120 - * - ``DatetimeTimedeltaMixin(DatetimeTimedeltaMixin&& other) noexcept`` - df_datetime_timedelta_mixin.h:127 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static int64_t get_unit_conversion_factor(numpy::DateTimeUnit from, numpy::DateTimeUnit to)`` - static int64_t - df_datetime_timedelta_mixin.h:612 - Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::optional mean(bool skipna = true) const`` - std::optional - df_datetime_timedelta_mixin.h:389 - :ref:`View ` Math Operations --------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DatetimeTimedeltaMixin ceil(const std::string& freq) const`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:319 - :ref:`View ` * - ``DatetimeTimedeltaMixin floor(const std::string& freq) const`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:300 - :ref:`View ` * - ``DatetimeTimedeltaMixin round(const std::string& freq) const`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:281 - :ref:`View ` * - ``static ArrayType round_array(const ArrayType& arr, int64_t freq_ns, RoundMode mode)`` - static ArrayType - df_datetime_timedelta_mixin.h:699 - I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string to_string() const override`` - std::string - df_datetime_timedelta_mixin.h:525 - :ref:`View ` Type Handling ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DatetimeTimedeltaMixin copy(const std::optional& name = std::nullopt) const`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:507 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DatetimeTimedeltaMixin as_unit(const std::string& target_unit) const`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:219 - :ref:`View ` * - ``std::unique_ptr clone() const override`` - std::unique_ptr - df_datetime_timedelta_mixin.h:498 - :ref:`View ` * - ``static ArrayType convert_array_unit(const ArrayType& arr, numpy::DateTimeUnit new_unit, int64_t factor, bool divide)`` - static ArrayType - df_datetime_timedelta_mixin.h:642 - * - ``static int64_t freq_to_nanoseconds(const std::string& freq)`` - static int64_t - df_datetime_timedelta_mixin.h:679 - * - ``\* pandas::DatetimeTDMixin idx(...)`` - \* pandas::DatetimeTDMixin - df_datetime_timedelta_mixin.h:192 - :ref:`View ` * - ``std::string inferred_type() const override`` - std::string - df_datetime_timedelta_mixin.h:570 - * - ``static numpy::DateTimeUnit parse_unit(const std::string& unit_str)`` - static numpy::DateTimeUnit - df_datetime_timedelta_mixin.h:596 - * - ``DatetimeTimedeltaMixin rename(const std::optional& new_name) const`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:517 - * - ``std::string repr() const`` - std::string - df_datetime_timedelta_mixin.h:562 - * - ``DatetimeTimedeltaMixin result(this->array().copy(), name.has_value() ? name : this->name())`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:508 - :ref:`View ` * - ``DatetimeTimedeltaMixin shift(int64_t periods, const std::optional& freq = std::nullopt) const`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:448 - :ref:`View ` * - ``DatetimeTimedeltaMixin snap(const std::string& freq = "S") const`` - DatetimeTimedeltaMixin - df_datetime_timedelta_mixin.h:334 - * - ``std::string unit() const`` - std::string - df_datetime_timedelta_mixin.h:196 - Internal Methods ---------------- *2 internal methods (prefixed with underscore)* Code Examples ------------- The following examples are extracted from the test suite. .. _example-datetimetimedeltamixin-mean-0: .. dropdown:: mean (np_test_1_all.cpp:11714) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11704 :emphasize-lines: 11 // Create test array auto array = createInt32Array({ 2, 3 }, 0); array.setElementAt({ 0, 0 }, 1); array.setElementAt({ 0, 1 }, 2); array.setElementAt({ 0, 2 }, 3); array.setElementAt({ 1, 0 }, 4); array.setElementAt({ 1, 1 }, 5); array.setElementAt({ 1, 2 }, 6); // Test mean without axis auto mean_all = mean(array); if (!(approx_equal(mean_all.getElementAt({ 0 }), 3.5, 1e-10))) { std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(mean_all.getElementAt({ 0 }), 3.5, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] mean (all elements) works correctly\n"; // Test mean along axis 0 auto mean_axis0 = mean(array, 0); if (!(mean_axis0.getShape()[0] == 3)) { .. _example-datetimetimedeltamixin-ceil-1: .. dropdown:: ceil (np_test_2_all.cpp:5735) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5725 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] floor function works correctly\n"; // Test ceil auto ceil_result = ceil(values); if (!(approx_equal(ceil_result.getElementAt({ 0 }), std::ceil(-2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(ceil_result.getElementAt({ 0 }), std::ceil(-2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(approx_equal(ceil_result.getElementAt({ 5 }), std::ceil(2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(ceil_result.getElementAt({ 5 }), std::ceil(2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-datetimetimedeltamixin-floor-2: .. dropdown:: floor (np_test_2_all.cpp:5721) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5711 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(approx_equal(round_result.getElementAt({ 5 }), std::round(2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(round_result.getElementAt({ 5 }), std::round(2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] round function works correctly\n"; // Test floor auto floor_result = floor(values); if (!(approx_equal(floor_result.getElementAt({ 0 }), std::floor(-2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 0 }), std::floor(-2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))) { std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-datetimetimedeltamixin-round-3: .. dropdown:: round (np_test_1_all.cpp:23769) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23759 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(power_scalar_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(power_scalar_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Power functions have correct signatures\n"; // Test rounding functions with new decimals parameter auto round_result = round(array); // Default decimals=0 auto round_decimals_result = round(array, 2); // With decimals if (!(round_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(round_decimals_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_decimals_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-datetimetimedeltamixin-to_string-4: .. dropdown:: to_string (np_test_1_all.cpp:454) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 444 :emphasize-lines: 11 // Modify through different views view1.setElementAt({0, 0}, 100); view2.setElementAt({2, 1}, 200); // This is (1, 2) in original view3.setElementAt({0, 0}, 300); // This is (1, 1) in original // std::cout << "After modifications through multiple views:" << std::endl; //original.printArray(); // Verify all changes are reflected if (!(original.getElementAt({0, 0}) == 100)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 2}) == 200)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 1}) == 300)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)"; .. _example-datetimetimedeltamixin-copy-5: .. dropdown:: copy (np_test_1_all.cpp:9812) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9802 :emphasize-lines: 11 //original.printArray(); // The modification should be at position (1,1) in original if (!(original.getElementAt({1, 1}) == 9999)) { std::string description = std::string("testSliceCopyVsViewMemoryOwnership():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 9999)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Slice view shares memory with original"; // Test slice copy (should be independent) auto slice_copy = original.sliceArray({{2, 4}, {2, 4}}); // std::cout << "Slice copy [2:4, 2:4]:"; //slice_copy.printArray(); slice_copy.setElementAt({0, 0}, 7777); // std::cout << "After modifying slice copy at (0,0):" << std::endl; // std::cout << "Original array:" << std::endl; //original.printArray(); // std::cout << "Slice copy:" << std::endl; .. _example-datetimetimedeltamixin-as_unit-6: .. dropdown:: as_unit (np_test_5_all.cpp:22866) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22856 :emphasize-lines: 11 if (iso_week < 1 || iso_week > 53) { pass = false; fail_msg = "ISO week range"; } if (iso_day < 1 || iso_day > 7) { pass = false; fail_msg = "ISO day range 1-7"; } // isoweekday (1=Monday, 7=Sunday) int isowd = ts.isoweekday(); if (isowd != 6) { pass = false; fail_msg = "2024-06-15 is Saturday = 6"; } // weekday (0=Monday, 6=Sunday) int wd = ts.weekday(); if (wd != 5) { pass = false; fail_msg = "2024-06-15 is Saturday = 5"; } if (!pass) { std::cout << " [FAIL] : in np_test_timestamp_calendar() : " << fail_msg; throw std::runtime_error("np_test_timestamp_calendar failed: " + fail_msg); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // ROUNDING TESTS .. _example-datetimetimedeltamixin-clone-7: .. dropdown:: clone (np_test_1_all.cpp:24942) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24932 :emphasize-lines: 11 } // Test reproducibility numpy::random::PCG64DXSM rng2(11111); if (rng2.next_uint64() != val) { std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : not reproducible"; throw std::runtime_error("np_test_bitgen_pcg64dxsm failed"); } // Test clone auto cloned = rng.clone(); if (cloned->name() != "PCG64DXSM") { std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : clone name incorrect"; throw std::runtime_error("np_test_bitgen_pcg64dxsm failed"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PHILOX TESTS .. _example-datetimetimedeltamixin-idx-8: .. dropdown:: idx (np_test_2_all.cpp:7377) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7367 :emphasize-lines: 11 throw std::runtime_error(description); } auto [row, col] = to_2d_indices(13, 5); if (!(row == 2 && col == 3)) { std::string description = std::string("test_matrix_operations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(row == 2 && col == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Matrix indexing: (2,3) -> " << idx << " -> (" << row << "," << col << ")" << std::endl; // Test vector operations double vec[3] = { 3.0, 4.0, 0.0 }; double norm = vector_norm_2(vec, 3); if (!(std::abs(norm - 5.0) < 1e-10)) { std::string description = std::string("test_matrix_operations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(norm - 5.0) < 1e-10)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Vector norm [3,4,0]: " << norm << std::endl; .. _example-datetimetimedeltamixin-result-9: .. dropdown:: result (np_test_1_all.cpp:6090) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6080 :emphasize-lines: 11 throw std::runtime_error(description); } // std::cout << "Timer restart test passed. Second: " << second_elapsed << " ms" << std::endl; std::cout << " -> tests passed" << std::endl; } void test_benchmark_result_storageBenchmarkSorting() { std::cout << "========= test_benchmark_result_storage ======================="; BenchmarkResult result("TestAlgorithm", 1000, 15.5, true, 4096); if (!(result.algorithm_name == "TestAlgorithm")) { std::string description = std::string("test_benchmark_result_storageBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.algorithm_name == \"TestAlgorithm\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(result.array_size == 1000)) { std::string description = std::string("test_benchmark_result_storageBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.array_size == 1000)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-datetimetimedeltamixin-shift-10: .. dropdown:: shift (np_test_1_all.cpp:2761) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2751 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(left_array.getElementAt({3}) == 16384)) { std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(left_array.getElementAt({3}) == 16384)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Left shift by array\n"; // Test excessive shift (should result in 0) auto excessive_left = left_shift(arr, 20); // More than 16 bits if (!(excessive_left.getElementAt({0}) == 0)) { std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(excessive_left.getElementAt({0}) == 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(excessive_left.getElementAt({1}) == 0)) { std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(excessive_left.getElementAt({1}) == 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description);