How to change datetime values into a separately formatted value

Viewed 71

I have a data set, which unfortunately has sporadic DateTime values, rather than int or str.

How could I go about editing the values, by iterating through the database and replacing the 2019-05-03 00:00:00 into 5-3, for example?

I have tried a few for loops but to no avail. Is there a pandas shortcut?

,age,menopause,tumor-size,inv-nodes,node-caps,deg-malig,breast,breast-quad,irradiat,Class
0,40-49,premeno,15-19,0-2,yes,3,right,left_up,no,recurrence-events
1,50-59,ge40,15-19,0-2,no,1,right,central,no,no-recurrence-events
2,50-59,ge40,35-39,0-2,no,2,left,left_low,no,recurrence-events
3,40-49,premeno,35-39,0-2,yes,3,right,left_low,yes,no-recurrence-events
4,40-49,premeno,30-34,2019-05-03 00:00:00,yes,2,left,right_up,no,recurrence-events
5,50-59,premeno,25-29,2019-05-03 00:00:00,no,2,right,left_up,yes,no-recurrence-events
6,50-59,ge40,40-44,0-2,no,3,left,left_up,no,no-recurrence-events
7,40-49,premeno,2014-10-01 00:00:00,0-2,no,2,left,left_up,no,no-recurrence-events
8,40-49,premeno,0-4,0-2,no,2,right,right_low,no,no-recurrence-events
9,40-49,ge40,40-44,15-17,yes,2,right,left_up,yes,no-recurrence-events
10,50-59,premeno,25-29,0-2,no,2,left,left_low,no,no-recurrence-events
11,60-69,ge40,15-19,0-2,no,2,right,left_up,no,no-recurrence-events
12,50-59,ge40,30-34,0-2,no,1,right,central,no,no-recurrence-events
13,50-59,ge40,25-29,0-2,no,2,right,left_up,no,no-recurrence-events
14,40-49,premeno,25-29,0-2,no,2,left,left_low,yes,recurrence-events
15,30-39,premeno,20-24,0-2,no,3,left,central,no,no-recurrence-events
16,50-59,premeno,2014-10-01 00:00:00,2019-05-03 00:00:00,no,1,right,left_up,no,no-recurrence-events
17,60-69,ge40,15-19,0-2,no,2,right,left_up,no,no-recurrence-events
18,50-59,premeno,40-44,0-2,no,2,left,left_up,no,no-recurrence-events
19,50-59,ge40,20-24,0-2,no,3,left,left_up,no,no-recurrence-events
20,50-59,lt40,20-24,0-2,?,1,left,left_low,no,recurrence-events
21,60-69,ge40,40-44,2019-05-03 00:00:00,no,2,right,left_up,yes,no-recurrence-events
22,50-59,ge40,15-19,0-2,no,2,right,left_low,no,no-recurrence-events
23,40-49,premeno,2014-10-01 00:00:00,0-2,no,1,right,left_up,no,no-recurrence-events
24,30-39,premeno,15-19,2019-08-06 00:00:00,yes,3,left,left_low,yes,recurrence-events
25,50-59,ge40,20-24,2019-05-03 00:00:00,yes,2,right,left_up,no,no-recurrence-events
4 Answers

Here is one way

df['inv-nodes']=df['inv-nodes'].str.extract('(\d{4})-(\d{2}-\d{2})')[1].fillna(df['tumor-size'])

0     15-19
1     15-19
2     35-39
3     35-39
4     30-34
5     25-29
6     40-44
7     10-01
8       0-4
9     40-44
10    25-29
11    15-19
12    30-34
13    25-29
14    25-29
15    20-24
16    10-01
17    15-19
18    40-44
19    20-24
20    20-24
21    40-44
22    15-19
23    10-01
24    15-19
25    20-24

you could use a custom function which uses regex to find datetime strings and replaces them with not-zero-padded '%m-%d' (on Linux, you could potientially also strftime with '%-m-%-d'...):

import re

def to_month_day(s):
    m = re.match("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}", s)
    if m:
        return m[0][5:7].lstrip('0') + '-' + m[0][8:10].lstrip('0')
    return s

# e.g.
df['inv-nodes'].apply(to_month_day)
# 0       0-2
# 1       0-2
# 2       0-2
# 3       0-2
# 4       5-3
# 5       5-3
# 6       0-2
# 7       0-2
# 8       0-2
# 9     15-17
# 10      0-2
# 11      0-2
# 12      0-2
# 13      0-2
# 14      0-2
# 15      0-2
# 16      5-3
# 17      0-2
# 18      0-2
# 19      0-2
# 20      0-2
# 21      5-3
# 22      0-2
# 23      0-2
# 24      8-6
# 25      5-3
import re
import datetime 

s = "2014-10-01 00:00:00"

pattern = re.compile("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}")

if pattern.match(s):
  d = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
  print(f"{str(d.month).zfill(2)}-{str(d.day).zfill(2)}")

This will only work on linux/unix systems so you might be ok on mac.

 df.loc[df['inv-nodes'].str.contains(':'),'inv-nodes'] = df.loc[df['inv-nodes'].str.contains(':')]['inv-nodes'].apply(lambda x: pd.to_datetime(x).strftime('%-m-%-d'))
Related