1 How to use timedelta in pandas


In [58]:
import numpy as np
import numpy.random as npr
import pandas as pd
import datetime as dt

sample_size = 1000
npr.seed(1234)

start_day = dt.datetime(2009, 7, 15, 0, 0)
start_day_num = dt.datetime(2009, 7, 15, 0, 0).timetuple().tm_yday # 196

# create a DataFrame of the kind that Mel appepars to be working with 
pdM = pd.DataFrame({
    'half_hours': npr.random_integers(low = 0, high = 48, size = sample_size),
    'num_days': npr.random_integers(low = 196, high = 365, size = sample_size)
    })

# create the timedelta measured in minutes
pdM['min_delta'] = pd.to_timedelta(
    pdM['half_hours']*30 + (pdM['num_days'] - start_day_num)*(24*60),
    unit = 'm'
    )

# create the datetime & day of week
pdM['YrMDayT'] = pd.to_datetime(start_day)  + pdM['min_delta'] 
pdM['dayofweek'] = pdM['YrMDayT'].dt.weekday
pdM


Out[58]:
half_hours num_days min_delta YrMDayT dayofweek
0 47 294 98 days 23:30:00 2009-10-21 23:30:00 2
1 19 333 137 days 09:30:00 2009-11-29 09:30:00 6
2 38 276 80 days 19:00:00 2009-10-03 19:00:00 5
3 12 298 102 days 06:00:00 2009-10-25 06:00:00 6
4 24 300 104 days 12:00:00 2009-10-27 12:00:00 1
5 15 207 11 days 07:30:00 2009-07-26 07:30:00 6
6 23 248 52 days 11:30:00 2009-09-05 11:30:00 5
7 41 333 137 days 20:30:00 2009-11-29 20:30:00 6
8 26 314 118 days 13:00:00 2009-11-10 13:00:00 1
9 30 235 39 days 15:00:00 2009-08-23 15:00:00 6
10 43 210 14 days 21:30:00 2009-07-29 21:30:00 2
11 30 230 34 days 15:00:00 2009-08-18 15:00:00 1
12 44 303 107 days 22:00:00 2009-10-30 22:00:00 4
13 26 343 147 days 13:00:00 2009-12-09 13:00:00 2
14 48 337 142 days 00:00:00 2009-12-04 00:00:00 4
15 28 266 70 days 14:00:00 2009-09-23 14:00:00 2
16 5 270 74 days 02:30:00 2009-09-27 02:30:00 6
17 16 360 164 days 08:00:00 2009-12-26 08:00:00 5
18 9 201 5 days 04:30:00 2009-07-20 04:30:00 0
19 47 257 61 days 23:30:00 2009-09-14 23:30:00 0
20 48 284 89 days 00:00:00 2009-10-12 00:00:00 0
21 12 353 157 days 06:00:00 2009-12-19 06:00:00 5
22 37 313 117 days 18:30:00 2009-11-09 18:30:00 0
23 34 285 89 days 17:00:00 2009-10-12 17:00:00 0
24 38 242 46 days 19:00:00 2009-08-30 19:00:00 6
25 3 237 41 days 01:30:00 2009-08-25 01:30:00 1
26 39 293 97 days 19:30:00 2009-10-20 19:30:00 1
27 11 211 15 days 05:30:00 2009-07-30 05:30:00 3
28 0 320 124 days 00:00:00 2009-11-16 00:00:00 0
29 41 311 115 days 20:30:00 2009-11-07 20:30:00 5
... ... ... ... ... ...
970 9 197 1 days 04:30:00 2009-07-16 04:30:00 3
971 36 320 124 days 18:00:00 2009-11-16 18:00:00 0
972 19 319 123 days 09:30:00 2009-11-15 09:30:00 6
973 4 200 4 days 02:00:00 2009-07-19 02:00:00 6
974 16 290 94 days 08:00:00 2009-10-17 08:00:00 5
975 35 227 31 days 17:30:00 2009-08-15 17:30:00 5
976 36 209 13 days 18:00:00 2009-07-28 18:00:00 1
977 11 320 124 days 05:30:00 2009-11-16 05:30:00 0
978 18 363 167 days 09:00:00 2009-12-29 09:00:00 1
979 9 198 2 days 04:30:00 2009-07-17 04:30:00 4
980 13 304 108 days 06:30:00 2009-10-31 06:30:00 5
981 18 265 69 days 09:00:00 2009-09-22 09:00:00 1
982 26 241 45 days 13:00:00 2009-08-29 13:00:00 5
983 27 243 47 days 13:30:00 2009-08-31 13:30:00 0
984 11 319 123 days 05:30:00 2009-11-15 05:30:00 6
985 29 361 165 days 14:30:00 2009-12-27 14:30:00 6
986 8 232 36 days 04:00:00 2009-08-20 04:00:00 3
987 36 207 11 days 18:00:00 2009-07-26 18:00:00 6
988 32 300 104 days 16:00:00 2009-10-27 16:00:00 1
989 0 267 71 days 00:00:00 2009-09-24 00:00:00 3
990 26 296 100 days 13:00:00 2009-10-23 13:00:00 4
991 31 246 50 days 15:30:00 2009-09-03 15:30:00 3
992 6 326 130 days 03:00:00 2009-11-22 03:00:00 6
993 22 361 165 days 11:00:00 2009-12-27 11:00:00 6
994 39 270 74 days 19:30:00 2009-09-27 19:30:00 6
995 41 330 134 days 20:30:00 2009-11-26 20:30:00 3
996 19 217 21 days 09:30:00 2009-08-05 09:30:00 2
997 41 248 52 days 20:30:00 2009-09-05 20:30:00 5
998 45 225 29 days 22:30:00 2009-08-13 22:30:00 3
999 40 280 84 days 20:00:00 2009-10-07 20:00:00 2

1000 rows × 5 columns