TimedeltaDtype ============== .. cpp:class:: pandas::TimedeltaDtype Data type class for pandas extension types. Example ------- .. code-block:: cpp #include using namespace pandas; // Use TimedeltaDtype TimedeltaDtype obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit TimedeltaDtype(numpy::DateTimeUnit unit)`` - pd_timedelta_dtype.h:43 - Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - pd_timedelta_dtype.h:69 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string kind() const override`` - std::string - pd_timedelta_dtype.h:77 - :ref:`View ` * - ``bool matches(const std::string& dtype_name) const`` - bool - pd_timedelta_dtype.h:119 - :ref:`View ` * - ``std::string name() const override`` - std::string - pd_timedelta_dtype.h:53 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - pd_timedelta_dtype.h:62 - * - ``std::string repr() const override`` - std::string - pd_timedelta_dtype.h:130 - :ref:`View ` * - ``const std::type_info& type() const override`` - const std::type_info& - pd_timedelta_dtype.h:84 - :ref:`View ` * - ``numpy::DateTimeUnit unit() const`` - numpy::DateTimeUnit - pd_timedelta_dtype.h:91 - :ref:`View ` * - ``std::string unit_string() const`` - std::string - pd_timedelta_dtype.h:98 - * - ``TimedeltaDtype with_unit(numpy::DateTimeUnit new_unit) const`` - TimedeltaDtype - pd_timedelta_dtype.h:139 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-timedeltadtype-kind-0: .. dropdown:: kind (pd_test_1_all.cpp:300) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 290 :emphasize-lines: 11 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; } } int pd_test_boolean_array_main() { std::cout << "====================================== running pd_test_boolean_array ==================================== " << std::endl; .. _example-timedeltadtype-matches-1: .. dropdown:: matches (pd_test_5_all.cpp:15741) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15731 :emphasize-lines: 11 for (size_t i = 0; i < 5; ++i) { // DatetimeIndex serializes individual entries via get_value_str() bdate_first5.push_back(idx.get_value_str(i)); } std::cout << " case_4 bdate_range first 5: "; for (auto& s : bdate_first5) std::cout << s << " "; std::cout << "\n case_4 expected first 5: "; for (auto& s : expected_visible_head) std::cout << s << " "; std::cout << "\n"; // First date matches (both start at 2023-01-04) pandas_tests::check(contains(bdate_first5[0], "2023-01-04"), "case_4.first_bdate_is_2023-01-04", local_fail); // Second date should differ: bdate gives 2023-01-05, fixture wants 2023-01-20 bool second_matches_expected = contains(bdate_first5[1], "2023-01-20"); pandas_tests::check(!second_matches_expected, "case_4.bdate_NOT_matching_filtered_fixture", local_fail); // The bdate second is the next business day: pandas_tests::check(contains(bdate_first5[1], "2023-01-05"), "case_4.bdate_second_is_2023-01-05_sequential", local_fail); .. _example-timedeltadtype-name-2: .. 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-timedeltadtype-repr-3: .. 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-timedeltadtype-type-4: .. dropdown:: type (pd_test_3_all.cpp:15450) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15440 :emphasize-lines: 11 /** * Test Series.convert_dtypes() parameter flags */ void pd_test_series_convert_dtypes_flags() { std::cout << "========= Series.convert_dtypes() flags ================="; // Test convert_integer=false - with floats disabled too, so it becomes string/object pandas::Series s({"1", "2", "3"}, "numbers"); auto converted = s.convert_dtypes(true, true, false, true, false); // convert_integer=false, convert_floating=false // Should remain object type (Series has dtype_name()="object") // When integer and floating are both disabled for integer-like strings, it falls back to string type if (converted->dtype_name() != "object") { std::cout << " [FAIL] : dtype should be object when convert_integer=false and convert_floating=false, got " << converted->dtype_name() << std::endl; throw std::runtime_error("pd_test_series_convert_dtypes_flags failed: convert_integer"); } // Test convert_boolean=false - strings stay as object/string type pandas::Series s_bool({"true", "false"}, "bools"); auto converted_bool = s_bool.convert_dtypes(true, true, true, false, true); // convert_boolean=false .. _example-timedeltadtype-unit-5: .. 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; }