-1

I have some data as below and need to get the weeknumber and year in the same row.

data.head()

+---------------+
|     epoch     |
+---------------+
| 1580495399964 |
| 1580495399334 |
| 1580495397591 |
| 1580495396967 |
| 1580495396331 |
+---------------+

How can I get a result like this:

data.head()
+---------------+-----------+
|     epoch     | week-year |
+---------------+-----------+
| 1580495399964 | 4-2020    |
| 1580495399334 | 4-2020    |
| 1580495397591 | 4-2020    |
| 1580495396967 | 4-2020    |
| 1580495396331 | 4-2020    |
+---------------+-----------+
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Charan
  • 63
  • 4

3 Answers3

0

Try:

data['week-year'] = data.epoch.apply(pd.to_datetime, unit='ms')
data['week-year'] = data['week-year'].dt.strftime('%U-%Y')

           epoch week-year
0  1580495399964    4-2020
1  1580495399334    4-2020
2  1580495397591    4-2020
3  1580495396967    4-2020
4  1580495396331    4-2020
luigigi
  • 4,146
  • 1
  • 13
  • 30
-1

Below code line you can use to generate the output in the format you require:

import time

time1 = 1580495399964
base = time.gmtime(1580495399964 / 1000)
print("{0:2}-{1:4}".format(base.tm_mon, base.tm_year))
Vishal Gada
  • 201
  • 3
  • 12
-2
data['week-year'] = pd.to_datetime(data['epoch']+19800, unit='ms').dt.strftime('%U-%Y')


+------------------+-----------+
|  epoch week-year | week-year |
+------------------+-----------+
| 0  1580495399964 | 4-2020    |
| 1  1580495399334 | 4-2020    |
| 2  1580495397591 | 4-2020    |
| 3  1580495396967 | 4-2020    |
| 4  1580495396331 | 4-2020    |
+------------------+-----------+

Note: Add 19800 if your epoch is in GMT only

Charan
  • 63
  • 4