Skip to content

ENH: Support datetime.date in Period Index #10798

Open
@max-sixty

Description

@max-sixty

PeriodIndexs should resolve when datetime.date is passed into in, but doesn't. Example below.

Tracing this through, it looks like parse_time_string is expected to return a tuple in get_loc (which is called by __contains__): https://github.com/pydata/pandas/blob/master/pandas/tseries/period.py#L610

But if passed something that isn't a string, just returns it back as a single item: https://github.com/pydata/pandas/blob/master/pandas/tseries/tools.py#L447

...so in this instance the lookup raises an exception and mistakenly propagates up that the value can't be found.

What's the best way forward here? Should the return type always be the same? Or do we add a check in get_loc to stop it calling parse_time_string? Or broaden the except here to be any exception: https://github.com/pydata/pandas/blob/master/pandas/tseries/period.py#L612

My inclination is the first, but whether that will have some blast radius / what the standards are? The third seems like a reasonable cut through too.

In [9]:

period_index
Out[9]:
PeriodIndex(['2015-01-01', '2015-01-02', '2015-01-05', '2015-01-06',
             '2015-01-07', '2015-01-08', '2015-01-09', '2015-01-12',
             '2015-01-13', '2015-01-14', '2015-01-15', '2015-01-16',
             '2015-01-19', '2015-01-20', '2015-01-21', '2015-01-22',
             '2015-01-23', '2015-01-26', '2015-01-27', '2015-01-28',
             '2015-01-29', '2015-01-30'],
            dtype='int64', freq='B')
In [10]:

'2015-01-06' in period_index
Out[10]:
True
In [15]:

dt = datetime.datetime(2015,1,6) 
dt
Out[15]:
datetime.datetime(2015, 1, 6, 0, 0)
In [16]:

dt in period_index
Out[16]:
False
In [17]:

d = datetime.date(2015,1,6) 
d
Out[17]:
datetime.date(2015, 1, 6)
In [18]:

d in period_index
Out[18]:
False

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeEnhancementIndexingRelated to indexing on series/frames, not to indexes themselvesPeriodPeriod data typedatetime.datestdlib datetime.date support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions