Reputation: 203
Below is a small subset of a quite big dataframe in python.
ID invoiceDate
1 2017-07-18
2 2017-10-17
3 2017-08-20
4 2017-09-05
5 2017-08-20
6 2017-11-08
7 2017-09-17
8 2017-11-10
9 2017-08-14
10 2017-08-24
Given that the period of interest is between 2017-07-13
and 2017-11-21
, it is equivalent to 20 weeks. I need to map each value in invoiceDate
to the corresponding week number. For example, 2017-07-18
is mapped to week #1 (week one starts and ends at 2017-07-13
and 2017-07-19
, respectively) or 2017-08-20
to week #6 (week six starts and ends at 2017-08-17
and 2017-08-23
, respectively). The invoiceDate
is of type object.
Upvotes: 1
Views: 535
Reputation: 6639
Are you looking for something like this?
df = pd.DataFrame()
df['ID'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
df['InvDate'] = ['2017-07-18', '2017-10-17', '2017-08-20', '2017-09-05', '2017-08-20', '2017-11-08', '2017-09-17', '2017-11-10', '2017-08-14', '2017-08-24']
df['InvDate'] = pd.to_datetime(df['InvDate'])
df['WeekNum'] = df['InvDate'].dt.to_period('W').dt.week
df['WeekNum'] = df['WeekNum'] - df['WeekNum'][0] + 1
Output:
ID InvDate WeekNum
0 1 2017-07-18 1
1 2 2017-10-17 14
2 3 2017-08-20 6
3 4 2017-09-05 8
4 5 2017-08-20 6
5 6 2017-11-08 17
6 7 2017-09-17 10
7 8 2017-11-10 18
8 9 2017-08-14 5
9 10 2017-08-24 7
Upvotes: 1
Reputation: 153460
Use to_period
with freq 'W-Wed' altering the start of the week.
s = df['invoiceDate'].dt.to_period('W-Wed').dt.week
df['Week_Num'] = (s - s.min() + 1)
df['Week_Num'] = (df['invoiceDate'].dt.to_period('W-Wed').dt.week -
df['invoiceDate'].dt.to_period('W-Wed').dt.week.min() + 1)
Output:
productID invoiceDate Week_Num
0 1 2017-07-18 1
1 2 2017-10-17 14
2 3 2017-08-20 6
3 4 2017-09-05 8
4 5 2017-08-20 6
5 6 2017-11-08 17
6 7 2017-09-17 10
7 8 2017-11-10 18
8 9 2017-08-14 5
9 10 2017-08-24 7
Upvotes: 5
Reputation: 323226
IIUC
(df.invoiceDate-pd.to_datetime('2017-07-13')).dt.days//7+1
Out[352]:
0 1
1 14
2 6
3 8
4 6
5 17
6 10
7 18
8 5
9 7
Name: invoiceDate, dtype: int64
Upvotes: 2