DatetimeTZDtype#
-
class pandas::DatetimeTZDtype#
pandas C++ class.
Example#
#include <pandas/pandas.h>
using namespace pandas;
// Use DatetimeTZDtype
DatetimeTZDtype obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
pd_datetime_tz_dtype.h:47 |
Iteration#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
size_t |
pd_datetime_tz_dtype.h:73 |
Type Checking#
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::string |
pd_datetime_tz_dtype.h:77 |
|
|
std::string |
pd_datetime_tz_dtype.h:57 |
|
|
numpy::DType |
pd_datetime_tz_dtype.h:69 |
|
|
static DatetimeUnit |
pd_datetime_tz_dtype.h:27 |
|
|
std::string |
pd_datetime_tz_dtype.h:64 |
|
|
const std::type_info& |
pd_datetime_tz_dtype.h:81 |
|
|
std::optional<std::string> |
pd_datetime_tz_dtype.h:53 |
|
|
std::string |
pd_datetime_tz_dtype.h:52 |
|
|
static std::string |
pd_datetime_tz_dtype.h:35 |
Code Examples#
The following examples are extracted from the test suite.
is_dtype (pd_test_3_all.cpp:18831)
18821 }
18822 if (!(dt1 != dt3)) {
18823 std::cout << " [FAIL] : != operator failed" << std::endl;
18824 throw std::runtime_error("pd_test_equality: != operator failed");
18825 }
18826
18827 std::cout << " -> tests passed" << std::endl;
18828}
18829
18830// ============================================================================
18831// Test is_dtype() method
18832// ============================================================================
18833
18834void pd_test_is_dtype() {
18835 std::cout << "========= DatetimeTZDtype: is_dtype() ======================";
18836
18837 pandas::DatetimeTZDtype dt("ns", "UTC");
18838
18839 if (!dt.is_dtype("datetime64[ns, UTC]")) {
18840 std::cout << " [FAIL] : is_dtype() exact match failed" << std::endl;
18841 throw std::runtime_error("pd_test_is_dtype: exact match failed");
is_tz_aware (pd_test_1_all.cpp:1425)
1415 void pd_test_datetime_array_timezone() {
1416 std::cout << "========= DatetimeArray: timezone ======================= ";
1417
1418 pandas::DatetimeArray arr(std::vector<std::string>{
1419 "2023-01-01",
1420 "2023-06-15"
1421 });
1422
1423 // Initially should be timezone-naive
1424 if (arr.is_tz_aware()) {
1425 std::cout << " [FAIL] : array should be timezone-naive initially" << std::endl;
1426 throw std::runtime_error("pd_test_datetime_array_timezone failed: naive");
1427 }
1428
1429 // Localize to UTC
1430 auto localized = arr.tz_localize("UTC");
1431 if (!localized.is_tz_aware()) {
1432 std::cout << " [FAIL] : localized array should be timezone-aware" << std::endl;
1433 throw std::runtime_error("pd_test_datetime_array_timezone failed: localize");
1434 }
kind (pd_test_1_all.cpp:300)
290 void pd_test_boolean_array_dtype() {
291 std::cout << "========= BooleanArray: dtype ======================= ";
292
293 pandas::BooleanArray arr;
294 if (arr.dtype().name() != "boolean") {
295 std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl;
296 throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name");
297 }
298
299 if (arr.dtype().kind() != "b") {
300 std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl;
301 throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind");
302 }
303
304 std::cout << " -> tests passed" << std::endl;
305 }
306}
307
308int pd_test_boolean_array_main() {
309 std::cout << "====================================== running pd_test_boolean_array ==================================== " << std::endl;
name (pd_test_1_all.cpp:295)
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 ======================= ";
293
294 pandas::BooleanArray arr;
295 if (arr.dtype().name() != "boolean") {
296 std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype name should be 'boolean'" << std::endl;
297 throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype name");
298 }
299
300 if (arr.dtype().kind() != "b") {
301 std::cout << " [FAIL] : in pd_test_boolean_array_dtype() : dtype kind should be 'b'" << std::endl;
302 throw std::runtime_error("pd_test_boolean_array_dtype failed: dtype kind");
303 }
304
305 std::cout << " -> tests passed" << std::endl;
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}
type (pd_test_3_all.cpp:15450)
15440/**
15441 * Test Series.convert_dtypes() parameter flags
15442 */
15443void pd_test_series_convert_dtypes_flags() {
15444 std::cout << "========= Series.convert_dtypes() flags =================";
15445
15446 // Test convert_integer=false - with floats disabled too, so it becomes string/object
15447 pandas::Series<std::string> s({"1", "2", "3"}, "numbers");
15448 auto converted = s.convert_dtypes(true, true, false, true, false); // convert_integer=false, convert_floating=false
15449
15450 // Should remain object type (Series<std::string> has dtype_name()="object")
15451 // When integer and floating are both disabled for integer-like strings, it falls back to string type
15452 if (converted->dtype_name() != "object") {
15453 std::cout << " [FAIL] : dtype should be object when convert_integer=false and convert_floating=false, got " << converted->dtype_name() << std::endl;
15454 throw std::runtime_error("pd_test_series_convert_dtypes_flags failed: convert_integer");
15455 }
15456
15457 // Test convert_boolean=false - strings stay as object/string type
15458 pandas::Series<std::string> s_bool({"true", "false"}, "bools");
15459 auto converted_bool = s_bool.convert_dtypes(true, true, true, false, true); // convert_boolean=false
tz (pd_test_2_all.cpp:17914)
17904 pandas::DataFrame df(data);
17905 df.set_index(std::make_unique<pandas::DatetimeIndex>(tz_aware_idx));
17906
17907 // Verify the index is timezone-aware
17908 const pandas::DatetimeIndex* original_idx = dynamic_cast<const pandas::DatetimeIndex*>(&df.index());
17909 if (!original_idx) {
17910 std::cout << " [FAIL] : in pd_test_tz_convert_basic() : index is not DatetimeIndex" << std::endl;
17911 throw std::runtime_error("pd_test_tz_convert_basic failed: index is not DatetimeIndex");
17912 }
17913
17914 std::string original_tz = original_idx->tz();
17915 if (original_tz.empty()) {
17916 std::cout << " [FAIL] : in pd_test_tz_convert_basic() : original index is not timezone-aware" << std::endl;
17917 throw std::runtime_error("pd_test_tz_convert_basic failed: original index is not timezone-aware");
17918 }
17919
17920 // Convert to Asia/Shanghai timezone
17921 pandas::DataFrame df_shanghai = df.tz_convert("Asia/Shanghai");
17922
17923 // Verify result has a DatetimeIndex
17924 const pandas::DatetimeIndex* converted_idx = dynamic_cast<const pandas::DatetimeIndex*>(&df_shanghai.index());
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}