DatetimeTimedeltaMixin ====================== .. cpp:class:: pandas::DatetimeTimedeltaMixin Mixin class providing shared functionality. Example ------- .. code-block:: cpp #include using namespace pandas; // 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)`` - pd_datetime_timedelta_mixin.h:105 - * - ``explicit DatetimeTimedeltaMixin(ArrayType&& array, const std::optional& name = std::nullopt)`` - pd_datetime_timedelta_mixin.h:113 - * - ``DatetimeTimedeltaMixin(const DatetimeTimedeltaMixin& other)`` - pd_datetime_timedelta_mixin.h:121 - * - ``DatetimeTimedeltaMixin(DatetimeTimedeltaMixin&& other) noexcept`` - pd_datetime_timedelta_mixin.h:128 - 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 - pd_datetime_timedelta_mixin.h:676 - Data Manipulation ----------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DatetimeTimedeltaMixin rename(const std::optional& new_name) const`` - DatetimeTimedeltaMixin - pd_datetime_timedelta_mixin.h:581 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::optional mean(bool skipna = true, std::optional axis = std::nullopt) const`` - std::optional - pd_datetime_timedelta_mixin.h:447 - :ref:`View ` Time Series ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DatetimeTimedeltaMixin shift(int64_t periods, const std::optional& freq = std::nullopt) const`` - DatetimeTimedeltaMixin - pd_datetime_timedelta_mixin.h:508 - :ref:`View ` 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 - pd_datetime_timedelta_mixin.h:589 - :ref:`View ` Conversion ---------- .. 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 - pd_datetime_timedelta_mixin.h:571 - :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, bool round_ok = true) const`` - DatetimeTimedeltaMixin - pd_datetime_timedelta_mixin.h:224 - :ref:`View ` * - ``DatetimeTimedeltaMixin ceil(const std::string& freq, const std::string& ambiguous = "raise", const std::string& nonexistent = "raise") const`` - DatetimeTimedeltaMixin - pd_datetime_timedelta_mixin.h:372 - :ref:`View ` * - ``std::unique_ptr clone() const override`` - std::unique_ptr - pd_datetime_timedelta_mixin.h:558 - :ref:`View ` * - ``static ArrayType convert_array_unit(const ArrayType& arr, numpy::DateTimeUnit new_unit, int64_t factor, bool divide)`` - static ArrayType - pd_datetime_timedelta_mixin.h:706 - * - ``DatetimeTimedeltaMixin floor(const std::string& freq, const std::string& ambiguous = "raise", const std::string& nonexistent = "raise") const`` - DatetimeTimedeltaMixin - pd_datetime_timedelta_mixin.h:342 - :ref:`View ` * - ``static int64_t freq_to_nanoseconds(const std::string& freq)`` - static int64_t - pd_datetime_timedelta_mixin.h:743 - * - ``std::string inferred_type() const override`` - std::string - pd_datetime_timedelta_mixin.h:634 - :ref:`View ` * - ``static numpy::DateTimeUnit parse_unit(const std::string& unit_str)`` - static numpy::DateTimeUnit - pd_datetime_timedelta_mixin.h:660 - * - ``std::string repr() const`` - std::string - pd_datetime_timedelta_mixin.h:626 - :ref:`View ` * - ``DatetimeTimedeltaMixin result(this->array().copy(), name.has_value() ? name : this->name())`` - DatetimeTimedeltaMixin - pd_datetime_timedelta_mixin.h:572 - :ref:`View ` * - ``DatetimeTimedeltaMixin round(const std::string& freq, const std::string& ambiguous = "raise", const std::string& nonexistent = "raise") const`` - DatetimeTimedeltaMixin - pd_datetime_timedelta_mixin.h:312 - :ref:`View ` * - ``static ArrayType round_array(const ArrayType& arr, int64_t freq_ns, RoundMode mode)`` - static ArrayType - pd_datetime_timedelta_mixin.h:763 - * - ``DatetimeTimedeltaMixin snap(const std::string& freq = "S") const`` - DatetimeTimedeltaMixin - pd_datetime_timedelta_mixin.h:392 - :ref:`View ` * - ``IndexTypeId type_id() const override`` - IndexTypeId - pd_datetime_timedelta_mixin.h:562 - :ref:`View ` * - ``std::string unit() const`` - std::string - pd_datetime_timedelta_mixin.h:197 - :ref:`View ` Internal Methods ---------------- *2 internal methods (prefixed with underscore)* Code Examples ------------- The following examples are extracted from the test suite. .. _example-datetimetimedeltamixin-rename-0: .. dropdown:: rename (pd_test_1_all.cpp:5816) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5806 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_rename() { std::cout << "========= rename ======================================"; pandas::CategoricalArray arr({"x", "y"}); pandas::CategoricalIndex idx(arr, "old_name"); pandas::CategoricalIndex renamed = idx.rename("new_name"); bool passed = (renamed.name().has_value() && *renamed.name() == "new_name" && renamed.size() == idx.size() && renamed.categories() == idx.categories()); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_rename()" << std::endl; throw std::runtime_error("pd_test_categorical_index_rename failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-datetimetimedeltamixin-mean-1: .. dropdown:: mean (pd_test_1_all.cpp:282) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 272 :emphasize-lines: 11 std::optional(true), std::optional(true) }); auto s = arr.sum(); if (!s.has_value() || s.value() != 3) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : sum should be 3" << std::endl; throw std::runtime_error("pd_test_boolean_array_reductions failed: sum"); } auto m = arr.mean(); if (!m.has_value() || std::abs(m.value() - 0.75) > 0.001) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : mean should be 0.75" << std::endl; 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 ======================= "; .. _example-datetimetimedeltamixin-shift-2: .. dropdown:: shift (pd_test_1_all.cpp:5188) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5178 :emphasize-lines: 11 // First element should be NaN val = d["A"].get_value_str(0); passed = std::isnan(std::stod(val)); if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_diff_shift() : diff NaN failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_diff_shift failed: diff NaN failed"); } // shift: [NaN, 1, 3, 6] auto s = df.shift(); val = s["A"].get_value_str(1); passed = std::abs(std::stod(val) - 1.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_dataframe_diff_shift() : shift failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_dataframe_diff_shift failed: shift failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-datetimetimedeltamixin-to_string-3: .. dropdown:: to_string (pd_test_1_all.cpp:2693) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2683 :emphasize-lines: 11 pandas::PeriodArray arr_m(std::vector{ "2020-01", "NaT", "2025-06" }, "M"); // Year auto years = arr_m.year(); auto y0 = years[0]; if (!y0.has_value() || y0.value() != 2020) { std::cout << " [FAIL] : year[0] should be 2020, got " << (y0.has_value() ? std::to_string(y0.value()) : "NA") << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[0]"); } auto y1 = years[1]; if (y1.has_value()) { std::cout << " [FAIL] : year[1] should be NA (NaT)" << std::endl; throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[1] should be NA"); } auto y2 = years[2]; .. _example-datetimetimedeltamixin-copy-4: .. dropdown:: copy (pd_test_1_all.cpp:5798) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5788 :emphasize-lines: 11 // ============================================================================ // Copy/Rename Tests // ============================================================================ void pd_test_categorical_index_copy() { std::cout << "========= copy ========================================"; pandas::CategoricalArray arr({"a", "b", "c"}); pandas::CategoricalIndex idx(arr, "original"); pandas::CategoricalIndex copied = idx.copy(); bool passed = (copied.size() == idx.size() && copied.name() == idx.name() && copied.categories() == idx.categories() && copied.ordered() == idx.ordered()); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_copy()" << std::endl; throw std::runtime_error("pd_test_categorical_index_copy failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-datetimetimedeltamixin-as_unit-5: .. dropdown:: as_unit (pd_test_1_all.cpp:9361) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9351 :emphasize-lines: 11 data.setElementAt({1}, numpy::datetime64(2000000000LL, numpy::DateTimeUnit::Nanosecond)); // 2 seconds in ns numpy::NDArray mask(std::vector{2}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeTDMixin idx(arr, "test"); // Convert to microseconds pandas::DatetimeTDMixin us_idx = idx.as_unit("us"); // Convert to same unit (should return identical) pandas::DatetimeTDMixin same_idx = idx.as_unit("ns"); bool passed = (us_idx.size() == 2 && same_idx.size() == 2 && us_idx.name().has_value() && *us_idx.name() == "test"); if (!passed) { 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"); } .. _example-datetimetimedeltamixin-ceil-6: .. dropdown:: ceil (pd_test_1_all.cpp:4949) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4939 :emphasize-lines: 11 throw std::runtime_error("pd_test_arithmetic_series_round failed: round failed"); } auto f = a.floor(); passed = std::abs(f[0] - 1.0) < 0.001 && std::abs(f[2] - 3.0) < 0.001 && std::abs(f[3] - (-2.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : floor failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_round failed: floor failed"); } auto c = a.ceil(); passed = std::abs(c[0] - 2.0) < 0.001 && std::abs(c[2] - 4.0) < 0.001 && std::abs(c[3] - (-1.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : ceil failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_round failed: ceil failed"); } // Round with decimals pandas::Series b({1.234, 2.567, 3.891}); auto r2 = b.round(2); passed = std::abs(r2[0] - 1.23) < 0.001 && std::abs(r2[1] - 2.57) < 0.001; .. _example-datetimetimedeltamixin-clone-7: .. dropdown:: clone (pd_test_1_all.cpp:5776) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5766 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_clone() { std::cout << "========= clone ======================================="; pandas::CategoricalArray arr({"p", "q", "r"}); pandas::CategoricalIndex idx(arr, "original"); std::unique_ptr cloned = idx.clone(); bool passed = (cloned != nullptr && cloned->size() == idx.size() && cloned->name() == idx.name()); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_clone()" << std::endl; throw std::runtime_error("pd_test_categorical_index_clone failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-datetimetimedeltamixin-floor-8: .. dropdown:: floor (pd_test_1_all.cpp:4942) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4932 :emphasize-lines: 11 pandas::Series a({1.4, 2.5, 3.6, -1.4, -2.5}); auto r = a.round(); bool passed = std::abs(r[0] - 1.0) < 0.001 && std::abs(r[2] - 4.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : round failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_round failed: round failed"); } auto f = a.floor(); passed = std::abs(f[0] - 1.0) < 0.001 && std::abs(f[2] - 3.0) < 0.001 && std::abs(f[3] - (-2.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : floor failed" << std::endl; throw std::runtime_error("pd_test_arithmetic_series_round failed: floor failed"); } auto c = a.ceil(); passed = std::abs(c[0] - 2.0) < 0.001 && std::abs(c[2] - 4.0) < 0.001 && std::abs(c[3] - (-1.0)) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_arithmetic_series_round() : ceil failed" << std::endl; .. _example-datetimetimedeltamixin-inferred_type-9: .. dropdown:: inferred_type (pd_test_1_all.cpp:5270) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5260 :emphasize-lines: 11 } void pd_test_categorical_index_array_constructor() { std::cout << "========= array constructor ==========================="; pandas::CategoricalArray arr({"apple", "banana", "apple", "cherry"}); pandas::CategoricalIndex idx(arr, "fruits"); bool passed = (idx.size() == 4 && !idx.empty() && idx.name().has_value() && *idx.name() == "fruits" && idx.inferred_type() == "categorical"); if (!passed) { std::cout << " [FAIL] : in pd_test_categorical_index_array_constructor()" << std::endl; throw std::runtime_error("pd_test_categorical_index_array_constructor failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_categorical_index_values_constructor() { std::cout << "========= values constructor =========================="; .. _example-datetimetimedeltamixin-repr-10: .. 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-datetimetimedeltamixin-result-11: .. dropdown:: result (pd_test_1_all.cpp:15406) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15396 :emphasize-lines: 11 data.setElementAt({0}, numpy::datetime64(100LL, numpy::DateTimeUnit::Nanosecond)); data.setElementAt({1}, numpy::datetime64(200LL, numpy::DateTimeUnit::Nanosecond)); numpy::NDArray mask(std::vector{2}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeIndexBase idx(arr, "original"); // Create join result (int64 values) numpy::NDArray join_result(std::vector{3}); join_result.setElementAt({0}, numpy::int64(500LL)); join_result.setElementAt({1}, numpy::int64(600LL)); join_result.setElementAt({2}, numpy::int64(700LL)); auto new_idx = idx._from_join_target(join_result); bool passed = (new_idx.size() == 3 && new_idx.name().has_value() && *new_idx.name() == "original"); if (!passed) { .. _example-datetimetimedeltamixin-round-12: .. dropdown:: round (pd_test_1_all.cpp:1688) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1678 :emphasize-lines: 11 void pd_test_floating_array_rounding() { std::cout << "========= FloatingArray: rounding ======================= "; pandas::FloatingArray arr({ std::optional(1.234), std::optional(2.567), std::nullopt }); auto rounded = arr.round(2); if (std::abs(rounded[0].value() - 1.23) > 0.001 || std::abs(rounded[1].value() - 2.57) > 0.001) { std::cout << " [FAIL] : in pd_test_floating_array_rounding() : round(2)" << std::endl; throw std::runtime_error("pd_test_floating_array_rounding failed: round(2)"); } if (!rounded.is_na(2)) { std::cout << " [FAIL] : in pd_test_floating_array_rounding() : round should preserve NA" << std::endl; throw std::runtime_error("pd_test_floating_array_rounding failed: NA preservation"); } .. _example-datetimetimedeltamixin-snap-13: .. dropdown:: snap (pd_test_1_all.cpp:8364) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8354 :emphasize-lines: 11 void pd_test_datetime_mixin_snap() { std::cout << "========= snap ========================================"; std::vector> values = { numpy::datetime64(1000000000123456789LL, numpy::DateTimeUnit::Nanosecond) }; pandas::DatetimeArray arr(values); pandas::DatetimeMixinIndex idx(arr); pandas::DatetimeMixinIndex snapped = idx.snap("s"); bool passed = (snapped.size() == 1); if (!passed) { std::cout << " [FAIL] : in pd_test_datetime_mixin_snap()" << std::endl; throw std::runtime_error("pd_test_datetime_mixin_snap failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-datetimetimedeltamixin-type_id-14: .. dropdown:: type_id (pd_test_3_all.cpp:25592) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 25582 :emphasize-lines: 11 // ------------------- pd_test_value_classify (end) ------------------ // ------------------- pd_test_index_type_id (start) ------------------ namespace dataframe_tests_index_type_id { void pd_test_index_type_id_dispatch() { std::cout << "========= IndexTypeId dispatch ======================="; // RangeIndex ::pandas::RangeIndex ri(0, 5); if (ri.type_id() != ::pandas::IndexTypeId::RangeIndex) throw std::runtime_error("RangeIndex type_id failed"); // Index ::pandas::Index si(std::vector{"a", "b", "c"}); if (si.type_id() != ::pandas::IndexTypeId::IndexString) throw std::runtime_error("Index type_id failed"); // Index ::pandas::Index ii(std::vector{1, 2, 3}); if (ii.type_id() != ::pandas::IndexTypeId::IndexInt64) .. _example-datetimetimedeltamixin-unit-15: .. dropdown:: unit (pd_test_1_all.cpp:9284) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9274 :emphasize-lines: 11 data.setElementAt({0}, numpy::datetime64(1000LL, numpy::DateTimeUnit::Nanosecond)); data.setElementAt({1}, numpy::datetime64(2000LL, numpy::DateTimeUnit::Nanosecond)); numpy::NDArray mask(std::vector{2}); mask.setElementAt({0}, numpy::bool_(false)); mask.setElementAt({1}, numpy::bool_(false)); pandas::DatetimeArray arr(data, mask); pandas::DatetimeTDMixin idx(arr); std::string unit = idx.unit(); bool passed = (unit == "ns"); // nanosecond if (!passed) { std::cout << " [FAIL] : in pd_test_datetime_unit_property() : unit property check failed, got '" << unit << "'" << std::endl; throw std::runtime_error("pd_test_datetime_unit_property failed"); } std::cout << " -> tests passed" << std::endl; }