Skip to content

Period does not round down for frequencies less that 1 hour #17666

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
makistsantekidis opened this issue Sep 25, 2017 · 3 comments
Open

Period does not round down for frequencies less that 1 hour #17666

makistsantekidis opened this issue Sep 25, 2017 · 3 comments
Labels
Bug Period Period data type

Comments

@makistsantekidis
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd

pd.Period(pd.to_datetime('20170101 11:52:30'), freq='4H')
Out[1]: Period('2017-01-01 11:00', '4H')
pd.Period(pd.to_datetime('20170101 10:32'), '1H')
Out[2]: Period('2017-01-01 10:00', 'H')
pd.Period(pd.to_datetime('20170101 11:56'), '1H')
Out[3]: Period('2017-01-01 11:00', 'H')
# All three correctly rounding down the time. 

# Using a frequency less than 1H introduces this problem. 
pd.Period(pd.to_datetime('20170101 11:56'), '30min')
Out[4]: Period('2017-01-01 11:56', '30T')
# Notice the time here. instead of round 11:30 it is 11:56
pd.Period(pd.to_datetime('20170101 11:52'), '30min')
Out[5]: Period('2017-01-01 11:52', '30T')

Problem description

When using pd.Period with frequency over one hour, everything works as expected, but using freq=30min or similar, does not produce the expected results as the ones of freq=1H.

This issue looks similar to #17053 but not for the same timescale.

Expected Output

pd.Period(pd.to_datetime('20170101 11:56'), '30min')
Out[3]: Period('2017-01-01 11:30', '30T')
pd.Period(pd.to_datetime('20170101 11:52'), '30min')
Out[4]: Period('2017-01-01 11:30', '30T')

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.20.3
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.4.0
Cython: 0.26
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.2.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

@makistsantekidis
Copy link
Author

makistsantekidis commented Sep 25, 2017

One way to get the correct rounding is by using pandas.Timestamp.floor and pandas.Timestamp.ceil.

pd.to_datetime('20170101 11:52:30').floor('30min')
Out[22]: Timestamp('2017-01-01 11:30:00')

@TomAugspurger TomAugspurger added Bug Period Period data type labels Sep 25, 2017
@TomAugspurger TomAugspurger added this to the Next Major Release milestone Sep 25, 2017
@sinhrks
Copy link
Member

sinhrks commented Sep 26, 2017

Thx for the report. It is handled as "Minute" freq with 30 mult.

p = pd.Period(pd.to_datetime('20170101 11:52'), '30min')
p
# Period('2017-01-01 11:52', '30T')
p + 1
# Period('2017-01-01 12:22', '30T')

As the same, hourly freq like "2H" is not rounded to even hours.

pd.Period(pd.to_datetime('20170101 11:56'), '2H')
# Period('2017-01-01 11:00', '2H')

@Licht-T
Copy link
Contributor

Licht-T commented Oct 23, 2017

This is because mult is not treated when generating ordinal.
https://github.com/pandas-dev/pandas/blob/master/pandas/_libs/period.pyx#L1223

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Period Period data type
Projects
None yet
Development

No branches or pull requests

5 participants