0

I am trying to add a day to my column CutoffSLAII if the time in the column is after 1.59am, however, I only want to add a day if the column FILE_START_TIME also has a time prior to 12:00am. If neither of these conditions are met, then the value in CutoffSLAII should be retained.

The code I am using does run, but nothing changes in the dataframe:

from datetime import datetime, time, timedelta import pandas as pd

def add_a_day(row: pd.Series) -> datetime:
    if pd.isna(x['CutoffSLAII', 'FILE_START_TIME']):
        return x['CutoffSLAII', 'FILE_START_TIME']
    tme = x['CutoffSLAII'].time()
    tme1 = x['FILE_START_TIME'].time()

    if tme < time(12, 0, 0) and tme1 > time(1, 59, 0):
        return x['CutoffSLAII'] + timedelta(days=1)

data: df2['CutoffSLAII'] = df2.apply(add_a_day, axis=1)

Data that I wish to add a day to:

1

Both FILE_START_TIME and CutoffSLAII are Datetime64[ns] dtypes, however, when I interact with one value in the columns, they are returned as a timestamp.

in: df2.iloc[0]['FILE_START_TIME']

out: Timestamp('2020-11-02 19:23:47')

The data is not embedded as I do not have enough reputation points, sorry for that.

The error message is now:

TypeError: string indices must be integers

PredragDj
  • 313
  • 1
  • 3
  • 10

2 Answers2

1

Your function add_a_day takes a variable named row but acts on another one named x. You may want to fix that first !

Yassinef16
  • 11
  • 1
  • Hello, so should I change the function to return the row rather than return x? Thanks – Cait Stowell Dec 05 '20 at 18:16
  • I think you have figured out by now that the variable `x` you are using is not declared inside the function. This should be `row` variable instead. – Yassinef16 Dec 07 '20 at 15:38
0

I'm a little bit confused what's going on. Has X been referenced somewhere else or is that supposed to be referencing row? Also you map row as a series to datetime when you are applying on the whole data frame. Lastly I think you are trying to reference the rows incorrectly.

if pd.isna(x['CutoffSLAII']) or pd.isna(x['FILE_START_TIME'])):

fthomson
  • 773
  • 3
  • 9
  • X is supposed to be referencing that row. I have changed the code at the end to df2["CutoffSLAII"].apply now. I will add an edit to the question. Sorry for the confusion, I've never written a function before! Just for my understanding, do you think the x is not required to reference the row? If not, can you explain why? Thanks – Cait Stowell Dec 05 '20 at 18:11
  • Hey so I think you are a bit mixed up on what x generally does. x is usually used as an iterator or to store a value. There is no predefined value for x. In your function you haven't defined x to anything. You should be using your input parameter "row" to access that row value. Also careful because if you make the change df2["CutoffSLAII"].apply(function) you are now applying to the that column directly and will not be able to access 'FILE_START_TIME'. I would leave it as df2.apply(function) since you plan on accessing multiple columns. I can edit my answer if you are still having trouble :) – fthomson Dec 05 '20 at 18:37