BUG: incorrect output of first('1M') in case if first index is the last day of the month · Issue #29623 · pandas-dev/pandas (original) (raw)

Code Sample

x = pd.Series(1, index=pd.bdate_range('2010-03-31', periods=100)) print(x.first('1M')) # Returns March and April print(x.first('2M')) # Returns March, April and May

etc...

Problem description

In case if the first index of the series falls on the last day of the month, Series.first('1M') returns two first months (this one day + the next month).

If the first value is not on the last day of the month - result is correct:

x = pd.Series(1, index=pd.bdate_range('2010-03-30', periods=100)) print(x.first('1M')) # Returns only March print(x.first('2M')) # Returns March and April

etc...

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.1.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.17.2
pytz : 2019.3
dateutil : 2.8.0
pip : 19.3.1
setuptools : 41.4.0
Cython : None
pytest : 5.2.1
hypothesis : None
sphinx : 2.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : None
pymysql : 0.9.3
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : 4.6.3
bottleneck : None
fastparquet : 0.1.6
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.0.1
numexpr : 2.7.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.12.1
pytables : None
s3fs : 0.3.4
scipy : 1.3.1
sqlalchemy : 1.3.8
tables : 3.4.4
xarray : None
xlrd : 1.1.0
xlwt : None
xlsxwriter : None