Skip to content

ENH: Support datetime.date in Period Index #10798

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
max-sixty opened this issue Aug 11, 2015 · 3 comments
Open

ENH: Support datetime.date in Period Index #10798

max-sixty opened this issue Aug 11, 2015 · 3 comments
Labels
datetime.date stdlib datetime.date support Datetime Datetime data dtype Enhancement Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type

Comments

@max-sixty
Copy link
Contributor

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
@sinhrks
Copy link
Member

sinhrks commented Aug 11, 2015

I agree we should accept datetime-likes as the same as other comparison ops.

I don't think parse_time_string in get_loc is a problem. It raises TypeError when the returned value is split to tuple, then catched and directly converted to Period. So, removing string_types condition in __contains__ should work?

idx = pd.period_range('2011-01-01', freq='D', periods=10)
idx.get_loc(datetime.date(2011, 1, 3))
# 2

pd.Period(datetime.date(2011, 1, 3), freq='D') in idx
# True

PR is welcome:)

@sinhrks sinhrks added the Period Period data type label Aug 11, 2015
@max-sixty
Copy link
Contributor Author

@sinhrks perfect, thanks!

@jreback jreback added this to the 0.17.0 milestone Aug 13, 2015
@jreback jreback modified the milestones: Next Major Release, 0.17.0 Aug 26, 2015
@jreback jreback added Datetime Datetime data dtype API Design labels Aug 26, 2015
@jreback
Copy link
Contributor

jreback commented Aug 26, 2015

as discussed in #10801 . This API needs some clarification as to how it should iteract (if at all) with Timestamps/datetime.datetime and such.

@mroeschke mroeschke changed the title Date / Datetime in Period Index ENH: Support datetime.date in Period Index Mar 31, 2020
@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label Sep 21, 2020
@jbrockmendel jbrockmendel added the datetime.date stdlib datetime.date support label Jun 5, 2021
@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
datetime.date stdlib datetime.date support Datetime Datetime data dtype Enhancement Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type
Projects
None yet
Development

No branches or pull requests

5 participants