Skip to content

BUG: incorrect output of first('1M') in case if first index is the last day of the month #29623

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

Closed
vfilimonov opened this issue Nov 14, 2019 · 4 comments · Fixed by #38331 or #38446
Closed
Assignees
Labels
Bug Datetime Datetime data dtype Frequency DateOffsets
Milestone

Comments

@vfilimonov
Copy link
Contributor

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

@rachel-frenkel
Copy link

take

@datapythonista
Copy link
Member

@jbrockmendel mind taking a look? That looks like a bug to me too, but may be we're missing something.

@rachel-frenkel let us know if you need help.

@jbrockmendel
Copy link
Member

It looks like in first before setting end_date = end = self.index[0] + offset there needs to be a check for offset.onOffset(index[0]), in which case we should return self.loc[:index[0]]

@simonjayhawkins
Copy link
Member

#38331 reverted #38448

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment