Skip to content
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

API: Period and PeriodIndex do not support some offset aliases #13871

Open
agraboso opened this issue Aug 1, 2016 · 7 comments
Open

API: Period and PeriodIndex do not support some offset aliases #13871

agraboso opened this issue Aug 1, 2016 · 7 comments
Assignees
Labels
API - Consistency Internal Consistency of API/Behavior Frequency DateOffsets Needs Discussion Requires discussion from core team before further action Period Period data type

Comments

@agraboso
Copy link
Contributor

agraboso commented Aug 1, 2016

Period and PeriodIndex raise ValueError when freq is any of the following strings:

'BM', 'CBM', 'MS', 'BMS', 'CBMS', 'BQ', 'QS', 'BQS', 'BA', 'AS', 'BAS', 'BH'

date_range is fine in all cases. Also confirmed on master.

Possible related to #4878.

Code Sample, a copy-pastable example if possible

import pandas as pd


FREQS = ['B', 'C', 'D', 'W', 'M', 'BM', 'CBM', 'MS', 'BMS', 'CBMS', 'Q',
         'BQ', 'QS', 'BQS', 'A', 'BA', 'AS', 'BAS', 'BH', 'H', 'T', 'min',
         'S', 'L', 'U', 'N']


for freq in FREQS:
    try:
        pd.Period('2016-01-01', freq=freq)
    except ValueError:
        print('Cannot create Period with freq={}'.format(freq))

    p = pd.Period('2016-01-01', freq='N')
    try:
        p.asfreq(freq)
    except ValueError:
        print('Cannot modify Period to freq={}'.format(freq))

    try:
        pd.PeriodIndex(start='2016-01-01', periods=10, freq=freq)
    except ValueError:
        print('Cannot create PeriodIndex with freq={}'.format(freq))

    pi = pd.PeriodIndex(start='2016-01-01', periods=10, freq='N')
    try:
        pi.asfreq(freq)
    except ValueError:
        print('Cannot modify PeriodIndex to freq={}'.format(freq))

    try:
        pd.date_range(start='2016-01-01', periods=10, freq=freq)
    except ValueError:
        print('Cannot create date_range with freq={}'.format(freq))

Output

Cannot create Period with freq=BM
Cannot modify Period to freq=BM
Cannot create PeriodIndex with freq=BM
Cannot modify PeriodIndex to freq=BM
Cannot create Period with freq=CBM
Cannot modify Period to freq=CBM
Cannot create PeriodIndex with freq=CBM
Cannot modify PeriodIndex to freq=CBM
Cannot create Period with freq=MS
Cannot modify Period to freq=MS
Cannot create PeriodIndex with freq=MS
Cannot modify PeriodIndex to freq=MS
Cannot create Period with freq=BMS
Cannot modify Period to freq=BMS
Cannot create PeriodIndex with freq=BMS
Cannot modify PeriodIndex to freq=BMS
Cannot create Period with freq=CBMS
Cannot modify Period to freq=CBMS
Cannot create PeriodIndex with freq=CBMS
Cannot modify PeriodIndex to freq=CBMS
Cannot create Period with freq=BQ
Cannot modify Period to freq=BQ
Cannot create PeriodIndex with freq=BQ
Cannot modify PeriodIndex to freq=BQ
Cannot create Period with freq=QS
Cannot modify Period to freq=QS
Cannot create PeriodIndex with freq=QS
Cannot modify PeriodIndex to freq=QS
Cannot create Period with freq=BQS
Cannot modify Period to freq=BQS
Cannot create PeriodIndex with freq=BQS
Cannot modify PeriodIndex to freq=BQS
Cannot create Period with freq=BA
Cannot modify Period to freq=BA
Cannot create PeriodIndex with freq=BA
Cannot modify PeriodIndex to freq=BA
Cannot create Period with freq=AS
Cannot modify Period to freq=AS
Cannot create PeriodIndex with freq=AS
Cannot modify PeriodIndex to freq=AS
Cannot create Period with freq=BAS
Cannot modify Period to freq=BAS
Cannot create PeriodIndex with freq=BAS
Cannot modify PeriodIndex to freq=BAS
Cannot create Period with freq=BH
Cannot modify Period to freq=BH
Cannot create PeriodIndex with freq=BH
Cannot modify PeriodIndex to freq=BH

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Darwin
OS-release: 15.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.24
numpy: 1.11.0
scipy: 0.17.1
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: None
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: None
tables: None
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None
None
@jreback
Copy link
Contributor

jreback commented Aug 1, 2016

@agraboso

with the exception of BH I don't think any of those make sense. They are representing a point-in-time, e.g. a month-START, rather than a Period which is a timespan.

@sinhrks

@jreback jreback added the Frequency DateOffsets label Aug 1, 2016
@jreback jreback changed the title Period and PeriodIndex do not support some offset aliases Period and PeriodIndex do not support some offset aliases Aug 1, 2016
@jreback
Copy link
Contributor

jreback commented Aug 1, 2016

actually, let's repurpose this issue to provide a more informative error for why these are not supported. E.g. they are valid point-in-time aliases (and not just a random string), so we can check for and report better.

@jreback jreback added Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels Aug 1, 2016
@jreback jreback added this to the Next Major Release milestone Aug 1, 2016
@jreback jreback changed the title Period and PeriodIndex do not support some offset aliases ERR: more informative error messages on invalid freq for Period when they are point-in-time Aug 1, 2016
@sinhrks
Copy link
Member

sinhrks commented Aug 1, 2016

Better error report is one thing, or it may be useful to support it (for DatetimeIndex compat).

I think monthly freq like BusinessMonthEnd can use the same internal repr as MonthEnd, and needs small changes in some impl (like to_timestamp).

@EricLeer
Copy link
Contributor

EricLeer commented May 7, 2021

take

@simonjayhawkins simonjayhawkins added Period Period data type API - Consistency Internal Consistency of API/Behavior Needs Discussion Requires discussion from core team before further action and removed Enhancement Error Reporting Incorrect or improved errors from pandas good first issue labels Jun 5, 2022
@simonjayhawkins simonjayhawkins changed the title ERR: more informative error messages on invalid freq for Period when they are point-in-time API: Period and PeriodIndex do not support some offset aliases Jun 5, 2022
@simonjayhawkins
Copy link
Member

@jreback I'm changing the title back to something like the original because of subsequent comment #13871 (comment)

@simonjayhawkins
Copy link
Member

and closing #4878 as duplicate

@simonjayhawkins
Copy link
Member

d = pd.date_range(start="2016-01-01", periods=10, freq="BM")
print(d)
print(d.to_period())
pd.period_range(start="2016-01-01", periods=10, freq="BM")
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
               '2016-05-31', '2016-06-30', '2016-07-29', '2016-08-31',
               '2016-09-30', '2016-10-31'],
              dtype='datetime64[ns]', freq='BM')
PeriodIndex(['2016-01', '2016-02', '2016-03', '2016-04', '2016-05', '2016-06',
             '2016-07', '2016-08', '2016-09', '2016-10'],
            dtype='period[M]')
---------------------------------------------------------------------------
...
ValueError: Invalid frequency: <BusinessMonthEnd>

Better error report is one thing, or it may be useful to support it (for DatetimeIndex compat).

I think monthly freq like BusinessMonthEnd can use the same internal repr as MonthEnd, and needs small changes in some impl (like to_timestamp).

Since to_period() works for a DatetimeIndex with freq='BM', it may make sense to use the same logic used to convert these and accept them in the period_range, Period, PeriodIndex constructors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Frequency DateOffsets Needs Discussion Requires discussion from core team before further action Period Period data type
Projects
None yet
Development

No branches or pull requests

8 participants