Skip to content

Commit 56fd4e3

Browse files
chris-b1jreback
authored andcommitted
BUG/API: QuarterBegin n=0 #11406
1 parent eec0bc6 commit 56fd4e3

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

doc/source/whatsnew/v0.18.0.txt

+28
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,40 @@ In addition, ``.round()`` will be available thru the ``.dt`` accessor of ``Serie
8989
Backwards incompatible API changes
9090
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9191

92+
Bug in QuarterBegin with n=0
93+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9294

95+
In previous versions, the behavior of the QuarterBegin offset was inconsistent
96+
depending on the date when the ``n`` parameter was 0. (:issue:`11406`)
9397

98+
The general semantics of anchored offsets for ``n=0`` is to not move the date
99+
when it is an anchor point (e.g., a quarter start date), and otherwise roll
100+
forward to the next anchor point.
94101

102+
.. ipython:: python
103+
104+
d = pd.Timestamp('2014-02-01')
105+
d
106+
d + pd.offsets.QuarterBegin(n=0, startingMonth=2)
107+
d + pd.offsets.QuarterBegin(n=0, startingMonth=1)
108+
109+
For the ``QuarterBegin`` offset in previous versions, the date would be rolled
110+
*backwards* if date was in the same month as the quarter start date.
111+
112+
.. code-block:: python
95113

114+
In [3]: d = pd.Timestamp('2014-02-15')
96115

116+
In [4]: d + pd.offsets.QuarterBegin(n=0, startingMonth=2)
117+
Out[4]: Timestamp('2014-02-01 00:00:00')
118+
119+
This behavior has been corrected in version 0.18.0, which is consistent with
120+
other anchored offsets like ``MonthBegin`` and ``YearBegin``.
121+
122+
.. ipython:: python
97123

124+
d = pd.Timestamp('2014-02-15')
125+
d + pd.offsets.QuarterBegin(n=0, startingMonth=2)
98126

99127

100128
Other API Changes

pandas/tseries/offsets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ def apply(self, other):
17671767
# make sure you roll forward, so negate
17681768
monthsSince = monthsSince - 3
17691769

1770-
if n < 0 and (monthsSince == 0 and other.day > 1):
1770+
if n <= 0 and (monthsSince == 0 and other.day > 1):
17711771
# after start, so come back an extra period as if rolled forward
17721772
n = n + 1
17731773

pandas/tseries/tests/test_offsets.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2918,8 +2918,8 @@ def test_offset(self):
29182918
datetime(2008, 2, 29): datetime(2008, 4, 1),
29192919
datetime(2008, 3, 15): datetime(2008, 4, 1),
29202920
datetime(2008, 3, 31): datetime(2008, 4, 1),
2921-
datetime(2008, 4, 15): datetime(2008, 4, 1),
2922-
datetime(2008, 4, 30): datetime(2008, 4, 1), }))
2921+
datetime(2008, 4, 15): datetime(2008, 7, 1),
2922+
datetime(2008, 4, 30): datetime(2008, 7, 1), }))
29232923

29242924
tests.append((QuarterBegin(startingMonth=1, n=-1),
29252925
{datetime(2008, 1, 1): datetime(2007, 10, 1),

0 commit comments

Comments
 (0)