Skip to content

Problem constructing Series from dict with datetime.date in level of MultiIndex #10060

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

Closed
eoincondron opened this issue May 5, 2015 · 4 comments

Comments

@eoincondron
Copy link

When calling the Series constructor on a dict with tuple keys containing a datetime.date object
the values are converted to NaNs as this example demonstrates:

In [52]:

pd.Series({(1, datetime.date(2012, 1, 1)): 1, 
           (2, datetime.date(2012, 1, 2)): 2})
Out[52]:
1  2012-01-01   NaN
2  2012-01-02   NaN

It only happens with MultiIndex but otherwise arises regardless of the number and order of the levels.
I know having datetime.date objects in an index is not optimal and that calling the constructor in this way actually forces the dates to Timestamp objects but in my use case I'm receiving data which is structured this way and converting it to Pandas objects.

@eoincondron
Copy link
Author

P.S.
version is 0.16.0

@jreback
Copy link
Contributor

jreback commented May 5, 2015

it seems the same question is #8802

You can do this if you really want.

In [4]: pd.MultiIndex.from_arrays([[1,2],Index([datetime.date(2013,1,1),datetime.date(2014,1,1)])])
Out[4]: 
MultiIndex(levels=[[1, 2], [2013-01-01, 2014-01-01]],
           labels=[[0, 1], [0, 1]])

In [5]: pd.MultiIndex.from_arrays([[1,2],Index([datetime.date(2013,1,1),datetime.date(2014,1,1)])]).levels[1]
Out[5]: Index([2013-01-01, 2014-01-01], dtype='object')

In [6]: pd.MultiIndex.from_arrays([[1,2],Index([datetime.date(2013,1,1),datetime.date(2014,1,1)])]).levels[1][0]
Out[6]: datetime.date(2013, 1, 1)

This is in general not very useful, much slower, and harder to deal with. Is their a reason you insist on using datetime.date?

@eoincondron
Copy link
Author

It's not really the same question. In the previous question I had a problem with the new version forcing dates to Timestamps which caused existing code not to work. I've adopted this and the code I write now doesn't use datetime.date objects in the index. However, I'm working with data which is produced by existing programmes written by others which isn't aimed at Pandas and has a dictionary with tuple keys containing datetime.date objects which I would like to convert to Pandas.
I have no problem with the index of the resultant Series (or DataFrame in the case of a dict of dicts) being forced to contain to Timestamps. The problem is that values of the result are all NaN which seems like an a genuine bug.

@jreback
Copy link
Contributor

jreback commented May 5, 2015

prob related to this: #9456

pull-requests welcome for fixing that (and you can use this as a test case as well)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants