У меня большой DataFrame (1000000+ строк) с информацией о сотрудниках.
Содержит информацию об идентификаторе сотрудника, дате записи и статусе оборота.Если текучесть кадров не равна 1, работник работает в данный момент.
Вот пример:
test_df =\
pd.DataFrame({'empl_id': [1,2,3,1,2,3,1,2,1,2,1,2,3],
'rec_date':pd.to_datetime(['20080131','20080131','20080131',
'20080229', '20080229', '20080229',
'20080331', '20080331',
'20080430', '20080430',
'20080531', '20080531', '20080531'],
format='%Y%m%d'),
'turnover':[0,0,0,0,0,1,0,0,0,0,1,0,0]})
+----+-----------+---------------------+------------+
| | empl_id | rec_date | turnover |
+====+===========+=====================+============+
| 0 | 1 | 2008-01-31 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 1 | 2 | 2008-01-31 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 2 | 3 | 2008-01-31 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 3 | 1 | 2008-02-29 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 4 | 2 | 2008-02-29 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 5 | 3 | 2008-02-29 00:00:00 | 1 |
+----+-----------+---------------------+------------+
| 6 | 1 | 2008-03-31 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 7 | 2 | 2008-03-31 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 8 | 1 | 2008-04-30 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 9 | 2 | 2008-04-30 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 10 | 1 | 2008-05-31 00:00:00 | 1 |
+----+-----------+---------------------+------------+
| 11 | 2 | 2008-05-31 00:00:00 | 0 |
+----+-----------+---------------------+------------+
| 12 | 3 | 2008-05-31 00:00:00 | 0 |
+----+-----------+---------------------+------------+
Мне нужно показать, уходит ли сотрудник из компании, например, через 2 месяца относительно времениуказано в записи
Я нашел решения, но обработка идет слишком медленно.Для DataFrame такого размера это займет более 54 часов!
Вот мой скрипт:
from datetime import datetime, date, timedelta
import calendar
import pandas as pd
import numpy as np
# look only in employees with turnover
res = test_df.groupby('empl_id')['turnover'].sum()
keys_with_turn = res[res>0].index
# function for add months
def add_months(sourcedate,months):
month = sourcedate.month - 1 + months
year = sourcedate.year + month // 12
month = month % 12 + 1
day = min(sourcedate.day, calendar.monthrange(year,month)[1])
return date(year,month,day)
# add 2 months and convert to timestamp
test_df['rec_date_plus_2'] = test_df['rec_date'].apply(lambda x: add_months(x, 2))
test_df['rec_date_plus_2'] = pd.to_datetime(test_df['rec_date_plus_2'])
test_df['turn_nxt_2'] = np.nan
for i in range(len(keys_with_turn)): # loop over employees ids
for index, row in test_df[test_df['empl_id']==keys_with_turn[i]].iterrows(): # loop over all recs with employee
a = row['rec_date']
b = row['rec_date_plus_2']
turn_coef = test_df[(test_df['empl_id']==keys_with_turn[i]) &
((test_df['rec_date']>=a) & (test_df['rec_date']<=b))]['turnover'].sum()
test_df.loc[(test_df['rec_date']==a) &
(test_df['empl_id']==keys_with_turn[i]), 'turn_nxt_2'] = 0 if turn_coef == 0 else 1
test_df['turn_nxt_2'].fillna(0, inplace=True)
Тот самый результат, который я ищу:
+----+-----------+---------------------+------------+--------------+
| | empl_id | rec_date | turnover | turn_nxt_2 |
+====+===========+=====================+============+==============+
| 0 | 1 | 2008-01-31 00:00:00 | 0 | 0 |
+----+-----------+---------------------+------------+--------------+
| 1 | 2 | 2008-01-31 00:00:00 | 0 | 0 |
+----+-----------+---------------------+------------+--------------+
| 2 | 3 | 2008-01-31 00:00:00 | 0 | 1 |
+----+-----------+---------------------+------------+--------------+
| 3 | 1 | 2008-02-29 00:00:00 | 0 | 0 |
+----+-----------+---------------------+------------+--------------+
| 4 | 2 | 2008-02-29 00:00:00 | 0 | 0 |
+----+-----------+---------------------+------------+--------------+
| 5 | 3 | 2008-02-29 00:00:00 | 1 | 1 |
+----+-----------+---------------------+------------+--------------+
| 6 | 1 | 2008-03-31 00:00:00 | 0 | 1 |
+----+-----------+---------------------+------------+--------------+
| 7 | 2 | 2008-03-31 00:00:00 | 0 | 0 |
+----+-----------+---------------------+------------+--------------+
| 8 | 1 | 2008-04-30 00:00:00 | 0 | 1 |
+----+-----------+---------------------+------------+--------------+
| 9 | 2 | 2008-04-30 00:00:00 | 0 | 0 |
+----+-----------+---------------------+------------+--------------+
| 10 | 1 | 2008-05-31 00:00:00 | 1 | 1 |
+----+-----------+---------------------+------------+--------------+
| 11 | 2 | 2008-05-31 00:00:00 | 0 | 0 |
+----+-----------+---------------------+------------+--------------+
| 12 | 3 | 2008-05-31 00:00:00 | 0 | 0 |
+----+-----------+---------------------+------------+--------------+
Как сделать это быстрее и чаще, чем Панды?