DatetimeDtype ============= .. cpp:class:: numpy::DatetimeDtype numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use DatetimeDtype DatetimeDtype obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit DatetimeDtype(numpy::DateTimeUnit unit)`` - df_datetime_dtype.h:47 - * - ``DatetimeDtype(numpy::DateTimeUnit unit, const std::string& tz_name)`` - df_datetime_dtype.h:63 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - df_datetime_dtype.h:98 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_tz_aware() const`` - bool - df_datetime_dtype.h:134 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string kind() const override`` - std::string - df_datetime_dtype.h:106 - * - ``bool matches(const std::string& dtype_name) const`` - bool - df_datetime_dtype.h:162 - :ref:`View ` * - ``std::string name() const override`` - std::string - df_datetime_dtype.h:78 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - df_datetime_dtype.h:91 - * - ``std::string repr() const override`` - std::string - df_datetime_dtype.h:173 - * - ``const std::type_info& type() const override`` - const std::type_info& - df_datetime_dtype.h:113 - :ref:`View ` * - ``std::shared_ptr tz() const`` - std::shared_ptr - df_datetime_dtype.h:127 - :ref:`View ` * - ``numpy::DateTimeUnit unit() const`` - numpy::DateTimeUnit - df_datetime_dtype.h:120 - * - ``std::string unit_string() const`` - std::string - df_datetime_dtype.h:141 - * - ``DatetimeDtype with_tz(std::shared_ptr new_tz) const`` - DatetimeDtype - df_datetime_dtype.h:193 - * - ``DatetimeDtype with_tz(const std::string& tz_name) const`` - DatetimeDtype - df_datetime_dtype.h:200 - * - ``DatetimeDtype with_unit(numpy::DateTimeUnit new_unit) const`` - DatetimeDtype - df_datetime_dtype.h:186 - * - ``DatetimeDtype without_tz() const`` - DatetimeDtype - df_datetime_dtype.h:207 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-datetimedtype-itemsize-0: .. dropdown:: itemsize (np_test_2_all.cpp:3540) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3530 :emphasize-lines: 11 } // Test size if (!(size(arr) == 24)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test itemsize if (!(itemsize(arr) == sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(itemsize(arr) == sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test nbytes if (!(nbytes(arr) == 24 * sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(nbytes(arr) == 24 * sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-datetimedtype-matches-1: .. dropdown:: matches (np_test_1_all.cpp:26699) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26689 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << " ✓ load_library() (stub)" << std::endl; } // Test ndpointer { auto ptr_desc = numpy::ctypeslib::ndpointer(2, { 10, 10 }); auto arr = NDArray({ 10, 10 }, 0.0); bool matches = ptr_desc.matches(arr); if (!(matches)) { std::string description = std::string("test_ctypes_interop():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(matches)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << " ✓ ndpointer()" << std::endl; } // std::cout << " [PASS] Sub-phase 6D complete (4 functions tested)" << std::endl; } .. _example-datetimedtype-name-2: .. dropdown:: name (np_test_1_all.cpp:24865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24855 :emphasize-lines: 11 uint64_t before_restore = rng.next_uint64(); rng.set_state(state); uint64_t after_restore = rng.next_uint64(); if (before_restore != after_restore) { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : state restore failed"; throw std::runtime_error("np_test_bitgen_mt19937 failed: state"); } // Test name if (rng.name() != "MT19937") { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : incorrect name"; throw std::runtime_error("np_test_bitgen_mt19937 failed: name"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PCG64 TESTS // ============================================================================ .. _example-datetimedtype-type-3: .. dropdown:: type (np_test_1_all.cpp:2240) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2230 :emphasize-lines: 11 std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 1}).is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(multi_array.getElementAt({0, 2}).is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 2}).is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test 4: Arrays with all same type (homogeneous) NDArray homo_array = createObjectZeros({5}); for (size_t i = 0; i < 5; ++i) { homo_array.setElementAt({i}, object_(static_cast(i * 10))); } // Verify all are same type for (size_t i = 0; i < 5; ++i) { object_ obj = homo_array.getElementAt({i}); if (!(obj.is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; .. _example-datetimedtype-tz-4: .. dropdown:: tz (np_test_5_all.cpp:22214) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22204 :emphasize-lines: 11 } } if (errors == 0) { std::cout << "np_test_timedelta_edge_cases -> tests passed" << std::endl; } return errors; } } // namespace numpy_tests_timedelta // ============================================================================= // Main Test Runner // ============================================================================= int np_test_timedelta_main() { int errors = 0; errors += numpy_tests_timedelta::np_test_timedelta_constructors(); errors += numpy_tests_timedelta::np_test_timedelta_static_factories(); errors += numpy_tests_timedelta::np_test_timedelta_components(); errors += numpy_tests_timedelta::np_test_timedelta_total_conversions();