pandas
pandas copied to clipboard
BUG: Inconsistent "is_year_start" from DatetimeIndex with freq "MS"
Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
dr = pd.date_range("2017-12-01", periods=2, freq="MS")
dr_comp = [dt.is_year_start for dt in dr]
dr_attr = dr.is_year_start
assert dr[1].is_year_start
assert dr_comp[1]
assert dr_attr[0] # Should raise error but doesn't
assert dr_attr[1] # Raises error but shouldn't
Issue Description
When working with DatetimeIndex with frequency "MS" the attribute "is_year_start" does not obtain the same attribute if you obtain the attribute directly from the DatatimeIndex instead of obtaining directly from each individual value of the DatetimeIndex.
Expected Behavior
I would expect for the same array of attributes independently of if it is obtained from a comprehensive list or as an attibute as shown in the documentation.
https://pandas.pydata.org/docs/reference/api/pandas.DatetimeIndex.is_year_start.html
import pandas as pd
dr = pd.date_range("2017-12-01", periods=2, freq="MS")
dr_comp = [dt.is_year_start for dt in dr]
dr_attr = dr.is_year_start
assert not dr[1].is_year_start
assert dr[1].is_year_start
assert not dr_comp[1]
assert dr_comp[1]
assert not dr_attr[0]
assert dr_attr[1]
Installed Versions
INSTALLED VERSIONS
commit : f538741432edf55c6b9fb5d0d496d2dd1d7c2457 python : 3.11.6.final.0 python-bits : 64 OS : Linux OS-release : 3.10.0-1160.95.1.el7.x86_64 Version : #1 SMP Fri Jun 23 08:44:55 EDT 2023 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : en_US.UTF-8 LANG : en_US.UTF-8 LOCALE : en_US.UTF-8
pandas : 2.2.0 numpy : 1.26.4 pytz : 2024.1 dateutil : 2.8.2 setuptools : 68.2.2 pip : 23.2.1 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : 8.16.1 pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : 3.1.2 pandas_gbq : None pyarrow : None pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : None sqlalchemy : 2.0.21 tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.4 qtpy : None pyqt5 : None
Thanks for the report. Confirmed on main, further investigations and PRs to fix are welcome!
take
Upon further inspection, it looks like is_quarter_start with the freq 'MS' is having the same issue
idx = pd.date_range('2017-01-01', periods=6, freq='MS')
idx[idx.is_quarter_start]
This gives the dates 2017-03-01 and 2017-06-01, which are of course not the actual start of quarters
looks like this was already reported earlier: https://github.com/pandas-dev/pandas/issues/49606