The code snippet below appears to show:
datetime.datetimeobjects have nonanosecondattribute on construction- when passed into
pandas'index.get_loc()they seem to aquire ananosecondattribute, possibly through conversion intoTimestamp - if the object passed into
get_loc()is a subclass ofdatetimehowever, it does not acquire ananosecondattribute and the code fails withdatetime_sub object has no attribute 'nanosecond'
The error is raised from the conversion from datetime to Timestamp in pandas' datetimes.py.
I'm not sure how to look further as the code enters what I imagine to be a C binary at that point.
It could be that pandas performs this conversion to Timestamp by checking for datetime as a type in a manner that excludes datetime subtypes.
Questions:
- Am I doing this wrong?
- Is this a bug in pandas?
- Assuming I must use a subclass of
datetime, Is there a way to get around this?
import pandas as pd
import datetime from datetime
class datetime_sub(datetime):
pass
dt = datetime(2012, 10, 22)
dt_s = datetime_sub(2012, 10, 22)
try:
print(dt.nanosecond)
except Exception as e:
print (e)
ind = pd.DatetimeIndex(pd.date_range('2012-10-1', '2012-10-30'))
dt_loc = ind.get_loc(dt)
dt_s_loc = ind.get_loc(dt_s)