ARJ
ARJ

Reputation: 2080

Use the Timestamp information to classify the columns into busniess hours and weekdays

I have a data frame with the following information,

         Timestamp         Day_of_week
    2017-07-11 09:31:44    Thursday
    2017-07-11 23:24:43    Thursday
    2017-07-23 14:24:34    Saturday
    2017-07-24 16:58:49    Wednesday
    2017-07-31 21:10:35    Monday

My aim is to append two more columns to the above data frame using the information from the Timestamp column. So this is what I tried, but I am not able to figure how to define a business hour (if between 9:00 AM to 18:00 PM).

def FDH(df):
    days=['Monday','Tuesday','Wednesday','Thursday','Friday']
    Time
    if df['day_of_week']==days:
        val = True
    else:
        value =False
    if df['time']>=09:00:00 and <=18:00:00:
        val=True

But the function is throwing the following error,

 File "<ipython-input-68-5ca185527341>", line 8
    if dates['time']>=09:00:00 && <=18:00:00:
                       ^
SyntaxError: invalid token

The aimmed output is,

UID    Timestamp        Weeday  Weekday_bussi_hour
AAD 2017-07-11 09:31:44 TRUE    TRUE
AAD 2017-07-11 23:24:43 TRUE    FALSE
SAP 2017-07-23 14:24:34 FALSE   FALSE
SAP 2017-07-24 16:58:49 TRUE    TRUE
YAS 2017-07-31 21:10:35 TRUE    FALSE

Upvotes: 0

Views: 131

Answers (2)

jezrael
jezrael

Reputation: 863256

You can use isin with between:

days=['Monday','Tuesday','Wednesday','Thursday','Friday']
df['busy_days'] = df['Day_of_week'].isin(days)
df['rush_hours'] = df['Timestamp'].dt.time.between(datetime.time(9), datetime.time(18))

Or dayofweek:

df['busy_days'] = df['Timestamp'].dt.dayofweek < 5
df['rush_hours'] = df['Timestamp'].dt.time.between(datetime.time(9), datetime.time(18))

print (df)
            Timestamp Day_of_week  busy_days  rush_hours
0 2017-07-11 09:31:44    Thursday       True        True
1 2017-07-11 23:24:43    Thursday       True       False
2 2017-07-23 14:24:34    Saturday      False        True
3 2017-07-24 16:58:49   Wednesday       True        True
4 2017-07-31 21:10:35      Monday       True       False

Upvotes: 2

AndreyF
AndreyF

Reputation: 1838

You can use between_time and weekday to achieve your goal

df['rush_hours'] = df['Timestamp'].between_time("9:00", "18:00") >= pd.datetime(1970,1,1)
df['rush_hours'].fillna(False, inplace=True)
df['busy_days'] = df['Timestamp'].dt.weekday < 5 # monday - friday

Upvotes: 1

Related Questions