PeriodDtype =========== .. cpp:class:: pandas::PeriodDtype Data type class for pandas extension types. Example ------- .. code-block:: cpp #include using namespace pandas; // Use PeriodDtype PeriodDtype obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit PeriodDtype(PeriodFrequency freq)`` - pd_period_dtype.h:54 - * - ``explicit PeriodDtype(const std::string& freq_str)`` - pd_period_dtype.h:62 - Iteration --------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - pd_period_dtype.h:87 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``PeriodFrequency freq() const`` - PeriodFrequency - pd_period_dtype.h:109 - :ref:`View ` * - ``std::string freq_string() const`` - std::string - pd_period_dtype.h:116 - :ref:`View ` * - ``std::string kind() const override`` - std::string - pd_period_dtype.h:95 - :ref:`View ` * - ``bool matches(const std::string& dtype_name) const`` - bool - pd_period_dtype.h:125 - :ref:`View ` * - ``std::string name() const override`` - std::string - pd_period_dtype.h:72 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - pd_period_dtype.h:80 - * - ``std::string repr() const override`` - std::string - pd_period_dtype.h:136 - :ref:`View ` * - ``const std::type_info& type() const override`` - const std::type_info& - pd_period_dtype.h:102 - :ref:`View ` * - ``PeriodDtype with_freq(PeriodFrequency new_freq) const`` - PeriodDtype - pd_period_dtype.h:143 - * - ``PeriodDtype with_freq(const std::string& new_freq) const`` - PeriodDtype - pd_period_dtype.h:150 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-perioddtype-freq-0: .. dropdown:: freq (pd_test_1_all.cpp:8233) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8223 :emphasize-lines: 11 std::cout << "========= freq property ==============================="; std::vector> values = { numpy::datetime64(0LL, numpy::DateTimeUnit::Nanosecond), numpy::datetime64(86400000000000LL, numpy::DateTimeUnit::Nanosecond) // 1 day }; pandas::DatetimeArray arr(values); pandas::DatetimeMixinIndex idx(arr); // Default freq is nullopt or inferred auto f = idx.freq(); std::string fs = idx.freqstr(); bool passed = true; // freq may or may not be set if (!passed) { std::cout << " [FAIL] : in pd_test_datetime_mixin_freq()" << std::endl; throw std::runtime_error("pd_test_datetime_mixin_freq failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-perioddtype-freq_string-1: .. dropdown:: freq_string (pd_test_1_all.cpp:15733) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15723 :emphasize-lines: 11 bool passed = (isnan_mask.size() == 2); if (!passed) { std::cout << " [FAIL] : in pd_test_period_index_isnan() : _isnan check failed" << std::endl; throw std::runtime_error("pd_test_period_index_isnan failed"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // unit() and freq_string() Tests // ============================================================================ void pd_test_unit_freq_string() { std::cout << "========= unit() / freq_string() ========================="; // PeriodIndex with monthly frequency numpy::NDArray data(std::vector{1}); data.setElementAt({0}, numpy::int64(0)); numpy::NDArray mask(std::vector{1}); .. _example-perioddtype-kind-2: .. 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-perioddtype-matches-3: .. 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-perioddtype-name-4: .. 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-perioddtype-repr-5: .. 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-perioddtype-type-6: .. 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