MonthBegin#
-
class pandas::MonthBegin#
pandas C++ class.
Example#
#include <pandas/pandas.h>
using namespace pandas;
// Use MonthBegin
MonthBegin obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
pd_dateoffset.h:684 |
Aggregation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
numpy::datetime64 |
pd_dateoffset.h:695 |
Comparison#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::unique_ptr<DateOffset> |
pd_dateoffset.h:691 |
Type Checking#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
pd_dateoffset.h:688 |
|
|
bool |
pd_dateoffset.h:722 |
Other Methods#
Code Examples#
The following examples are extracted from the test suite.
MonthBegin (pd_test_5_all.cpp:87101)
87091 std::cout << "-- case_5_monthend_of_yearly\n";
87092 auto idx = mk_idx({"2020-12-31", "2021-12-31", "2022-12-31"});
87093 auto up = idx.upsample(pandas::MonthEnd(1));
87094 pandas_tests::check(up.size() == 25,
87095 "case_5.monthend_of_yearly.size==25", local_fail);
87096}
87097
87098void f_core_05_upsample_05f4ab_case_6_monthbegin_of_yearly(int& local_fail) {
87099 std::cout << "-- case_6_monthbegin_of_yearly\n";
87100 auto idx = mk_idx({"2020-01-01", "2021-01-01", "2022-01-01"});
87101 auto up = idx.upsample(pandas::MonthBegin(1));
87102 pandas_tests::check(up.size() == 25,
87103 "case_6.monthbegin_of_yearly.size==25", local_fail);
87104}
87105
87106void f_core_05_upsample_05f4ab_case_7_two_month_stride(int& local_fail) {
87107 std::cout << "-- case_7_two_month_stride\n";
87108 auto idx = mk_idx({"2020-12-31", "2022-12-31"});
87109 auto up = idx.upsample(pandas::MonthEnd(2));
87110 pandas_tests::check(up.size() == 13,
87111 "case_7.two_month_stride.size==13", local_fail);
apply (pd_test_1_all.cpp:11244)
11234 void pd_test_func_apply_dataframe_apply_axis0() {
11235 std::cout << "========= DataFrame apply axis=0 ======================";
11236
11237 std::map<std::string, std::vector<double>> data = {
11238 {"A", {1.0, 2.0, 3.0}},
11239 {"B", {4.0, 5.0, 6.0}}
11240 };
11241 pandas::DataFrame df(data);
11242
11243 // apply axis=0 applies function to each column
11244 auto result = df.apply([](const std::vector<double>& col) {
11245 return std::accumulate(col.begin(), col.end(), 0.0);
11246 }, 0);
11247
11248 bool passed = true;
11249
11250 // Plan F·dtype: axis=0 reduce now returns a single "result" column
11251 // with the original column names ("A", "B") as the row index.
11252 // Sum of A: 1+2+3=6, Sum of B: 4+5+6=15
11253 const auto& result_col = result["result"];
11254 double sum_a = std::stod(result_col.get_value_str(0));
negate (pd_test_4_all.cpp:6343)
6333 EXPECT(static_cast<int64_t>(out[1]) == 6LL * 86400000000000LL);
6334 EXPECT(out.dtype_name() == "datetime64[ns]");
6335}
6336
6337void test_sub_dateoffset_calendar_monthend() {
6338 // 2024-01-31 in ns
6339 int64_t jan31 = 1706659200LL * 1000000000LL;
6340 auto s = make_dt_series({jan31});
6341 pandas::MonthEnd me(1);
6342 auto out = s.sub_dateoffset(me);
6343 auto neg = me.negate();
6344 auto ref = s.add_dateoffset(*neg);
6345 EXPECT(out.size() == 1);
6346 EXPECT(static_cast<int64_t>(out[0]) == static_cast<int64_t>(ref[0]));
6347 EXPECT(out.dtype_name() == "datetime64[ns]");
6348}
6349
6350void test_sub_dateoffset_equals_add_negated() {
6351 int64_t jan31 = 1706659200LL * 1000000000LL;
6352 auto s = make_dt_series({jan31, jan31 + 86400000000000LL});
6353 pandas::MonthEnd me(2);
is_on_offset (pd_test_3_all.cpp:18263)
18253void pd_test_business_day_offset() {
18254 std::cout << "========= BusinessDay offset ===========================";
18255
18256 pandas::BusinessDay offset(1);
18257 if (offset.freqstr() != "B") {
18258 std::cout << " [FAIL] : BusinessDay freqstr() failed" << std::endl;
18259 throw std::runtime_error("pd_test_business_day_offset: freqstr() failed");
18260 }
18261
18262 // Test is_on_offset (Friday = weekday)
18263 numpy::datetime64 friday("2020-01-17"); // Friday
18264 if (!offset.is_on_offset(friday)) {
18265 std::cout << " [FAIL] : BusinessDay is_on_offset(Friday) failed" << std::endl;
18266 throw std::runtime_error("pd_test_business_day_offset: is_on_offset(Friday) failed");
18267 }
18268
18269 // Test is_on_offset (Saturday = weekend)
18270 numpy::datetime64 saturday("2020-01-18"); // Saturday
18271 if (offset.is_on_offset(saturday)) {
18272 std::cout << " [FAIL] : BusinessDay is_on_offset(Saturday) should be false" << std::endl;
freqstr (pd_test_1_all.cpp:2671)
2661 }
2662
2663 pandas::PeriodDtype dtype_y("Y");
2664 if (dtype_y.name() != "period[Y]") {
2665 std::cout << " [FAIL] : dtype_y.name() should be 'period[Y]'" << std::endl;
2666 throw std::runtime_error("pd_test_period_array_freq_validation failed: dtype name Y");
2667 }
2668
2669 // Test frequency string
2670 pandas::PeriodArray arr(std::vector<std::string>{"2024-01-15"}, "D");
2671 if (arr.freqstr() != "D") {
2672 std::cout << " [FAIL] : arr.freqstr() should be 'D'" << std::endl;
2673 throw std::runtime_error("pd_test_period_array_freq_validation failed: freqstr");
2674 }
2675
2676 std::cout << " -> tests passed" << std::endl;
2677 }
2678
2679 void pd_test_period_array_year_month_quarter() {
2680 std::cout << "========= PeriodArray: year/month/quarter components ======================= ";
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;