Skip to content

ENH: Implement '_period_dtype_code' attribute to 'pandas._libs.tslibs.offsets.MonthBegin' #38859

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
yohplala opened this issue Dec 31, 2020 · 13 comments · Fixed by #53879
Closed
Assignees

Comments

@yohplala
Copy link

yohplala commented Dec 31, 2020

Code...

index_offsets = pd.tseries.frequencies.to_offset('1MS')
time_marker = pd.Timestamp('2020/01/02 1:59')
period = pd.Period(time_marker, freq = index_offsets.freqstr)

... raises following error message:

Traceback (most recent call last):

  File "pandas/_libs/tslibs/period.pyx", line 2436, 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:

Traceback (most recent call last):

  File "<ipython-input-63-c78e6c74b47c>", line 1, in <module>
    period = pd.Period(time_marker, freq = index_offsets.freqstr)

  File "pandas/_libs/tslibs/period.pyx", line 2422, in pandas._libs.tslibs.period.Period.__new__

  File "pandas/_libs/tslibs/period.pyx", line 2438, in pandas._libs.tslibs.period.freq_to_dtype_code

ValueError: Invalid frequency: {0}

Please, could an appropriate '_period_dtype_code' attribute be provided to 'pandas._libs.tslibs.offsets.MonthBegin' object to solve this error?
Thanks for your amazing lib!

INSTALLED VERSIONS
------------------
commit           : db08276bc116c438d3fdee492026f8223584c477
python           : 3.8.5.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.4.0-58-generic
Version          : #64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : fr_FR.UTF-8
LOCALE           : fr_FR.UTF-8

pandas           : 1.1.3
numpy            : 1.19.2
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 20.2.4
setuptools       : 50.3.1.post20201107
Cython           : 0.29.21
pytest           : 6.1.1
hypothesis       : None
sphinx           : 3.2.1
blosc            : None
feather          : None
xlsxwriter       : 1.3.7
lxml.etree       : 4.6.1
html5lib         : 1.1
pymysql          : None
psycopg2         : None
jinja2           : 2.11.2
IPython          : 7.19.0
pandas_datareader: None
bs4              : 4.9.3
bottleneck       : 1.3.2
fsspec           : 0.8.3
fastparquet      : 0.4.2
gcsfs            : None
matplotlib       : 3.3.2
numexpr          : 2.7.1
odfpy            : None
openpyxl         : 3.0.5
pandas_gbq       : None
pyarrow          : None
pytables         : None
pyxlsb           : None
s3fs             : None
scipy            : 1.5.2
sqlalchemy       : 1.3.20
tables           : 3.6.1
tabulate         : None
xarray           : None
xlrd             : 1.2.0
xlwt             : 1.3.0
numba            : 0.51.2

Not building from sources (using anaconda)

@yohplala yohplala added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 31, 2020
@jreback
Copy link
Contributor

jreback commented Dec 31, 2020

you need to show versions and if you are building from source

@yohplala
Copy link
Author

you need to show versions and if you are building from source

Hi Jeff, sorry, information added.

@jbrockmendel
Copy link
Member

There is no Period corresponding to MonthBegin. Monthly Periods use offsets.MonthEnd

@yohplala
Copy link
Author

yohplala commented Jan 1, 2021

There is no Period corresponding to MonthBegin. Monthly Periods use offsets.MonthEnd

Hi, I have tried to check in the source, and I am not finding any MonthBegin in pandas/_libs/tslibs/period.pyxas stated by the error message, so my answer is maybe not appropriate.

Your answer seems to relate to the 2nd part of the error message:
File "pandas/_libs/tslibs/period.pyx", line 2438, in pandas._libs.tslibs.period.freq_to_dtype_code

But what happens if the direct root cause is solved?
AttributeError: 'pandas._libs.tslibs.offsets.MonthBegin' object has no attribute '_period_dtype_code'

Why not adding a Month '_period_dtype_code` to 'pandas._libs.tslibs.offsets.MonthBegin' object?
What does prevent it?

@jbrockmendel
Copy link
Member

There is not a one-to-one mapping between DateOffset subclasses and Periods. There are many DateOffsets for which there is no corresponding Period. MonthBegin is one of them.

@yohplala
Copy link
Author

yohplala commented Jan 1, 2021

There is not a one-to-one mapping between DateOffset subclasses and Periods. There are many DateOffsets for which there is no corresponding Period. MonthBegin is one of them.

Yes I understand that, and I can understand that a Period can only point toward single DateOffset.
But my question is
'What is the a trouble if several DateOffsets point toward the same Period?'

@jbrockmendel jbrockmendel added Frequency DateOffsets and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
@mroeschke mroeschke added the Needs Discussion Requires discussion from core team before further action label Aug 14, 2021
@jbrockmendel jbrockmendel mentioned this issue May 29, 2023
3 tasks
@jbrockmendel
Copy link
Member

@MarcoGorelli has the recent work in documenting what offsets are valid Periods address this?

@MarcoGorelli
Copy link
Member

Almost

All that needs doing to close this issue IMO is to, in

One of pandas period strings or corresponding objects. Accepted
strings are listed in the
:ref:`offset alias section <timeseries.offset_aliases>` in the user docs.

to replace "offset alias" with "period alias", and to change the link to timeseries.period_aliases

Anyone fancy making a PR?

@MarcoGorelli MarcoGorelli added Docs good first issue and removed Enhancement Needs Discussion Requires discussion from core team before further action labels Jun 25, 2023
@nandsha
Copy link

nandsha commented Jun 26, 2023

Hi @MarcoGorelli ,

Thanks for the opportunity!
I would like to (start) contribute, and after going through the details, I find this one seems to be a good fit for me!
I will try to get back on this with an update in about 2-3 weeks' time, and if that sounds fair, could you please assign this one to me?

Cheers,
Nandha

@MarcoGorelli
Copy link
Member

all that's needed is a 2-word change, it shouldn't take 2-3 weeks' time

if anyone wants to make a PR, feel free to do so

@rsm-23
Copy link
Contributor

rsm-23 commented Jun 27, 2023

Can I take it @MarcoGorelli ? Or should I wait for @nandsha to comeback?

@MarcoGorelli
Copy link
Member

go ahead, thanks @rsm-23

@rsm-23
Copy link
Contributor

rsm-23 commented Jun 27, 2023

take

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

Successfully merging a pull request may close this issue.

7 participants