Skip to content

BUG: DateTimeIndex.is_year_start unexpected behavior when constructed with freq 'MS' date_range (#57377) #57494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ Categorical

Datetimelike
^^^^^^^^^^^^
- Bug in :attr:`is_year_start` where a DateTimeIndex constructed via a date_range with frequency 'MS' wouldn't have the correct year or quarter start attributes (:issue:`57377`)
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56382`)
Expand Down
6 changes: 4 additions & 2 deletions pandas/_libs/tslibs/fields.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,12 @@ def get_start_end_field(
# month of year. Other offsets use month, startingMonth as ending
# month of year.

if (freqstr[0:2] in ["MS", "QS", "YS"]) or (
freqstr[1:3] in ["MS", "QS", "YS"]):
# According to the above comment, the first conditional is for QS and YS only
if (freqstr[0:2] in ["QS", "YS"]) or (
freqstr[1:3] in ["QS", "YS"]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic is already broken on main - it seems to assume that n is only a single digit

and so it produces this kind of nonsense:

In [39]: dr = pd.date_range("2017-01-01", periods=2, freq="7YS")

In [40]: dr
Out[40]: DatetimeIndex(['2017-01-01', '2024-01-01'], dtype='datetime64[ns]', freq='7YS-JAN')

In [41]: dr.is_year_start
Out[41]: array([ True,  True])

In [42]: dr = pd.date_range("2017-01-01", periods=2, freq="10YS")

In [43]: dr
Out[43]: DatetimeIndex(['2017-01-01', '2027-01-01'], dtype='datetime64[ns]', freq='10YS-JAN')

In [44]: dr.is_year_start
Out[44]: array([False, False])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, looks like this broken logic was introduced 13 years ago... 😭

https://github.com/pandas-dev/pandas/pull/4823/files

end_month = 12 if month_kw == 1 else month_kw - 1
start_month = month_kw

else:
end_month = month_kw
start_month = (end_month % 12) + 1
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def f(self):
month_kw = 12
if freq:
kwds = freq.kwds
month_kw = kwds.get("startingMonth", kwds.get("month", 12))
month_kw = kwds.get("startingMonth", kwds.get("month", month_kw))

result = fields.get_start_end_field(
values, field, self.freqstr, month_kw, reso=self._creso
Expand Down
78 changes: 78 additions & 0 deletions pandas/tests/indexes/datetimes/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,81 @@ def test_dti_is_month_start_custom(self):
msg = "Custom business days is not supported by is_month_start"
with pytest.raises(ValueError, match=msg):
dti.is_month_start

@pytest.mark.parametrize(
"timestamp, freq, periods, expected_values",
[
("2017-12-01", "MS", 3, np.array([False, True, False])),
("2017-12-01", "QS", 3, np.array([True, False, False])),
("2017-12-01", "YS", 3, np.array([True, True, True])),
],
)
def test_dti_dr_is_year_start(self, timestamp, freq, periods, expected_values):
# GH57377
result = date_range(timestamp, freq=freq, periods=periods).is_year_start
tm.assert_numpy_array_equal(result, expected_values)

@pytest.mark.parametrize(
"timestamp, freq, periods, expected_values",
[
("2017-12-01", "ME", 3, np.array([True, False, False])),
("2017-12-01", "QE", 3, np.array([True, False, False])),
("2017-12-01", "YE", 3, np.array([True, True, True])),
],
)
def test_dti_dr_is_year_end(self, timestamp, freq, periods, expected_values):
# GH57377
result = date_range(timestamp, freq=freq, periods=periods).is_year_end
tm.assert_numpy_array_equal(result, expected_values)

@pytest.mark.parametrize(
"timestamp, freq, periods, expected_values",
[
("2017-12-01", "MS", 3, np.array([False, True, False])),
("2017-12-01", "QS", 3, np.array([True, True, True])),
("2017-12-01", "YS", 3, np.array([True, True, True])),
],
)
def test_dti_dr_is_quarter_start(self, timestamp, freq, periods, expected_values):
# GH57377
result = date_range(timestamp, freq=freq, periods=periods).is_quarter_start
tm.assert_numpy_array_equal(result, expected_values)

@pytest.mark.parametrize(
"timestamp, freq, periods, expected_values",
[
("2017-12-01", "ME", 3, np.array([True, False, False])),
("2017-12-01", "QE", 3, np.array([True, True, True])),
("2017-12-01", "YE", 3, np.array([True, True, True])),
],
)
def test_dti_dr_is_quarter_end(self, timestamp, freq, periods, expected_values):
# GH57377
result = date_range(timestamp, freq=freq, periods=periods).is_quarter_end
tm.assert_numpy_array_equal(result, expected_values)

@pytest.mark.parametrize(
"timestamp, freq, periods, expected_values",
[
("2017-12-01", "MS", 3, np.array([True, True, True])),
("2017-12-01", "QS", 3, np.array([True, True, True])),
("2017-12-01", "YS", 3, np.array([True, True, True])),
],
)
def test_dti_dr_is_month_start(self, timestamp, freq, periods, expected_values):
# GH57377
result = date_range(timestamp, freq=freq, periods=periods).is_month_start
tm.assert_numpy_array_equal(result, expected_values)

@pytest.mark.parametrize(
"timestamp, freq, periods, expected_values",
[
("2017-12-01", "ME", 3, np.array([True, True, True])),
("2017-12-01", "QE", 3, np.array([True, True, True])),
("2017-12-01", "YE", 3, np.array([True, True, True])),
],
)
def test_dti_dr_is_month_end(self, timestamp, freq, periods, expected_values):
# GH57377
result = date_range(timestamp, freq=freq, periods=periods).is_month_end
tm.assert_numpy_array_equal(result, expected_values)
Loading