week_of_month

week_of_month(idx, engine='pandas')

The “week_of_month” function calculates the week number of a given date within its month.

Parameters

Name Type Description Default
idx pd.Series or pd.DatetimeIndex The parameter “idx” is a pandas Series object that represents a specific date for which you want to determine the week of the month. required
engine str The engine parameter is used to specify the engine to use for calculating the week of the month. It can be either “pandas” or “polars”. - The default value is “pandas”. - When “polars”, the function will internally use the polars library for calculating the week of the month. This can be faster than using “pandas” for large datasets. 'pandas'

Returns

Type Description
pd.Series The week of the month for a given date.

Examples

import pytimetk as tk
import pandas as pd

dates = pd.date_range("2020-01-01", "2020-02-28", freq="1D")
dates
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',
               '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',
               '2020-01-09', '2020-01-10', '2020-01-11', '2020-01-12',
               '2020-01-13', '2020-01-14', '2020-01-15', '2020-01-16',
               '2020-01-17', '2020-01-18', '2020-01-19', '2020-01-20',
               '2020-01-21', '2020-01-22', '2020-01-23', '2020-01-24',
               '2020-01-25', '2020-01-26', '2020-01-27', '2020-01-28',
               '2020-01-29', '2020-01-30', '2020-01-31', '2020-02-01',
               '2020-02-02', '2020-02-03', '2020-02-04', '2020-02-05',
               '2020-02-06', '2020-02-07', '2020-02-08', '2020-02-09',
               '2020-02-10', '2020-02-11', '2020-02-12', '2020-02-13',
               '2020-02-14', '2020-02-15', '2020-02-16', '2020-02-17',
               '2020-02-18', '2020-02-19', '2020-02-20', '2020-02-21',
               '2020-02-22', '2020-02-23', '2020-02-24', '2020-02-25',
               '2020-02-26', '2020-02-27', '2020-02-28'],
              dtype='datetime64[ns]', freq='D')
# Works on DateTimeIndex
tk.week_of_month(dates, engine='pandas')
0     1
1     1
2     1
3     1
4     1
5     1
6     1
7     2
8     2
9     2
10    2
11    2
12    2
13    2
14    3
15    3
16    3
17    3
18    3
19    3
20    3
21    4
22    4
23    4
24    4
25    4
26    4
27    4
28    5
29    5
30    5
31    1
32    1
33    1
34    1
35    1
36    1
37    1
38    2
39    2
40    2
41    2
42    2
43    2
44    2
45    3
46    3
47    3
48    3
49    3
50    3
51    3
52    4
53    4
54    4
55    4
56    4
57    4
58    4
Name: week_of_month, dtype: int64
# Works on DateTimeIndex
tk.week_of_month(dates, engine='polars')
0     1
1     1
2     1
3     1
4     1
5     1
6     1
7     2
8     2
9     2
10    2
11    2
12    2
13    2
14    3
15    3
16    3
17    3
18    3
19    3
20    3
21    4
22    4
23    4
24    4
25    4
26    4
27    4
28    5
29    5
30    5
31    1
32    1
33    1
34    1
35    1
36    1
37    1
38    2
39    2
40    2
41    2
42    2
43    2
44    2
45    3
46    3
47    3
48    3
49    3
50    3
51    3
52    4
53    4
54    4
55    4
56    4
57    4
58    4
Name: week_of_month, dtype: uint32
# Works on Pandas Series
dates.to_series().week_of_month()
2020-01-01    1
2020-01-02    1
2020-01-03    1
2020-01-04    1
2020-01-05    1
2020-01-06    1
2020-01-07    1
2020-01-08    2
2020-01-09    2
2020-01-10    2
2020-01-11    2
2020-01-12    2
2020-01-13    2
2020-01-14    2
2020-01-15    3
2020-01-16    3
2020-01-17    3
2020-01-18    3
2020-01-19    3
2020-01-20    3
2020-01-21    3
2020-01-22    4
2020-01-23    4
2020-01-24    4
2020-01-25    4
2020-01-26    4
2020-01-27    4
2020-01-28    4
2020-01-29    5
2020-01-30    5
2020-01-31    5
2020-02-01    1
2020-02-02    1
2020-02-03    1
2020-02-04    1
2020-02-05    1
2020-02-06    1
2020-02-07    1
2020-02-08    2
2020-02-09    2
2020-02-10    2
2020-02-11    2
2020-02-12    2
2020-02-13    2
2020-02-14    2
2020-02-15    3
2020-02-16    3
2020-02-17    3
2020-02-18    3
2020-02-19    3
2020-02-20    3
2020-02-21    3
2020-02-22    4
2020-02-23    4
2020-02-24    4
2020-02-25    4
2020-02-26    4
2020-02-27    4
2020-02-28    4
Freq: D, Name: week_of_month, dtype: int64
# Works on Pandas Series
dates.to_series().week_of_month(engine='polars')
0     1
1     1
2     1
3     1
4     1
5     1
6     1
7     2
8     2
9     2
10    2
11    2
12    2
13    2
14    3
15    3
16    3
17    3
18    3
19    3
20    3
21    4
22    4
23    4
24    4
25    4
26    4
27    4
28    5
29    5
30    5
31    1
32    1
33    1
34    1
35    1
36    1
37    1
38    2
39    2
40    2
41    2
42    2
43    2
44    2
45    3
46    3
47    3
48    3
49    3
50    3
51    3
52    4
53    4
54    4
55    4
56    4
57    4
58    4
Name: week_of_month, dtype: uint32