Skip to content

BUG: Timestamp.to_period() fails for freq="MS" #38914

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

Open
2 of 3 tasks
aiwalter opened this issue Jan 3, 2021 · 1 comment
Open
2 of 3 tasks

BUG: Timestamp.to_period() fails for freq="MS" #38914

aiwalter opened this issue Jan 3, 2021 · 1 comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Period Period data type

Comments

@aiwalter
Copy link

aiwalter commented Jan 3, 2021

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

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

idx = pd.date_range(start='1/1/2018', end='12/1/2018', freq="MS")
idx[-1].to_period()

Problem description

This results in the following exception:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
pandas\_libs\tslibs\period.pyx in pandas._libs.tslibs.period.freq_to_dtype_code()

AttributeError: 'pandas._libs.tslibs.offsets.MonthBegin' object has no attribute '_period_dtype_code'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-9-8bcd22fce921> in <module>
      1 idx = pd.date_range(start='1/1/2018', end='12/1/2018', freq="MS")
----> 2 idx[-1].to_period()

pandas\_libs\tslibs\timestamps.pyx in pandas._libs.tslibs.timestamps._Timestamp.to_period()

pandas\_libs\tslibs\period.pyx in pandas._libs.tslibs.period.Period.__new__()

pandas\_libs\tslibs\period.pyx in pandas._libs.tslibs.period.freq_to_dtype_code()

ValueError: Invalid frequency: {0}

Timestamp.to_period() does not work for freq="MS", other freq values might also not work?

Expected Output

Period('2018-01', 'M')

This can only be achieved by the following workaround:

idx = pd.date_range(start='1/1/2018 00:00:00', end='1/5/2018 00:01:00', freq="MS")
cutoff = idx[-1]
date = pd.DatetimeIndex([cutoff], freq=cutoff.freq)
date.to_period()[0]
>>> Period('2018-01', 'M')

Output of pd.show_versions()

INSTALLED VERSIONS

commit : b5958ee
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : AMD64 Family 21 Model 112 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.1.5
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.2.4
setuptools : 50.3.1.post20201107
Cython : 0.29.21
pytest : 3.6.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.4
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.4
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.50.1

@aiwalter aiwalter added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 3, 2021
@MarcoGorelli MarcoGorelli added Error Reporting Incorrect or improved errors from pandas and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 3, 2021
@MarcoGorelli
Copy link
Member

AttributeError: 'pandas._libs.tslibs.offsets.MonthBegin' object has no attribute '_period_dtype_code'

That should be caught and re-raised with a more helpful exception+message. PR would be wlecome.

xref #38885 (comment)

Pawel-Kranzberg added a commit to Pawel-Kranzberg/pandas that referenced this issue Feb 18, 2021
Add ability to use `MonthBegin`, `BMonthBegin` and `BMonthEnd` as periods, e.g., by using their offset aliases as `freq` in `date_range`.
It would address pandas-dev#38914.
@mroeschke mroeschke added Enhancement Period Period data type labels Aug 14, 2021
@jbrockmendel jbrockmendel mentioned this issue May 29, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants