回答編集履歴
1
コード修正
answer
CHANGED
@@ -13,26 +13,22 @@
|
|
13
13
|
"昭和47年12月",]
|
14
14
|
})
|
15
15
|
|
16
|
-
def
|
16
|
+
def conv(s):
|
17
|
-
m = re.search(r'(\d+)-(\d+)', s)
|
18
|
-
|
17
|
+
# 年月を抽出
|
19
|
-
return int(m.group(1)), int(m.group(2))
|
20
|
-
m = re.search(r'平成(\d+)年(\d+)月', s)
|
21
|
-
if m:
|
22
|
-
return int(m.group(1))+1988, int(m.group(2))
|
23
|
-
|
18
|
+
rules = [(r'(\d+)-(\d+)', 0), (r'平成(\d+)年(\d+)月', 1988), (r'昭和(\d+)年(\d+)月', 1925)]
|
24
|
-
if m:
|
25
|
-
return int(m.group(1))+1925, int(m.group(2))
|
26
|
-
|
19
|
+
y, m = None, None
|
20
|
+
for exp, offset in rules:
|
21
|
+
ret = re.search(exp, s)
|
22
|
+
if ret:
|
23
|
+
y = int(ret.group(1)) + offset
|
24
|
+
m = int(ret.group(2))
|
25
|
+
break
|
27
26
|
|
28
|
-
def conv(s):
|
29
|
-
y,m = conv_ym(s)
|
30
27
|
s = ''
|
31
28
|
if y and m:
|
32
29
|
s = f'{y}年{m}月'
|
33
30
|
return s
|
34
31
|
|
35
|
-
|
36
32
|
df['西暦年月'] = df['基準日'].apply(conv)
|
37
33
|
print(df)
|
38
34
|
# 基準日 西暦年月
|
@@ -40,4 +36,6 @@
|
|
40
36
|
#1 2018-05-11 2018年5月
|
41
37
|
#2 平成30年09月 2018年9月
|
42
38
|
#3 昭和47年12月 1972年12月
|
39
|
+
|
40
|
+
|
43
41
|
```
|