|
27 | 27 | from pandas.errors import PerformanceWarning
|
28 | 28 |
|
29 | 29 | from pandas import (
|
| 30 | + DataFrame, |
30 | 31 | DatetimeIndex,
|
31 | 32 | Series,
|
32 | 33 | date_range,
|
@@ -1067,3 +1068,51 @@ def test_dateoffset_add_sub_timestamp_series_with_nano(offset, expected):
|
1067 | 1068 | assert testseries[0] == teststamp
|
1068 | 1069 | testseries = offset + testseries
|
1069 | 1070 | assert testseries[0] == expected
|
| 1071 | + |
| 1072 | + |
| 1073 | +@pytest.mark.parametrize( |
| 1074 | + "n_months, scaling_factor, start_timestamp, expected_timestamp", |
| 1075 | + [ |
| 1076 | + (1, 2, "2020-01-30", "2020-03-30"), |
| 1077 | + (2, 1, "2020-01-30", "2020-03-30"), |
| 1078 | + (1, 0, "2020-01-30", "2020-01-30"), |
| 1079 | + (2, 0, "2020-01-30", "2020-01-30"), |
| 1080 | + (1, -1, "2020-01-30", "2019-12-30"), |
| 1081 | + (2, -1, "2020-01-30", "2019-11-30"), |
| 1082 | + ], |
| 1083 | +) |
| 1084 | +def test_offset_multiplication( |
| 1085 | + n_months, scaling_factor, start_timestamp, expected_timestamp |
| 1086 | +): |
| 1087 | + # GH 47953 |
| 1088 | + mo1 = DateOffset(months=n_months) |
| 1089 | + |
| 1090 | + startscalar = Timestamp(start_timestamp) |
| 1091 | + startarray = Series([startscalar]) |
| 1092 | + |
| 1093 | + resultscalar = startscalar + (mo1 * scaling_factor) |
| 1094 | + resultarray = startarray + (mo1 * scaling_factor) |
| 1095 | + |
| 1096 | + expectedscalar = Timestamp(expected_timestamp) |
| 1097 | + expectedarray = Series([expectedscalar]) |
| 1098 | + assert resultscalar == expectedscalar |
| 1099 | + |
| 1100 | + tm.assert_series_equal(resultarray, expectedarray) |
| 1101 | + |
| 1102 | + |
| 1103 | +def test_dateoffset_operations_on_dataframes(): |
| 1104 | + # GH 47953 |
| 1105 | + df = DataFrame({"T": [Timestamp("2019-04-30")], "D": [DateOffset(months=1)]}) |
| 1106 | + frameresult1 = df["T"] + 26 * df["D"] |
| 1107 | + df2 = DataFrame( |
| 1108 | + { |
| 1109 | + "T": [Timestamp("2019-04-30"), Timestamp("2019-04-30")], |
| 1110 | + "D": [DateOffset(months=1), DateOffset(months=1)], |
| 1111 | + } |
| 1112 | + ) |
| 1113 | + expecteddate = Timestamp("2021-06-30") |
| 1114 | + with tm.assert_produces_warning(PerformanceWarning): |
| 1115 | + frameresult2 = df2["T"] + 26 * df2["D"] |
| 1116 | + |
| 1117 | + assert frameresult1[0] == expecteddate |
| 1118 | + assert frameresult2[0] == expecteddate |
0 commit comments