英文:
Creating a column that takes a Week Number and Year and returns a Date
问题
我正在使用一个数据框,其中我创建了一个“Year”和“Week #”列。我尝试创建一个新的“Date”列,用于从“Year”和“Week #”列中获取日期。
这是我的数据框现在的样子:
| Year | Week # | 
|---|---|
| 2023 | 10 | 
| 2023 | 11 | 
| 2023 | 12 | 
应该看起来像这样:
| Year | Week # | Date | 
|---|---|---|
| 2023 | 10 | 3/6/23 | 
| 2023 | 11 | 3/13/23 | 
| 2023 | 12 | 3/20/23 | 
我尝试了以下代码:
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'
然而,我得到了以下错误:
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         ..
35913    42\n35914    
43\n35915    44\n35916    45\n35917    46\nName: Week #, Length: 35918, dtype: 
object-1' does not match format '%Y-%W-%w'.
我还尝试了以下代码:
from datetime import datetime
from isoweek import Week
df['Date'] = Week(df['Year'], df['Week #']).monday()
但我得到了以下错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
英文:
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().
答案1
得分: 1
你可以使用 format 参数来指定日期格式。首先将日期以字符串格式构建,然后将其转换为实际日期。
data = {"year": [2022, 2023], "week": [1,2]}
df = pd.DataFrame(data)
print(df)
#    year  week
# 0  2022     1
# 1  2023     2
# 创建 'year-w-1' 格式的日期
df['date'] = df['year'].astype(str).str.cat(others=[df['week'].astype(str), np.array(['1']*len(df))], sep='-')
print(df)
#    year  week      date
# 0  2022     1  2022-1-1
# 1  2023     2  2023-2-1
# 格式为 年-周-星期几 (1 = 星期一)
df['date'] = pd.to_datetime(df['date'], format="%Y-%W-%w")
print(df)
#    year  week       date
# 0  2022     1 2022-01-03
# 1  2023     2 2023-01-09
请注意,第二个数据框中的 -1 后来被解释为星期几!
英文:
IIUC, you can use the format parameter to specify your date format. First build your date in string format, then turn it into a real date.
data = {"year": [2022, 2023], "week": [1,2]}
df = pd.DataFrame(data)
print(df)
#    year  week
# 0  2022     1
# 1  2023     2
# Create 'year-w-1' formatted dates
df['date'] = df['year'].astype(str).str.cat(others=[df['week'].astype(str), np.array(['1']*len(df))], sep='-')
print(df)
#    year  week      date
# 0  2022     1  2022-1-1
# 1  2023     2  2023-2-1
# format is year-week-weekday (1 = Monday)
df['date'] = pd.to_datetime(df['date'], format="%Y-%W-%w")
print(df)
#    year  week       date
# 0  2022     1 2022-01-03
# 1  2023     2 2023-01-09
Note that the -1 in the second dataframe is later interpreted as weekday!
答案2
得分: 1
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论