1

Here's my data

I was doing sheet2['device_create_week'] = sheet2['device_create_at'].dt.week here's the result

        device_create_at        device_create_week
136     2014-08-27 17:29:23     35
245     2015-09-06 15:46:00     39
257     2014-09-29 22:26:34     40
258     2014-11-05 13:02:18     40
480     2020-02-02 17:59:54     6
481     2020-02-02 17:59:54     6
482     2020-02-02 17:59:54     6

But I want

        device_create_at        device_create_week
136     2014-08-27 17:29:23     2014-35
245     2015-09-06 15:46:00     2015-39
257     2014-09-29 22:26:34     2014-40
258     2014-11-05 13:02:18     2014-40
480     2020-02-02 17:59:54     2020-6
481     2020-02-02 17:59:54     2020-6
482     2020-02-02 17:59:54     2020-6

Other solution is fine as long as it is plotable, and I can see growth every week

Nabih Ibrahim Bawazir
  • 631
  • 2
  • 15
  • 27

1 Answers1

2

Use year + week:

sheet2['device_create_week'] = sheet2['device_create_at'].dt.year.astype(str) + '-' + 
                               sheet2['device_create_at'].dt.week.astype(str)

print (sheet2)

       device_create_at device_create_week
136 2014-08-27 17:29:23            2014-35
245 2015-09-06 15:46:00            2015-36
257 2014-09-29 22:26:34            2014-40
258 2014-11-05 13:02:18            2014-45
480 2020-02-02 17:59:54             2020-5
481 2020-02-02 17:59:54             2020-5
482 2020-02-02 17:59:54             2020-5

Another solution with strftime with V:

sheet2['device_create_week'] = sheet2['device_create_at'].dt.strftime('%Y-%V')

print (sheet2)
       device_create_at device_create_week
136 2014-08-27 17:29:23            2014-35
245 2015-09-06 15:46:00            2015-36
257 2014-09-29 22:26:34            2014-40
258 2014-11-05 13:02:18            2014-45
480 2020-02-02 17:59:54            2020-05
481 2020-02-02 17:59:54            2020-05
482 2020-02-02 17:59:54            2020-05
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252