Home > Mobile >  Trouble trying to get the Duration since newest review using ( dt.date.today() - newest google revie
Trouble trying to get the Duration since newest review using ( dt.date.today() - newest google revie

Time:07-26

df_test = df_all.copy()

df_test['measure'] = df_test['newest_review_date'].str.split().str[0].replace('an', 1).replace('Not', 0).astype(int)
df_test['value'] = df_test['newest_review_date'].str.split().str[1]

df_test['review_date_exact'] = df_test.apply(lambda x: x.date_now - relativedelta(seconds = x.measure) if "second" in x.value else
                                             (x.date_now - relativedelta(minutes = x.measure) if "min" in x.value else
                                             (x.date_now - relativedelta(hours = x.measure) if "hour" in x.value else
                                             (x.date_now - relativedelta(days = x.measure) if "day" in x.value else
                                             (x.date_now - relativedelta(weeks = x.measure) if "week" in x.value else
                                             (x.date_now - relativedelta(months = x.measure) if "month" in x.value else
                                             (x.date_now - relativedelta(years = x.measure) if "year" in x.value else dt.datetime(1990,1,1,) )))))), axis = 1)

df_test['duration'] = df_test.apply(lambda x : abs( (pd.to_datetime(dt.date.today()) - x['review_date_exact']).days ) , axis=1)

df_test

The code works for the most part, if a review was posted an hour ago it will correctly categorise the duration as "1 day" but if the review was posted 18 hours ago for some reason it doesn't fall under the duration "1" and shows 0 day instead.

Example of the output in the last column 'duration':

figure 1

CodePudding user response:

When you use dt.date.today() you get the datetime at midnight, rather than now. So the duration is the duration since midnight. Use datetime.now() instead to get the time (or use a datetime value for 'now' from whenever you want the relative value to be from)

  • Related