回答編集履歴
1
コード修正
test
CHANGED
@@ -2,19 +2,23 @@
|
|
2
2
|
```Python
|
3
3
|
import pandas as pd
|
4
4
|
|
5
|
-
A = pd.DataFrame({'
|
5
|
+
A = pd.DataFrame({'st':['2022-07-01 01:02:03'], 'ed':['2022-07-02 02:03:04']})
|
6
|
-
A['
|
6
|
+
A['st'] = pd.to_datetime(A['st'])
|
7
|
+
A['ed'] = pd.to_datetime(A['ed'])
|
7
8
|
|
8
|
-
def get_t
|
9
|
+
def get_attr(sr, attr):
|
9
|
-
|
10
|
+
return sr.apply( lambda x: getattr(x, attr))
|
10
11
|
|
12
|
+
cols = []
|
11
|
-
|
13
|
+
for col in ['st','ed']:
|
14
|
+
for attr in ['month','day','hour']:
|
12
|
-
|
15
|
+
dt_col = f'{col}_{attr}'
|
13
|
-
get_t
|
16
|
+
A[dt_col] = get_attr( A[col], attr)
|
17
|
+
cols.append(dt_col)
|
14
18
|
|
15
|
-
print(A)
|
19
|
+
print(A[cols])
|
20
|
+
# st_month st_day st_hour ed_month ed_day ed_hour
|
16
|
-
#
|
21
|
+
#0 7 1 1 7 2 2
|
17
|
-
#0 2022-07-01 01:02:03 2022 7 1
|
18
22
|
```
|
19
23
|
|
20
24
|
|