回答編集履歴

2

2023/03/29 07:34

投稿

melian
melian

スコア19769

test CHANGED
@@ -4,6 +4,7 @@
4
4
  from datetime import timedelta
5
5
 
6
6
  df = pd.read_csv('data.csv', parse_dates=['Time'])
7
+ cols = df.columns
7
8
 
8
9
  # 1/13 0:00:00~1/15 23:55:00 と 2/13 0:00:00~2/15 23:55:00 のインデックス(5分毎)を作成
9
10
  first_day, last_day = df['Time'].min().date(), df['Time'].max().date() + timedelta(days=1)
@@ -11,14 +12,13 @@
11
12
  time_idx = time_idx.union(pd.date_range('2023-02-15', last_day, freq='5min', inclusive='left'))
12
13
 
13
14
  # reset datetime index
14
- dfx = df.set_index('Time').reindex(time_idx).rename_axis('Time').reset_index()
15
+ dfx = df.set_index('Time').reindex(time_idx).rename_axis('Time').reset_index()[cols]
15
16
 
16
17
  # fullfill NaN
17
18
  dfx['A'] = dfx['A'].fillna('A').map('"{}"'.format)
18
19
  dfx['num'] = dfx['num'].fillna(0, downcast='infer')
19
20
 
20
21
  # save to CSV file
21
- dfx = dfx[['A', 'Time', 'num']]
22
22
  dfx.to_csv('filled_data.csv', index=False, quoting=3)
23
23
  ```
24
24
 

1

2023/03/29 07:23

投稿

melian
melian

スコア19769

test CHANGED
@@ -1,35 +1,24 @@
1
- Pandas使う場合。
1
+ > 1/13の0:00:00~1/15の23:55:00・2/13の0:00:00~2/15の23:55:00と、この期間内であれば全ての時間帯での5分おきのデータ作成したい
2
2
  ```python
3
3
  import pandas as pd
4
+ from datetime import timedelta
4
5
 
5
6
  df = pd.read_csv('data.csv', parse_dates=['Time'])
6
7
 
8
+ # 1/13 0:00:00~1/15 23:55:00 と 2/13 0:00:00~2/15 23:55:00 のインデックス(5分毎)を作成
7
- dfx = df.set_index('Time').resample('5min').last().iloc[::-1].reset_index()
9
+ first_day, last_day = df['Time'].min().date(), df['Time'].max().date() + timedelta(days=1)
8
- dfx = dfx.query('Time < "2023-01-16" or Time >= "2023-02-13"')
10
+ time_idx = pd.date_range(first_day, '2023-01-16', freq='5min', inclusive='left')
11
+ time_idx = time_idx.union(pd.date_range('2023-02-15', last_day, freq='5min', inclusive='left'))
9
12
 
13
+ # reset datetime index
14
+ dfx = df.set_index('Time').reindex(time_idx).rename_axis('Time').reset_index()
15
+
16
+ # fullfill NaN
10
- dfx['A'] = dfx['A'].ffill().map('"{}"'.format)
17
+ dfx['A'] = dfx['A'].fillna('A').map('"{}"'.format)
11
18
  dfx['num'] = dfx['num'].fillna(0, downcast='infer')
19
+
20
+ # save to CSV file
12
21
  dfx = dfx[['A', 'Time', 'num']]
13
22
  dfx.to_csv('filled_data.csv', index=False, quoting=3)
14
23
  ```
15
24
 
16
- **filled_data.csv**
17
- ```csv
18
- A,Time,num
19
- "A",2023-02-15 23:55:00,1
20
- "A",2023-02-15 23:50:00,2
21
- "A",2023-02-15 23:45:00,0
22
- "A",2023-02-15 23:40:00,3
23
- "A",2023-02-15 23:35:00,0
24
- "A",2023-02-15 23:30:00,2
25
- "A",2023-02-15 23:25:00,1
26
- "A",2023-02-15 23:20:00,0
27
- "A",2023-02-15 23:15:00,0
28
- "A",2023-02-15 23:10:00,1
29
- "A",2023-02-15 23:05:00,0
30
- "A",2023-02-15 23:00:00,4
31
- "A",2023-02-15 22:55:00,2
32
- "A",2023-02-15 22:50:00,0
33
- "A",2023-02-15 22:45:00,1
34
- ```
35
-