0

Wave Group

I have a column called as datetime of type datetime64[ns] and for eg: it is represented as 2019-10-27 06:00:00 I would like to create a new column called waves which takes a interval from datetime and output as a categorical value in the waves table. I have attached a photo of the date range conidition for reference. I tried to use a loop for this , but I used a wrong syntax for it I believe.

def wave(row):
    if (row["datetime"] <= pd.to_datetime("2019-11-16").time())&(row["datetime"] >= pd.to_datetime("2020-02-28").time()):
        wave="before covid"
    elif (row["datetime"] <= pd.to_datetime("2020-03-01").time())&(row["datetime"].dt.time >= pd.to_datetime("2020-06-15").time()):
        wave="1st wave"
    elif (row["datetime"].dt.time <= pd.to_datetime("2020-06-16").time())&(row["datetime"].dt.time >= pd.to_datetime("2020-09-30").time()):
        wave="between waves"
    elif (row["datetime"].dt.time <= pd.to_datetime("2020-10-01").time())&(row["datetime"].dt.time >= pd.to_datetime("2021-01-15").time()):
        wave="2nd wave"
df["wave"]=df.apply(lambda row:wave(row),axis=1)
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Jan
  • 17
  • 5
  • 1
    always put the full error message (starting at the word "Traceback") in question (not in comments) as text (not a screenshot, not link to external portal). – maryam_k Apr 26 '22 at 12:30
  • It says that ("'Timestamp' object has no attribute 'dt'", 'occurred at index 0') – Jan Apr 26 '22 at 13:41
  • This is the condition for the time period I want to meet with the loop function: Before covid: 16th of Nov 2019 until 28th of Feb 2020 First wave: 1st of Mar 2020 until 15th of Jun 2020 Between waves: 16th of Jun 2020 until 30th of Sep 2020 Second wave: 1st of Okt 2020 until 15th of Jan 2021 – Jan Apr 26 '22 at 13:55
  • Are you using `pandas`? If so, you should add it as a tag. – Code-Apprentice Apr 26 '22 at 15:37
  • 1
    the waves are categorized by day/month/year, why do you compare to the time in the first place? – FObersteiner Apr 26 '22 at 15:37
  • Does this answer your question? [Timestamp object has no attribute dt](https://stackoverflow.com/questions/62803633/timestamp-object-has-no-attribute-dt) – Code-Apprentice Apr 26 '22 at 15:39
  • The above link should solve the error, but probably doesn't actually solve your problem. As FObersteiner says, you shouldn't be using the just time anyway. You should use the full datetime. – Code-Apprentice Apr 26 '22 at 15:40
  • Also, use `and` instead of `&`. Or better yet use `a <= b <= c`. – Code-Apprentice Apr 26 '22 at 15:42
  • I actually would like to compare year-month-day instead of time. But how do I change the code in order to achieve that? – Jan Apr 26 '22 at 15:44
  • Yes , I use pandas. How can I use it as a tag? – Jan Apr 26 '22 at 15:45
  • You can edit your question, you can change/add tags once you're in the edit-mode. @Question: just compare Timestamps, e.g. `row["datetime"] <= pd.Timestamp("2019-11-16")`, i.e. remove the .dt.time and .time(). – FObersteiner Apr 26 '22 at 16:57
  • `def wave(row): if (row["datetime"] <= pd.Timestamp("2019-11-16")) & (row["datetime"] >= pd.Timestamp("2020-02-28")): wave="before covid" elif (row["datetime"] <= pd.Timestamp("2020-03-01")) & (row["datetime"] >= pd.Timestamp("2020-06-15")): wave="1st wave" elif (row["datetime"] <= pd.Timestamp("2020-06-16")) & (row["datetime"] >= pd.Timestamp("2020-09-30")): wave="between waves" elif (row["datetime"] <= pd.Timestamp("2020-10-01")) & (row["datetime"] >= pd.Timestamp("2021-01-15")): wave="2nd wave" ` I tried this code with the change. – Jan Apr 26 '22 at 17:40
  • But it gives me an error : TypeError: ("'<=' not supported between instances of 'str' and 'Timestamp'", 'occurred at index 0') – Jan Apr 26 '22 at 17:41
  • So I got this error because the datatype of my `datetime` was object. So I changed it first by `df['datetime'] = pd.to_datetime(df['datetime'])` , then the code above runs. It creates a column called wave but the column is empty and do not contain the categorical values such as "before covid", "1st wave" etc. Does anyone know how I can fix this? – Jan Apr 27 '22 at 06:04

0 Answers0