Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 238
jtlz2 Avatar asked May 29 '26 18:05

jtlz2


2 Answers

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)
like image 170
jpp Avatar answered May 31 '26 09:05

jpp


In general, you should try to avoid calls to apply in Pandas, as it is very slow. For example, if you're trying to find out the minutes within quarters of hours, you can use:

from datetime import timedelta
df = pd.DataFrame({'dates': pd.to_datetime(['2071-12-12 10:04:44', '2071-12-12 10:30:44'])})
>>> df.dates.dt.minute.mod(15)
0    4
1    0
Name: dates, dtype: int64
like image 32
Ami Tavory Avatar answered May 31 '26 08:05

Ami Tavory



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!