Skip to content

"can't compare datetime.date to str" in slicing/indexing (version 0.11.0) #3532

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
vfilimonov opened this issue May 6, 2013 · 5 comments
Closed

Comments

@vfilimonov
Copy link
Contributor

Hello all

I don't know if this has been already fixed in developing 0.12, but just in case:

it seemed that indexing with datetime.date does not work properly.

E.g. the code

IDX=[datetime.strptime(yy,'%Y-%m-%d') for yy in ["2005-01-01","2006-01-01","2006-01-01","2007-01-01"]]
IDX
df = pd.DataFrame([1,2,3,4], index=IDX)
print type(df.index[0])
print df.ix['2005-01-01']
df[:'2006-01-02']

works correctly. And the code

IDX=[datetime.date(datetime.strptime(yy,'%Y-%m-%d')) for yy in ["2005-01-01","2006-01-01","2006-01-01","2007-01-01"]]
IDX
df = pd.DataFrame([1,2,3,4], index=IDX)
print type(df.index[0])
print df.ix['2005-01-01']
df[:'2006-01-02']

failed with the issue "TypeError: can't compare datetime.date to str"

@jreback
Copy link
Contributor

jreback commented May 6, 2013

datetime.date types are not real friendly, use datetime.datetime

@vfilimonov
Copy link
Contributor Author

OK, understood. Thank you for the reply. And are there any elegant way of converting column/index from datetime.date to datetime.datetime in DataFrame? I use pandas.io.sql.read_frame to get the table from database and types are assigned automatically.

@jreback
Copy link
Contributor

jreback commented May 6, 2013

it really shouldn't give you back that type anyhow (there are a couple of open bugs related to that)

try:

import pandas as pd

df['A'] = pd.to_datetime(df['A'])

you want datetimes to have the dtype datetime64[ns]

In [26]: df = DataFrame([Timestamp('20000101')]*3,index=range(3))

In [27]: df
Out[27]: 
                    0
0 2000-01-01 00:00:00
1 2000-01-01 00:00:00
2 2000-01-01 00:00:00

In [28]: df.dtypes
Out[28]: 
0    datetime64[ns]
dtype: object

@vfilimonov
Copy link
Contributor Author

jreback, thanks a lot! pd.to_datetime was exactly what I was looking for.

@jreback
Copy link
Contributor

jreback commented May 7, 2013

great...i have noted the other issue (that date is not well behanved (esp from sql)).....so closing this unless you have further issues

@jreback jreback closed this as completed May 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants