Reputation: 407
Sample Data
Year Week_No Value
2015 52 3
2016 2 7
2015 51 5
2016 1 6
2015 50 4
Below is the code that I have tried
import datetime
d = "2015-50"
r = datetime.datetime.strptime(d + '-1', "%Y-%W-%w")
print(r)
2015-12-14 00:00:00
How to move ahead to create a datetime column?
Upvotes: 4
Views: 382
Reputation: 7224
You can try this:
df['datetime'] = df.apply(lambda x: datetime.datetime.strptime(str(x.Year) + '-' + str(x.Week_No) + '-1', "%Y-%W-%w"), axis=1)
output:
Year Week_No Value datetime
0 2015 52 3 2015-12-28
1 2016 2 7 2016-01-11
2 2015 51 5 2015-12-21
3 2016 1 6 2016-01-04
4 2015 50 4 2015-12-14
Upvotes: 2
Reputation: 863246
I think in pandas is best use to_datetime
with add last value -1
for day of week:
df['datetime'] = pd.to_datetime(df.Year.astype(str) + '-' +
df.Week_No.astype(str) + '-1', format="%Y-%W-%w")
print (df)
Year Week_No Value datetime
0 2015 52 3 2015-12-28
1 2016 2 7 2016-01-11
2 2015 51 5 2015-12-21
3 2016 1 6 2016-01-04
4 2015 50 4 2015-12-14
Upvotes: 2