DatetimeTimedeltaMixin#

class pandas::DatetimeTimedeltaMixin#

Mixin class providing shared functionality.

Example#

#include <pandas/pandas.h>
using namespace pandas;

// Use DatetimeTimedeltaMixin
DatetimeTimedeltaMixin obj;
// ... operations ...

Constructors#

Signature

Location

Example

explicit DatetimeTimedeltaMixin(const ArrayType& array, const std::optional<std::string>& name = std::nullopt)

pd_datetime_timedelta_mixin.h:105

explicit DatetimeTimedeltaMixin(ArrayType&& array, const std::optional<std::string>& 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#

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#

Signature

Return Type

Location

Example

DatetimeTimedeltaMixin rename(const std::optional<std::string>& new_name) const

DatetimeTimedeltaMixin

pd_datetime_timedelta_mixin.h:581

View

Statistics#

Signature

Return Type

Location

Example

std::optional<value_type> mean(bool skipna = true, std::optional<int> axis = std::nullopt) const

std::optional<value_type>

pd_datetime_timedelta_mixin.h:447

View

Time Series#

Signature

Return Type

Location

Example

DatetimeTimedeltaMixin shift(int64_t periods, const std::optional<std::string>& freq = std::nullopt) const

DatetimeTimedeltaMixin

pd_datetime_timedelta_mixin.h:508

View

I/O#

Signature

Return Type

Location

Example

std::string to_string() const override

std::string

pd_datetime_timedelta_mixin.h:589

View

Conversion#

Signature

Return Type

Location

Example

DatetimeTimedeltaMixin copy(const std::optional<std::string>& name = std::nullopt) const

DatetimeTimedeltaMixin

pd_datetime_timedelta_mixin.h:571

View

Other Methods#

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

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

View

std::unique_ptr<IndexBase> clone() const override

std::unique_ptr<IndexBase>

pd_datetime_timedelta_mixin.h:558

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

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

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

View

DatetimeTimedeltaMixin result(this->array().copy(), name.has_value() ? name : this->name())

DatetimeTimedeltaMixin

pd_datetime_timedelta_mixin.h:572

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

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

View

IndexTypeId type_id() const override

IndexTypeId

pd_datetime_timedelta_mixin.h:562

View

std::string unit() const

std::string

pd_datetime_timedelta_mixin.h:197

View

Internal Methods#

2 internal methods (prefixed with underscore)

Code Examples#

The following examples are extracted from the test suite.

rename (pd_test_1_all.cpp:5816)
5806    std::cout << " -> tests passed" << std::endl;
5807}
5808
5809void pd_test_categorical_index_rename() {
5810    std::cout << "========= rename ======================================";
5811
5812    pandas::CategoricalArray arr({"x", "y"});
5813    pandas::CategoricalIndex idx(arr, "old_name");
5814
5815    pandas::CategoricalIndex renamed = idx.rename("new_name");
5816
5817    bool passed = (renamed.name().has_value() && *renamed.name() == "new_name" &&
5818                   renamed.size() == idx.size() && renamed.categories() == idx.categories());
5819    if (!passed) {
5820        std::cout << "  [FAIL] : in pd_test_categorical_index_rename()" << std::endl;
5821        throw std::runtime_error("pd_test_categorical_index_rename failed");
5822    }
5823
5824    std::cout << " -> tests passed" << std::endl;
5825}
mean (pd_test_1_all.cpp:282)
272            std::optional<bool>(true),
273            std::optional<bool>(true)
274        });
275
276        auto s = arr.sum();
277        if (!s.has_value() || s.value() != 3) {
278            std::cout << "  [FAIL] : in pd_test_boolean_array_reductions() : sum should be 3" << std::endl;
279            throw std::runtime_error("pd_test_boolean_array_reductions failed: sum");
280        }
281
282        auto m = arr.mean();
283        if (!m.has_value() || std::abs(m.value() - 0.75) > 0.001) {
284            std::cout << "  [FAIL] : in pd_test_boolean_array_reductions() : mean should be 0.75" << std::endl;
285            throw std::runtime_error("pd_test_boolean_array_reductions failed: mean");
286        }
287
288        std::cout << " -> tests passed" << std::endl;
289    }
290
291    void pd_test_boolean_array_dtype() {
292        std::cout << "========= BooleanArray: dtype ======================= ";
shift (pd_test_1_all.cpp:5188)
5178            // First element should be NaN
5179            val = d["A"].get_value_str(0);
5180            passed = std::isnan(std::stod(val));
5181            if (!passed) {
5182                std::cout << "  [FAIL] : in pd_test_arithmetic_dataframe_diff_shift() : diff NaN failed" << std::endl;
5183                throw std::runtime_error("pd_test_arithmetic_dataframe_diff_shift failed: diff NaN failed");
5184            }
5185
5186            // shift: [NaN, 1, 3, 6]
5187            auto s = df.shift();
5188            val = s["A"].get_value_str(1);
5189            passed = std::abs(std::stod(val) - 1.0) < 0.001;
5190            if (!passed) {
5191                std::cout << "  [FAIL] : in pd_test_arithmetic_dataframe_diff_shift() : shift failed" << std::endl;
5192                throw std::runtime_error("pd_test_arithmetic_dataframe_diff_shift failed: shift failed");
5193            }
5194
5195            std::cout << " -> tests passed" << std::endl;
5196        }
to_string (pd_test_1_all.cpp:2693)
2683        pandas::PeriodArray arr_m(std::vector<std::string>{
2684            "2020-01",
2685            "NaT",
2686            "2025-06"
2687        }, "M");
2688
2689        // Year
2690        auto years = arr_m.year();
2691        auto y0 = years[0];
2692        if (!y0.has_value() || y0.value() != 2020) {
2693            std::cout << "  [FAIL] : year[0] should be 2020, got " << (y0.has_value() ? std::to_string(y0.value()) : "NA") << std::endl;
2694            throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[0]");
2695        }
2696
2697        auto y1 = years[1];
2698        if (y1.has_value()) {
2699            std::cout << "  [FAIL] : year[1] should be NA (NaT)" << std::endl;
2700            throw std::runtime_error("pd_test_period_array_year_month_quarter failed: year[1] should be NA");
2701        }
2702
2703        auto y2 = years[2];
copy (pd_test_1_all.cpp:5798)
5788// ============================================================================
5789// Copy/Rename Tests
5790// ============================================================================
5791
5792void pd_test_categorical_index_copy() {
5793    std::cout << "========= copy ========================================";
5794
5795    pandas::CategoricalArray arr({"a", "b", "c"});
5796    pandas::CategoricalIndex idx(arr, "original");
5797
5798    pandas::CategoricalIndex copied = idx.copy();
5799
5800    bool passed = (copied.size() == idx.size() && copied.name() == idx.name() &&
5801                   copied.categories() == idx.categories() && copied.ordered() == idx.ordered());
5802    if (!passed) {
5803        std::cout << "  [FAIL] : in pd_test_categorical_index_copy()" << std::endl;
5804        throw std::runtime_error("pd_test_categorical_index_copy failed");
5805    }
5806
5807    std::cout << " -> tests passed" << std::endl;
5808}
as_unit (pd_test_1_all.cpp:9361)
9351    data.setElementAt({1}, numpy::datetime64(2000000000LL, numpy::DateTimeUnit::Nanosecond));  // 2 seconds in ns
9352
9353    numpy::NDArray<numpy::bool_> mask(std::vector<size_t>{2});
9354    mask.setElementAt({0}, numpy::bool_(false));
9355    mask.setElementAt({1}, numpy::bool_(false));
9356
9357    pandas::DatetimeArray arr(data, mask);
9358    pandas::DatetimeTDMixin idx(arr, "test");
9359
9360    // Convert to microseconds
9361    pandas::DatetimeTDMixin us_idx = idx.as_unit("us");
9362
9363    // Convert to same unit (should return identical)
9364    pandas::DatetimeTDMixin same_idx = idx.as_unit("ns");
9365
9366    bool passed = (us_idx.size() == 2 && same_idx.size() == 2 &&
9367                   us_idx.name().has_value() && *us_idx.name() == "test");
9368    if (!passed) {
9369        std::cout << "  [FAIL] : in pd_test_datetime_as_unit() : as_unit check failed" << std::endl;
9370        throw std::runtime_error("pd_test_datetime_as_unit failed");
9371    }
ceil (pd_test_1_all.cpp:4949)
4939                throw std::runtime_error("pd_test_arithmetic_series_round failed: round failed");
4940            }
4941
4942            auto f = a.floor();
4943            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;
4944            if (!passed) {
4945                std::cout << "  [FAIL] : in pd_test_arithmetic_series_round() : floor failed" << std::endl;
4946                throw std::runtime_error("pd_test_arithmetic_series_round failed: floor failed");
4947            }
4948
4949            auto c = a.ceil();
4950            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;
4951            if (!passed) {
4952                std::cout << "  [FAIL] : in pd_test_arithmetic_series_round() : ceil failed" << std::endl;
4953                throw std::runtime_error("pd_test_arithmetic_series_round failed: ceil failed");
4954            }
4955
4956            // Round with decimals
4957            pandas::Series<double> b({1.234, 2.567, 3.891});
4958            auto r2 = b.round(2);
4959            passed = std::abs(r2[0] - 1.23) < 0.001 && std::abs(r2[1] - 2.57) < 0.001;
clone (pd_test_1_all.cpp:5776)
5766    std::cout << " -> tests passed" << std::endl;
5767}
5768
5769void pd_test_categorical_index_clone() {
5770    std::cout << "========= clone =======================================";
5771
5772    pandas::CategoricalArray arr({"p", "q", "r"});
5773    pandas::CategoricalIndex idx(arr, "original");
5774
5775    std::unique_ptr<pandas::IndexBase> cloned = idx.clone();
5776
5777    bool passed = (cloned != nullptr && cloned->size() == idx.size() &&
5778                   cloned->name() == idx.name());
5779    if (!passed) {
5780        std::cout << "  [FAIL] : in pd_test_categorical_index_clone()" << std::endl;
5781        throw std::runtime_error("pd_test_categorical_index_clone failed");
5782    }
5783
5784    std::cout << " -> tests passed" << std::endl;
5785}
floor (pd_test_1_all.cpp:4942)
4932            pandas::Series<double> a({1.4, 2.5, 3.6, -1.4, -2.5});
4933
4934            auto r = a.round();
4935            bool passed = std::abs(r[0] - 1.0) < 0.001 && std::abs(r[2] - 4.0) < 0.001;
4936            if (!passed) {
4937                std::cout << "  [FAIL] : in pd_test_arithmetic_series_round() : round failed" << std::endl;
4938                throw std::runtime_error("pd_test_arithmetic_series_round failed: round failed");
4939            }
4940
4941            auto f = a.floor();
4942            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;
4943            if (!passed) {
4944                std::cout << "  [FAIL] : in pd_test_arithmetic_series_round() : floor failed" << std::endl;
4945                throw std::runtime_error("pd_test_arithmetic_series_round failed: floor failed");
4946            }
4947
4948            auto c = a.ceil();
4949            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;
4950            if (!passed) {
4951                std::cout << "  [FAIL] : in pd_test_arithmetic_series_round() : ceil failed" << std::endl;
inferred_type (pd_test_1_all.cpp:5270)
5260}
5261
5262void pd_test_categorical_index_array_constructor() {
5263    std::cout << "========= array constructor ===========================";
5264
5265    pandas::CategoricalArray arr({"apple", "banana", "apple", "cherry"});
5266    pandas::CategoricalIndex idx(arr, "fruits");
5267
5268    bool passed = (idx.size() == 4 && !idx.empty() &&
5269                   idx.name().has_value() && *idx.name() == "fruits" &&
5270                   idx.inferred_type() == "categorical");
5271    if (!passed) {
5272        std::cout << "  [FAIL] : in pd_test_categorical_index_array_constructor()" << std::endl;
5273        throw std::runtime_error("pd_test_categorical_index_array_constructor failed");
5274    }
5275
5276    std::cout << " -> tests passed" << std::endl;
5277}
5278
5279void pd_test_categorical_index_values_constructor() {
5280    std::cout << "========= values constructor ==========================";
repr (pd_test_1_all.cpp:10906)
10896    std::cout << " -> tests passed" << std::endl;
10897}
10898
10899void pd_test_extension_index_repr() {
10900    std::cout << "========= repr =========================";
10901
10902    pandas::CategoricalArray arr({"a", "b", "c"});
10903    // Use ExtensionIndex<CategoricalArray> directly to test base class repr
10904    pandas::ExtensionIndex<pandas::CategoricalArray> idx(arr, "test");
10905
10906    std::string repr_str = idx.repr();
10907
10908    bool passed = (!repr_str.empty() && repr_str.find("ExtensionIndex") != std::string::npos);
10909    if (!passed) {
10910        std::cout << "  [FAIL] : in pd_test_extension_index_repr() : repr check failed" << std::endl;
10911        throw std::runtime_error("pd_test_extension_index_repr failed");
10912    }
10913
10914    std::cout << " -> tests passed" << std::endl;
10915}
result (pd_test_1_all.cpp:15406)
15396    data.setElementAt({0}, numpy::datetime64(100LL, numpy::DateTimeUnit::Nanosecond));
15397    data.setElementAt({1}, numpy::datetime64(200LL, numpy::DateTimeUnit::Nanosecond));
15398
15399    numpy::NDArray<numpy::bool_> mask(std::vector<size_t>{2});
15400    mask.setElementAt({0}, numpy::bool_(false));
15401    mask.setElementAt({1}, numpy::bool_(false));
15402
15403    pandas::DatetimeArray arr(data, mask);
15404    pandas::DatetimeIndexBase idx(arr, "original");
15405
15406    // Create join result (int64 values)
15407    numpy::NDArray<numpy::int64> join_result(std::vector<size_t>{3});
15408    join_result.setElementAt({0}, numpy::int64(500LL));
15409    join_result.setElementAt({1}, numpy::int64(600LL));
15410    join_result.setElementAt({2}, numpy::int64(700LL));
15411
15412    auto new_idx = idx._from_join_target(join_result);
15413
15414    bool passed = (new_idx.size() == 3 &&
15415                   new_idx.name().has_value() && *new_idx.name() == "original");
15416    if (!passed) {
round (pd_test_1_all.cpp:1688)
1678    void pd_test_floating_array_rounding() {
1679        std::cout << "========= FloatingArray: rounding ======================= ";
1680
1681        pandas::FloatingArray<double> arr({
1682            std::optional<double>(1.234),
1683            std::optional<double>(2.567),
1684            std::nullopt
1685        });
1686
1687        auto rounded = arr.round(2);
1688        if (std::abs(rounded[0].value() - 1.23) > 0.001 ||
1689            std::abs(rounded[1].value() - 2.57) > 0.001) {
1690            std::cout << "  [FAIL] : in pd_test_floating_array_rounding() : round(2)" << std::endl;
1691            throw std::runtime_error("pd_test_floating_array_rounding failed: round(2)");
1692        }
1693
1694        if (!rounded.is_na(2)) {
1695            std::cout << "  [FAIL] : in pd_test_floating_array_rounding() : round should preserve NA" << std::endl;
1696            throw std::runtime_error("pd_test_floating_array_rounding failed: NA preservation");
1697        }
snap (pd_test_1_all.cpp:8364)
8354void pd_test_datetime_mixin_snap() {
8355    std::cout << "========= snap ========================================";
8356
8357    std::vector<std::optional<numpy::datetime64>> values = {
8358        numpy::datetime64(1000000000123456789LL, numpy::DateTimeUnit::Nanosecond)
8359    };
8360    pandas::DatetimeArray arr(values);
8361    pandas::DatetimeMixinIndex idx(arr);
8362
8363    pandas::DatetimeMixinIndex snapped = idx.snap("s");
8364
8365    bool passed = (snapped.size() == 1);
8366    if (!passed) {
8367        std::cout << "  [FAIL] : in pd_test_datetime_mixin_snap()" << std::endl;
8368        throw std::runtime_error("pd_test_datetime_mixin_snap failed");
8369    }
8370
8371    std::cout << " -> tests passed" << std::endl;
8372}
type_id (pd_test_3_all.cpp:25592)
25582// ------------------- pd_test_value_classify (end) ------------------
25583
25584// ------------------- pd_test_index_type_id (start) ------------------
25585namespace dataframe_tests_index_type_id {
25586
25587void pd_test_index_type_id_dispatch() {
25588    std::cout << "========= IndexTypeId dispatch =======================";
25589
25590    // RangeIndex
25591    ::pandas::RangeIndex ri(0, 5);
25592    if (ri.type_id() != ::pandas::IndexTypeId::RangeIndex)
25593        throw std::runtime_error("RangeIndex type_id failed");
25594
25595    // Index<string>
25596    ::pandas::Index<std::string> si(std::vector<std::string>{"a", "b", "c"});
25597    if (si.type_id() != ::pandas::IndexTypeId::IndexString)
25598        throw std::runtime_error("Index<string> type_id failed");
25599
25600    // Index<int64>
25601    ::pandas::Index<numpy::int64> ii(std::vector<numpy::int64>{1, 2, 3});
25602    if (ii.type_id() != ::pandas::IndexTypeId::IndexInt64)
unit (pd_test_1_all.cpp:9284)
9274    data.setElementAt({0}, numpy::datetime64(1000LL, numpy::DateTimeUnit::Nanosecond));
9275    data.setElementAt({1}, numpy::datetime64(2000LL, numpy::DateTimeUnit::Nanosecond));
9276
9277    numpy::NDArray<numpy::bool_> mask(std::vector<size_t>{2});
9278    mask.setElementAt({0}, numpy::bool_(false));
9279    mask.setElementAt({1}, numpy::bool_(false));
9280
9281    pandas::DatetimeArray arr(data, mask);
9282    pandas::DatetimeTDMixin idx(arr);
9283
9284    std::string unit = idx.unit();
9285
9286    bool passed = (unit == "ns");  // nanosecond
9287    if (!passed) {
9288        std::cout << "  [FAIL] : in pd_test_datetime_unit_property() : unit property check failed, got '" << unit << "'" << std::endl;
9289        throw std::runtime_error("pd_test_datetime_unit_property failed");
9290    }
9291
9292    std::cout << " -> tests passed" << std::endl;
9293}