Skip to content
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

TST: add tests for DatetimeIndex.is_year_start/is_quarter_start on "BMS" frequency #58691

Conversation

natmokval
Copy link
Contributor

@natmokval natmokval commented May 12, 2024

@simonjayhawkins
Copy link
Member

Thanks @natmokval for the PR.

Should the OP be an issue instead.? or is there already an open issue for this?

@natmokval
Copy link
Contributor Author

natmokval commented May 15, 2024

Should the OP be an issue instead.? or is there already an open issue for this?

thanks @simonjayhawkins for the comment. No, there is no open issue for this.
It's my mistake; I should have opened an issue first. Now I've opened the issue for this PR.

@natmokval natmokval marked this pull request as ready for review June 4, 2024 09:41
@natmokval natmokval added Bug Frequency DateOffsets labels Jun 4, 2024
@natmokval natmokval changed the title BUG: DatetimeIndex is_year_start breaks on freq BusinessMonthStart BUG: DatetimeIndex is_year_start breaks on BusinessMonthBegin frequency Jun 4, 2024
@natmokval
Copy link
Contributor Author

Fixed bug in DatetimeIndex.is_year_start and DatetimeIndex.is_quarter_start returning False instead of True when using BusinessMonthBegin frequency. @MarcoGorelli, could you please take a look at this PR?

@MarcoGorelli
Copy link
Member

thanks for looking into this

not sure this fixes the issue to be honest:

In [2]: pd.DatetimeIndex(['1976-11-01', '1976-12-01', '1977-01-03'], freq='BMS')
Out[2]: DatetimeIndex(['1976-11-01', '1976-12-01', '1977-01-03'], dtype='datetime64[s]', freq='BMS')

In [3]: arr = pd.DatetimeIndex(['1976-11-01', '1976-12-01', '1977-01-03'], freq='BMS')

In [4]: arr.is_year_start
Out[4]: array([False, False,  True])

In [5]: [x.is_year_start for x in arr]
Out[5]: [False, False, False]

We'd expect Out[4] and Out[5] to be the same, right?

Here's a little hypothesis test I put together:

import pytest
import hypothesis.strategies as st
from hypothesis import given
import pandas as pd
from datetime import datetime


@pytest.mark.parametrize('freq', [
    'MS',
    'BMS',
])
@given(dt = st.datetimes(min_value=datetime(1960, 1, 1), max_value=datetime(1980, 1, 1)))
def test_me(freq, dt):
    d = pd.date_range(dt, periods=3, freq=freq)
    result = [x for x in d.is_year_start]
    expected = [x.is_year_start for x in d]
    assert result == expected

Granted, it looks like this is already super-broken on main anyway, but I think this requires a more general and a very careful and focused fix

@MarcoGorelli
Copy link
Member

Sorry, as an addendum to my previous comment: Timestamp doesn't have freq, so the two results can't be expected to be the same

If I amend the hypothesis test to be:

@given(
    dt = st.datetimes(min_value=datetime(1960, 1, 1), max_value=datetime(1980, 1, 1)),
    n = st.integers(min_value=1, max_value=10),
    freq = st.sampled_from(['BMS'])
)
def test_me(freq, dt, n):
    freq = f'{n}{freq}'
    d = pd.date_range(dt, periods=3, freq=freq)
    result = [x for x in d.is_year_start]
    expected = []
    for x in d:
        if x.is_year_start:
            expected.append(True)
        else:
            if x.day_of_week == 0 and (x - pd.Timedelta(days=1)).is_year_start:
                expected.append(True)
            elif x.day_of_week == 0 and (x - pd.Timedelta(days=2)).is_year_start:
                expected.append(True)
            else:
                expected.append(False)
    assert result == expected

then it does indeed pass

I think we should get #57494 in first though, as I think that that also fixes the issues "for free" (but then we keep the test from this one)

@natmokval
Copy link
Contributor Author

I think we should get #57494 in first though, as I think that that also fixes the issues "for free" (but then we keep the test from this one)

thanks, I will update this PR after we get the #57494 in.

@natmokval natmokval added Testing pandas testing functions or related to the test suite and removed Bug labels Jun 9, 2024
@natmokval natmokval changed the title BUG: DatetimeIndex is_year_start breaks on BusinessMonthBegin frequency TST: add tests for DatetimeIndex.is_year_start/is_quarter_start on "BMS" frequency Jun 9, 2024
@natmokval
Copy link
Contributor Author

@MarcoGorelli, I updated this PR, could you please take a look at my changes?

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

thanks @natmokval !

@MarcoGorelli MarcoGorelli merged commit a787f45 into pandas-dev:main Jun 9, 2024
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frequency DateOffsets Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: DatetimeIndex.is_year_start breaks on BusinessMonthBegin frequency
3 participants