创建一个列,该列接受周数和年份,并返回一个日期。

huangapple go评论108阅读模式
英文:

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

我尝试了以下代码:

  1. from datetime import datetime
  2. df['Date'] = datetime.strptime('{}-{}-1'.format(df['Year'], df['Week #']), '%Y-%W-%w').strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'

然而,我得到了以下错误:

  1. ValueError: time data '0 2020\n1 2020\n2 2020\n3 2020\n4
  2. 2020\n ... \n35913 2024\n35914 2024\n35915 2024\n35916
  3. 2024\n35917 2024\nName: Year, Length: 35918, dtype: int64-0 02\n1
  4. 03\n2 04\n3 05\n4 06\n ..
  5. 35913 42\n35914
  6. 43\n35915 44\n35916 45\n35917 46\nName: Week #, Length: 35918, dtype:
  7. object-1' does not match format '%Y-%W-%w'.

我还尝试了以下代码:

  1. from datetime import datetime
  2. from isoweek import Week
  3. df['Date'] = Week(df['Year'], df['Week #']).monday()

但我得到了以下错误:

  1. 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

  1. from datetime import datetime
  2. 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

  1. ---------------------------------------------------------------------------
  2. ValueError Traceback (most recent call last)
  3. Cell In[57], line 3
  4. 1 from datetime import datetime
  5. ----> 3 df['Date'] = datetime.strptime('{}-{}-1'.format(df['Year'], df['Week #']), '%Y-%W-%w').strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
  6. 4 df
  7. File /opt/anaconda3/lib/python3.9/_strptime.py:568, in _strptime_datetime(cls, data_string, format)
  8. 565 def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
  9. 566 """Return a class cls instance based on the input string and the
  10. 567 format string."""
  11. --> 568 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  12. 569 tzname, gmtoff = tt[-2:]
  13. 570 args = tt[:6] + (fraction,)
  14. File /opt/anaconda3/lib/python3.9/_strptime.py:349, in _strptime(data_string, format)
  15. 347 found = format_regex.match(data_string)
  16. 348 if not found:
  17. --> 349 raise ValueError("time data %r does not match format %r" %
  18. 350 (data_string, format))
  19. 351 if len(data_string) != found.end():
  20. 352 raise ValueError("unconverted data remains: %s" %
  21. 353 data_string[found.end():])
  22. ValueError: time data '0 2020\n1 2020\n2 2020\n3 2020\n4
  23. 2020\n ... \n35913 2024\n35914 2024\n35915 2024\n35916
  24. 2024\n35917 2024\nName: Year, Length: 35918, dtype: int64-0 02\n1
  25. 03\n2 04\n3 05\n4 06\n ..\n35913 42\n35914
  26. 43\n35915 44\n35916 45\n35917 46\nName: Week #, Length: 35918, dtype:
  27. object-1' does not match format '%Y-%W-%w'

I also tried the following

  1. from datetime import datetime
  2. from isoweek import Week
  3. df['Date'] = Week(df['Year'], df['Week #']).monday()

But I got the following error

  1. ---------------------------------------------------------------------------
  2. ValueError Traceback (most recent call last)
  3. Cell In[51], line 4
  4. 1 from datetime import datetime
  5. 2 from isoweek import Week
  6. ----> 4 df['Date'] = Week(df['Year'], df['Week #']).monday()
  7. File /opt/anaconda3/lib/python3.9/site-packages/isoweek.py:34, in Week.__new__(cls, year, week)
  8. 27 def __new__(cls, year, week):
  9. 28 """Initialize a Week tuple with the given year and week number.
  10. 29
  11. 30 The week number does not have to be within range. The numbers
  12. 31 will be normalized if not. The year must be within the range
  13. 32 1 to 9999.
  14. 33 """
  15. ---> 34 if week < 1 or week > 52:
  16. 35 return cls(year, 1) + (week - 1)
  17. 36 if year < 1 or year > 9999:
  18. File /opt/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py:1527, in NDFrame.__nonzero__(self)
  19. 1525 @final
  20. 1526 def __nonzero__(self) -> NoReturn:
  21. -> 1527 raise ValueError(
  22. 1528 f"The truth value of a {type(self).__name__} is ambiguous. "
  23. 1529 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
  24. 1530 )
  25. ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

答案1

得分: 1

你可以使用 format 参数来指定日期格式。首先将日期以字符串格式构建,然后将其转换为实际日期。

  1. data = {"year": [2022, 2023], "week": [1,2]}
  2. df = pd.DataFrame(data)
  3. print(df)
  4. # year week
  5. # 0 2022 1
  6. # 1 2023 2
  7. # 创建 'year-w-1' 格式的日期
  8. df['date'] = df['year'].astype(str).str.cat(others=[df['week'].astype(str), np.array(['1']*len(df))], sep='-')
  9. print(df)
  10. # year week date
  11. # 0 2022 1 2022-1-1
  12. # 1 2023 2 2023-2-1
  13. # 格式为 年-周-星期几 (1 = 星期一)
  14. df['date'] = pd.to_datetime(df['date'], format="%Y-%W-%w")
  15. print(df)
  16. # year week date
  17. # 0 2022 1 2022-01-03
  18. # 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.

  1. data = {"year": [2022, 2023], "week": [1,2]}
  2. df = pd.DataFrame(data)
  3. print(df)
  4. # year week
  5. # 0 2022 1
  6. # 1 2023 2
  7. # Create 'year-w-1' formatted dates
  8. df['date'] = df['year'].astype(str).str.cat(others=[df['week'].astype(str), np.array(['1']*len(df))], sep='-')
  9. print(df)
  10. # year week date
  11. # 0 2022 1 2022-1-1
  12. # 1 2023 2 2023-2-1
  13. # format is year-week-weekday (1 = Monday)
  14. df['date'] = pd.to_datetime(df['date'], format="%Y-%W-%w")
  15. print(df)
  16. # year week date
  17. # 0 2022 1 2022-01-03
  18. # 1 2023 2 2023-01-09

Note that the -1 in the second dataframe is later interpreted as weekday!

答案2

得分: 1

  1. import pandas as pd
  2. import datetime
  3. # create a sample dataframe with week number and year columns
  4. df = pd.DataFrame({"Year": [2016, 2016, 2016, 2017, 2017, 2017],"Week": [43, 44, 51, 2, 5, 12]})
  5. # define a function that takes a week number and year and returns a date
  6. def week_to_date(week, year):
  7. # get the first day of the week (Monday) for the given week and year
  8. date = datetime.date.fromisocalendar(year, week, 1)
  9. return date
  10. # apply the function to the dataframe and create a new column with the date
  11. df["Date"] = df.apply(lambda row: week_to_date(row["Week"], row["Year"]), axis=1)
  12. # print the dataframe
  13. print(df)

Output:

  1. Year Week Date
  2. 0 2016 43 2016-10-24
  3. 1 2016 44 2016-10-31
  4. 2 2016 51 2016-12-19
  5. 3 2017 2 2017-01-09
  6. 4 2017 5 2017-01-30
  7. 5 2017 12 2017-03-20
英文:

As i do not have your data frame to match i am using sample input

  1. import pandas as pd
  2. import datetime
  3. # create a sample dataframe with week number and year columns
  4. df = pd.DataFrame({"Year": [2016, 2016, 2016, 2017, 2017, 2017],"Week": [43, 44, 51, 2, 5, 12]})
  5. # define a function that takes a week number and year and returns a date
  6. def week_to_date(week, year):
  7. # get the first day of the week (Monday) for the given week and year
  8. date = datetime.date.fromisocalendar(year, week, 1)
  9. return date
  10. # apply the function to the dataframe and create a new column with the date
  11. df["Date"] = df.apply(lambda row: week_to_date(row["Week"], row["Year"]), axis=1)
  12. # print the dataframe
  13. print(df)

Output:

  1. Year Week Date
  2. 0 2016 43 2016-10-24
  3. 1 2016 44 2016-10-31
  4. 2 2016 51 2016-12-19
  5. 3 2017 2 2017-01-09
  6. 4 2017 5 2017-01-30
  7. 5 2017 12 2017-03-20

huangapple
  • 本文由 发表于 2023年2月16日 02:29:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定