Skip to content

DatetimeIndex construction with periods and closed #23176

@Adirio

Description

@Adirio

Code Sample, a copy-pastable example if possible

import pandas as pd

dti1 = pd.DatetimeIndex(start="2018-10-16", periods=12, freq="H", closed="right")
print(dti1)

dti2 = pd.DatetimeIndex(end="2018-10-16", periods=12, freq="H", closed="left")
print(dti2)

Outputs:

DatetimeIndex(['2018-10-16 01:00:00', '2018-10-16 02:00:00',
               '2018-10-16 03:00:00', '2018-10-16 04:00:00',
               '2018-10-16 05:00:00', '2018-10-16 06:00:00',
               '2018-10-16 07:00:00', '2018-10-16 08:00:00',
               '2018-10-16 09:00:00', '2018-10-16 10:00:00',
               '2018-10-16 11:00:00'],
              dtype='datetime64[ns]', freq='H')
DatetimeIndex(['2018-10-15 13:00:00', '2018-10-15 14:00:00',
               '2018-10-15 15:00:00', '2018-10-15 16:00:00',
               '2018-10-15 17:00:00', '2018-10-15 18:00:00',
               '2018-10-15 19:00:00', '2018-10-15 20:00:00',
               '2018-10-15 21:00:00', '2018-10-15 22:00:00',
               '2018-10-15 23:00:00'],
              dtype='datetime64[ns]', freq='H')

Problem description

When explicitly asking for a number of periods, an index of that size is expected. By using closed with the matching boundary specified (start for closed="right" and end for closed="left") we get one less than the requested periods.

Probably this also happens with other functions/methods that use the closed argument and specify the number of items desired.

Expected Output

DatetimeIndex(['2018-10-16 01:00:00', '2018-10-16 02:00:00',
               '2018-10-16 03:00:00', '2018-10-16 04:00:00',
               '2018-10-16 05:00:00', '2018-10-16 06:00:00',
               '2018-10-16 07:00:00', '2018-10-16 08:00:00',
               '2018-10-16 09:00:00', '2018-10-16 10:00:00',
               '2018-10-16 11:00:00', '2018-10-16 12:00:00'],
              dtype='datetime64[ns]', freq='H')
DatetimeIndex(['2018-10-15 12:00:00', '2018-10-15 13:00:00',
               '2018-10-15 14:00:00', '2018-10-15 15:00:00',
               '2018-10-15 16:00:00', '2018-10-15 17:00:00',
               '2018-10-15 18:00:00', '2018-10-15 19:00:00',
               '2018-10-15 20:00:00', '2018-10-15 21:00:00',
               '2018-10-15 22:00:00', '2018-10-15 23:00:00'],
              dtype='datetime64[ns]', freq='H')

Output of pd.show_versions() (short version)

python: 3.6.4.final.0

pandas: 0.23.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions