回答編集履歴

1

2024/02/21 16:33

投稿

melian
melian

スコア20675

test CHANGED
@@ -9,15 +9,16 @@
9
9
  '2020-01-01 12:00:00']
10
10
 
11
11
  s1 = pd.to_datetime(pd.Series(date_strs), format='%Y-%m-%d %H:%M:%S', errors='coerce')
12
- gid = s1.diff(-1).dt.total_seconds().eq(0).cumsum()
12
+ gid = (s1.notna() & s1.diff().dt.total_seconds().ne(0)).cumsum()
13
13
  gid += s1.groupby(gid, group_keys=False).transform(lambda s: s.isna().any())
14
14
 
15
15
  s2 = pd.to_datetime(pd.Series(date_strs), format='%Y-%m-%d %H:%M:%S %f', errors='coerce').dt.floor('s')
16
16
  s2 = s2.groupby(gid, group_keys=False)\
17
17
  .apply(lambda s:
18
- pd.date_range(
18
+ s1[s.index] if (idx := s.first_valid_index()) is None
19
- s[s.first_valid_index()] - (s.first_valid_index() - s.index[0]) * pd.Timedelta('3s'),
19
+ else pd.date_range(s[idx] - (idx - s.index[0]) * pd.Timedelta('3s'),
20
- freq='3s', periods=len(s)).to_series().set_axis(s.index))
20
+ freq='3s', periods=len(s)).to_series().set_axis(s.index))
21
+
21
22
 
22
23
  print(s2)
23
24