Skip to content

BUG: DatetimeIndex.is_year_start returns incorrect array if .freq == 'MS' #49606

Closed
@rwijtvliet

Description

@rwijtvliet

Pandas version checks

  • I have checked that this issue has not already been reported.

  • 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

i = pd.date_range('2020-01-01', freq='MS', periods=12)
# DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
#                '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01',
#                '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01'],
#               dtype='datetime64[ns]', freq='MS')

# Incorrect result:
i.is_year_start 
# array([False, False, False, False, False, False, False, False, False, False, False, True])

# Correct result when first removing the frequency:
i.freq = None
i.is_year_start
# array([True, False, False, False, False, False, False, False, False, False, False, False])

Issue Description

The .freq property of the DatetimeIndex influences the result of the .is_year_start property. If it is set to MonthStart, the december timestamp is incorrectly recognised as the start of the year.

.is_quarter_start has a similar issue.

The issue does not arise for .freq == 'QS'.

Expected Behavior

Even if i.freq == 'MS', the result of i.is_year_start should start with True, not end with True.

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : de_DE.cp1252

pandas : 1.5.1
numpy : 1.23.4
pytz : 2022.6
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.2.2
Cython : None
pytest : 7.1.3
hypothesis : None
sphinx : 5.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.5.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeIndexRelated to the Index class or subclasses

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions