Skip to content

DatetimeIndex.to_period does not support multiple of frequencies #14070

Open
@sdementen

Description

@sdementen

The code below is the simplest way I found to "snap" a DateTimeIndex to some "frequency" (is there any better way?).
I would have expected the to_period method to work as flexibly as the resample method that accepts any rule (like 15T, 2H, ...) but the to_period method does not appear to handle properly multiples of frequencies (and neither raises an exception).

Code Sample, a copy-pastable example if possible

import pandas
import datetime
dt=pandas.date_range('2016-01-01 00:00','2016-01-01 05:00',freq='15T',closed="left")
print(dt.to_period('H').start_time) # snap to the hour : OK
print(dt.to_period('2H').start_time) # snap to even hours : not OK

Actual Output

DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 01:00:00', '2016-01-01 01:00:00',
               '2016-01-01 01:00:00', '2016-01-01 01:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 03:00:00', '2016-01-01 03:00:00',
               '2016-01-01 03:00:00', '2016-01-01 03:00:00',
               '2016-01-01 04:00:00', '2016-01-01 04:00:00',
               '2016-01-01 04:00:00', '2016-01-01 04:00:00'],
              dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 01:00:00', '2016-01-01 01:00:00',
               '2016-01-01 01:00:00', '2016-01-01 01:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 03:00:00', '2016-01-01 03:00:00',
               '2016-01-01 03:00:00', '2016-01-01 03:00:00',
               '2016-01-01 04:00:00', '2016-01-01 04:00:00',
               '2016-01-01 04:00:00', '2016-01-01 04:00:00'],
              dtype='datetime64[ns]', freq=None)

Expected Output

DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 01:00:00', '2016-01-01 01:00:00',
               '2016-01-01 01:00:00', '2016-01-01 01:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 03:00:00', '2016-01-01 03:00:00',
               '2016-01-01 03:00:00', '2016-01-01 03:00:00',
               '2016-01-01 04:00:00', '2016-01-01 04:00:00',
               '2016-01-01 04:00:00', '2016-01-01 04:00:00'],
              dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 00:00:00', '2016-01-01 00:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 02:00:00', '2016-01-01 02:00:00',
               '2016-01-01 04:00:00', '2016-01-01 04:00:00',
               '2016-01-01 04:00:00', '2016-01-01 04:00:00'],
              dtype='datetime64[ns]', freq=None)

output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 32
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.18.1
nose: None
pip: 8.1.2
setuptools: 23.0.0
Cython: None
numpy: 1.11.0
scipy: 0.17.1
statsmodels: None
xarray: None
IPython: 4.2.0
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: None
tables: None
numexpr: 2.6.1
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: None
xlsxwriter: 0.9.2
lxml: 3.6.0
bs4: None
html5lib: None
httplib2: 0.9.2
apiclient: None
sqlalchemy: 1.0.12
pymysql: None
psycopg2: 2.6.2 (dt dec pq3 ext)
jinja2: 2.8
boto: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypePeriodPeriod data type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions