I'm currently working with a dataframe where I created a Year and Week # column. I'm trying to create a new column Date that gives me the date for a from the Year and Week # columns.
This is what my dataframe looks like now
Year | Week # |
---|---|
2023 | 10 |
2023 | 11 |
2023 | 12 |
It should look like this
Year | Week # | Date |
---|---|---|
2023 | 10 | 3/6/23 |
2023 | 11 | 3/13/23 |
2023 | 12 | 3/20/23 |
I tried the following
from datetime import datetime
df['Date'] = datetime.strptime('{}-{}-1'.format(df['Year'], df['Week #']), '%Y-%W-%w').strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
However, I got this error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[57], line 3
1 from datetime import datetime
----> 3 df['Date'] = datetime.strptime('{}-{}-1'.format(df['Year'], df['Week #']), '%Y-%W-%w').strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
4 df
File /opt/anaconda3/lib/python3.9/_strptime.py:568, in _strptime_datetime(cls, data_string, format)
565 def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
566 """Return a class cls instance based on the input string and the
567 format string."""
--> 568 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
569 tzname, gmtoff = tt[-2:]
570 args = tt[:6] + (fraction,)
File /opt/anaconda3/lib/python3.9/_strptime.py:349, in _strptime(data_string, format)
347 found = format_regex.match(data_string)
348 if not found:
--> 349 raise ValueError("time data %r does not match format %r" %
350 (data_string, format))
351 if len(data_string) != found.end():
352 raise ValueError("unconverted data remains: %s" %
353 data_string[found.end():])
ValueError: time data '0 2020\n1 2020\n2 2020\n3 2020\n4
2020\n ... \n35913 2024\n35914 2024\n35915 2024\n35916
2024\n35917 2024\nName: Year, Length: 35918, dtype: int64-0 02\n1
03\n2 04\n3 05\n4 06\n ..\n35913 42\n35914
43\n35915 44\n35916 45\n35917 46\nName: Week #, Length: 35918, dtype:
object-1' does not match format '%Y-%W-%w'
I also tried the following
from datetime import datetime
from isoweek import Week
df['Date'] = Week(df['Year'], df['Week #']).monday()
But I got the following error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[51], line 4
1 from datetime import datetime
2 from isoweek import Week
----> 4 df['Date'] = Week(df['Year'], df['Week #']).monday()
File /opt/anaconda3/lib/python3.9/site-packages/isoweek.py:34, in Week.__new__(cls, year, week)
27 def __new__(cls, year, week):
28 """Initialize a Week tuple with the given year and week number.
29
30 The week number does not have to be within range. The numbers
31 will be normalized if not. The year must be within the range
32 1 to 9999.
33 """
---> 34 if week < 1 or week > 52:
35 return cls(year, 1) + (week - 1)
36 if year < 1 or year > 9999:
File /opt/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py:1527, in NDFrame.__nonzero__(self)
1525 @final
1526 def __nonzero__(self) -> NoReturn:
-> 1527 raise ValueError(
1528 f"The truth value of a {type(self).__name__} is ambiguous. "
1529 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1530 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
As i do not have your data frame to match i am using sample input
import pandas as pd
import datetime
# create a sample dataframe with week number and year columns
df = pd.DataFrame({"Year": [2016, 2016, 2016, 2017, 2017, 2017],"Week": [43, 44, 51, 2, 5, 12]})
# define a function that takes a week number and year and returns a date
def week_to_date(week, year):
# get the first day of the week (Monday) for the given week and year
date = datetime.date.fromisocalendar(year, week, 1)
return date
# apply the function to the dataframe and create a new column with the date
df["Date"] = df.apply(lambda row: week_to_date(row["Week"], row["Year"]), axis=1)
# print the dataframe
print(df)
Output:
Year Week Date
0 2016 43 2016-10-24
1 2016 44 2016-10-31
2 2016 51 2016-12-19
3 2017 2 2017-01-09
4 2017 5 2017-01-30
5 2017 12 2017-03-20