0

I have a dataframe with datetimes but would like to convert it to week dates. These week conversion should align with the pd.grouper(freq='W') format. How can I do this?

date         | week_date
2022-03-10   | 2022-03-06
2022-03-15   | 2022-03-13
2022-03-21   | 2022-03-20

When you use pd.grouper(freq='W'), these results of how weeks are formatted is in the week_date column above. How do I get this formatting without using grouper just by a random date of a user?

titutubs
  • 355
  • 1
  • 9
  • Does this answer your question? [How to get week number in Python?](https://stackoverflow.com/questions/2600775/how-to-get-week-number-in-python) – Rabinzel Aug 06 '22 at 22:45
  • @Rabinzel it does not. i need the week frequency in date format not as a week index :( – titutubs Aug 06 '22 at 22:46
  • Maybe I don't get some of it, but that doesn't make sense to me. Your desired output as date can't be seen as week 1, 2, 4 in python, it will be 1st, 2nd and 4th of january. What happen if it is not january, is the last number increasing till 52? – Rabinzel Aug 06 '22 at 23:02
  • Your choice of output formatting, e.g. `2020-01-04`, seems regrettable. It invites confusion with the iso8601 representation of January 4th. https://imgs.xkcd.com/comics/iso_8601.png Consider using strings like `2020 04` or `2020 52` instead. – J_H Aug 06 '22 at 23:11
  • When you use pd.grouper(freq='W'), these results of how weeks are formatted is in the week_date column above. How do I get this formatting without using grouper just by a random date of a user? – titutubs Aug 06 '22 at 23:24
  • Do you mean week start? – BeRT2me Aug 06 '22 at 23:28
  • @BeRT2me yeah. the format that grouper (freq=w) outputs. If that is week start then that is what I am looking for – titutubs Aug 06 '22 at 23:32
  • I'm not sure how you've come up with this output. It looks like an anchored week start of Saturday which is an atypical week start. `df['week_date'] = pd.to_datetime(df['date']).dt.to_period('W-SAT').dt.start_time` (see [this answer](/a/68634326/15497888) as an example). You can play around with [anchored offsets](https://pandas.pydata.org/docs/user_guide/timeseries.html#anchored-offsets) to get your desired start dates if Saturday is not what you're expecting. – Henry Ecker Aug 07 '22 at 03:26

1 Answers1

2

Does this get at what you're looking for?

import pandas as pd

# Make frame
data = {"date": ["2022-03-10", "2022-03-15", "2022-03-21"]}
df = pd.DataFrame(data=data)

# Convert values to datetime
df["date"] = pd.to_datetime(df["date"])

# Get week number, with pd.DateOffset used to start the week on Sunday
week_date_values = df["date"].dt.isocalendar()
df["week_date"] = (df['date'] - pd.to_timedelta(df['date'].dt.dayofweek, unit='d') + pd.DateOffset(days=-1)).dt.strftime("%Y-%m-%d")

# Combine with "(week n)" values
df["week_date"] = df["week_date"] + " " + "(week " + week_date_values["week"].astype(str) + ")"

Output

date week_date
2022-03-10 2022-03-06 (week 10)
2022-03-15 2022-03-13 (week 11)
2022-03-21 2022-03-20 (week 12)
Ray
  • 463
  • 4
  • 8