-1

I am currently working on the pandas DataFrame with DateTime object. Is there a way to extract the month-weekofthemonth from pandas datetime object?

data = pd.DataFrame(pd.date_range(' 1/ 1/ 2020', periods = 7, freq ='D'))

0  2000-01-01
1  2000-01-02
2  2000-01-03
3  2000-01-04
4  2000-01-05
5  2000-01-06
6  2000-01-07

Expected:

0  2000-01-01       01-01
1  2000-01-02       01-01
2  2000-01-03       01-01
3  2000-01-04       01-01
4  2000-01-05       01-01 
5  2000-01-06       01-01
6  2000-01-07       01-01
7  2000-01-08       01-02 
8  2000-01-09       01-02
9  2000-01-10       01-02
wwii
  • 23,232
  • 7
  • 37
  • 77
ganpat
  • 41
  • 1
  • 5

2 Answers2

1

Based on Week of a month pandas

data[0].apply(lambda d: f'{d.month:02}-{(d.day-1) // 7 + 1:02}')

should give

0    01-01
1    01-01
2    01-01
3    01-01
4    01-01
5    01-01
6    01-01
7    01-02
8    01-02
9    01-02

Name: 0, dtype: object

Ronak Agrawal
  • 1,006
  • 1
  • 19
  • 48
0

Refer to Week of a month pandas

import pandas as pd

data = pd.DataFrame(pd.date_range(' 1/ 1/ 2020', periods = 10, freq ='D'))
data['WoM'] = data[0].apply(lambda d : str(d.month) + '-0'+str(d.day//8+1))
print(data)

This gives you:

           0   WoM
0 2020-01-01  1-01
1 2020-01-02  1-01
2 2020-01-03  1-01
3 2020-01-04  1-01
4 2020-01-05  1-01
5 2020-01-06  1-01
6 2020-01-07  1-01
7 2020-01-08  1-02
8 2020-01-09  1-02
9 2020-01-10  1-02