0

I have the follow df:

time_series                date   sales
store_0051_item_909446    3/2021    4.0
store_0051_item_909446    4/2021    4.0
store_0051_item_909446    5/2021    1.0
store_0053_item_909446    6/2021    1.0
store_0059_item_909446    7/2021    0.0

Being 'date' week/year. I want transform this field in datetime: week-year. I tried with the follow code:

df['date'] = df['date'].apply(lambda x: datetime.strptime(x, '%W/%Y'))

But, return the follow df:

time_series                  date       sales
store_0051_item_909446    2021-01-01    4.0
store_0051_item_909446    2021-01-01    4.0
store_0051_item_909446    2021-01-01    1.0
store_0053_item_909446    2021-01-01    1.0
store_0059_item_909446    2021-01-01    0.0

What am I missing in the code?

Vivian
  • 95
  • 5
  • 3
    Does this answer your question? [Get date from week number](https://stackoverflow.com/questions/17087314/get-date-from-week-number) – Jack Moody May 27 '21 at 21:21
  • 1
    Yes, thanks for that! :D – Vivian May 27 '21 at 21:38
  • It's very unclear from Python's documentation that `%W` won't work unless used in conjunction with `w%` - possibly even a bug? – Grismar May 27 '21 at 22:05
  • Yeah, Grismar... make sense, I would like that returned this: W-Y: 45-2020. My doubt is: In datetime this format don't exist? – Vivian May 28 '21 at 18:22

1 Answers1

0
df['week'] = df['date'].apply(lambda x: x.isocalendar()[1])
rudolfovic
  • 3,163
  • 2
  • 14
  • 38