pythonpandasdatetimeclass-extensions

Python Pandas: How do I apply a function requiring an extended class (datetime)?


How can I use pandas apply for a function that requires an extension of a standard class (datetime)?

Specifically, I would like to import datetime_modulo from the excellent gist at https://gist.github.com/treyhunner/6218526.

This code extends the standard datetime class to allow the modulo operation to be applied to datetime objects, e.g.

from datetime_modulo import datetime
from datetime import timedelta
d = datetime.now()
print d % timedelta(seconds=60)

Now I need to apply this modulo operation to a pandas DataFrame column/Series, e.g.

df['dates'] = pd.to_datetime(df.index.values)
df['datetime_mod'] = df['dates'].apply(lambda x: x % timedelta(minutes=15))

But pandas is not able to detect the extended datetime class (unless I am just using it wrongly):

TypeError: unsupported operand type(s) for %: 'Timestamp' and 'datetime.timedelta'

How to proceed?


Solution

  • You can try, as per this suggestion, converting the operand to datetime explicitly:

    from datetime_modulo import datetime
    from datetime import timedelta
    
    df = pd.DataFrame({'Time': [pd.to_datetime('now')]})
    
    def modulo(x):
        dt = datetime(year=x.year,month=x.month,day=x.day, hour=x.hour, minute=x.minute, second=x.second)
        return dt % timedelta(seconds=60)
    
    df['Time'] = df['Time'].apply(modulo)