PyTimedelta =========== .. cpp:class:: pandas::PyTimedelta Scalar type representing a single value. Example ------- .. code-block:: cpp #include using namespace pandas; // Use PyTimedelta PyTimedelta obj; // ... operations ... I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string to_string() const`` - std::string - pd_timedelta_array.h:90 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``double total_seconds() const`` - double - pd_timedelta_array.h:81 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-pytimedelta-to_string-0: .. 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-pytimedelta-total_seconds-1: .. dropdown:: total_seconds (pd_test_1_all.cpp:4224) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4214 :emphasize-lines: 11 void pd_test_timedelta_array_total_seconds() { std::cout << "========= TimedeltaArray: total_seconds ======================= "; pandas::TimedeltaArray arr({ std::optional(numpy::timedelta64(1, numpy::DateTimeUnit::Day)), std::optional(numpy::timedelta64(1, numpy::DateTimeUnit::Hour)), std::nullopt }); auto total = arr.total_seconds(); auto t0 = total[0]; if (!t0.has_value() || std::abs(t0.value() - 86400.0) > 0.001) { std::cout << " [FAIL] : total_seconds[0] should be 86400" << std::endl; throw std::runtime_error("pd_test_timedelta_array_total_seconds failed: total_seconds[0]"); } auto t1 = total[1]; if (!t1.has_value() || std::abs(t1.value() - 3600.0) > 0.001) { std::cout << " [FAIL] : total_seconds[1] should be 3600" << std::endl;