简体   繁体   中英

Setting column value on a slice of DataFrame not working

I have a dataset with employee payroll information (df2). It has a date, job title, shift start time, hours worked.

The goal is to create a dataset (df) which shows how many employees were working at any given hour.

The problem I am facing is that setting the value in a column is not having any effect on the original dataset (df).

data1 = [['2/1/2019','Cashier',0,0,0,0,0,0,0], ['2/2/2019','Cashier',0,0,0,0,0,0,0], ['2/1/2019','Server',0,0,0,0,0,0,0]]
cols1 = ['Date', 'Job'] + list(pd.date_range(pd.to_datetime('2/1/2019 15:00'), periods=7, freq='H'))
df=pd.DataFrame(data1, columns=cols1)

data2=[['2/1/2019', 'Cashier', pd.to_datetime('2/1/2019 16:00'), 5.2]]
cols2=['Date', 'Job', 'Start', 'Hours']
df2=pd.DataFrame(data2, columns=cols2)
def count_shifts(x):
    start_time=x['Start']
    worked_in_minutes =round(x['Hours']) * 60 + (x['Hours'] - round(x['Hours']))
    worked_range_index = pd.date_range(start_time, periods=worked_in_minutes, freq='T')
    worked_series = pd.Series(1/60, index=worked_range_index)
    worked_series=worked_series.resample('H', label='left').sum()
    df.loc[:,list(worked_series.index)] \
        [(df['Job']==x['Job']) & (df['Date']==x['Date'])] = worked_series.values


df2.apply(count_shifts, axis=1)

I expect df columns corresponding to the hours 15:00,16:00,17:00,18:00,19:00 to equal 1 and 20:00 to equal .2 But the actual result is that they are still 0

There are two issues:

First

worked_in_minutes =round(x['Hours']) * 60 + (x['Hours'] - round(x['Hours'])) is not doing what you expect it to do. It equals 300.2 for the first row in df2 instead of 312 which is what you might be expecting. There is no point in separating out the hours/minutes since it's already in decimal. worked_in_minutes = round(x['Hours'] * 60 should suffice.

Second, your assigning statement is first getting a subset and then setting something. This can have unexpected behavior.

Change it to df.loc[(df['Job']==x['Job']) & (df['Date']==x['Date']),list(worked_series.index)] = worked_series.values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM