Calculate ON/OFF duration

```zsh
ykishi@dezembro on2off_duration % cat create-dummy.sh   
#!/bin/sh

echo "when,status"

for i in $(seq 1 10); do
  TIME=`echo $((RANDOM%+10))`
  let TIME=${TIME}+1

  # echo "sleeping for ${TIME} secs ..."
  sleep ${TIME}

  div=2
  MOD=$(( i % div ))
  # echo ${MOD}

  case ${MOD} in
    1) value="off" ;;
    *) value="on" ;;
  esac

  printf "%s,%s\n" "$(date '+%Y-%m-%d %H:%M:%S'|tr -d '\n')" ${value}
done
ykishi@dezembro on2off_duration % cat work.csv          
when,status
2021-07-03 11:46:16,off
2021-07-03 11:46:24,on
2021-07-03 11:46:34,off
2021-07-03 11:46:43,on
2021-07-03 11:46:44,off
2021-07-03 11:46:51,on
2021-07-03 11:47:00,off
2021-07-03 11:47:09,on
2021-07-03 11:47:18,off
2021-07-03 11:47:20,on
ykishi@dezembro on2off_duration % cat on2off_duration.py
```
```Python
#!/usr/bin/env python3

import pandas as pd

def dump_info(df):
  print(df)
  print(df.dtypes)
  print()
  return

# =========================================================================================================
# CSVファイルを読み込む
# =========================================================================================================
csv_filename = "./work.csv"
df = pd.read_csv(csv_filename)
dump_info(df)

# =========================================================================================================
# whenの列をdatetime型に変換する
# =========================================================================================================
df['when'] = pd.to_datetime(df['when'])
dump_info(df)

# =========================================================================================================
# whenの各セルの差分を得る
# =========================================================================================================
df['duration'] = df['when'].diff()
dump_info(df)

# =========================================================================================================
# statusがoffのものだけ抽出する(onの連続、offの連続があった場合のケースは別途クレンジング編で対応する予定)
# =========================================================================================================
df = df[df['status'] == 'off']
dump_info(df)

# =========================================================================================================
# durationがNaTのものを判定する列を追加
# =========================================================================================================
df['judge'] = df['duration'].apply(lambda _: str(_) == 'NaT')
dump_info(df)

# =========================================================================================================
# durationがNaTの行を削除する
# =========================================================================================================
df = df[df['judge'] == False]
dump_info(df)

```
```
ykishi@dezembro on2off_duration % ./on2off_duration.py  
                  when status
0  2021-07-03 11:46:16    off
1  2021-07-03 11:46:24     on
2  2021-07-03 11:46:34    off
3  2021-07-03 11:46:43     on
4  2021-07-03 11:46:44    off
5  2021-07-03 11:46:51     on
6  2021-07-03 11:47:00    off
7  2021-07-03 11:47:09     on
8  2021-07-03 11:47:18    off
9  2021-07-03 11:47:20     on
when      object
status    object
dtype: object

                 when status
0 2021-07-03 11:46:16    off
1 2021-07-03 11:46:24     on
2 2021-07-03 11:46:34    off
3 2021-07-03 11:46:43     on
4 2021-07-03 11:46:44    off
5 2021-07-03 11:46:51     on
6 2021-07-03 11:47:00    off
7 2021-07-03 11:47:09     on
8 2021-07-03 11:47:18    off
9 2021-07-03 11:47:20     on
when      datetime64[ns]
status            object
dtype: object

                 when status        duration
0 2021-07-03 11:46:16    off             NaT
1 2021-07-03 11:46:24     on 0 days 00:00:08
2 2021-07-03 11:46:34    off 0 days 00:00:10
3 2021-07-03 11:46:43     on 0 days 00:00:09
4 2021-07-03 11:46:44    off 0 days 00:00:01
5 2021-07-03 11:46:51     on 0 days 00:00:07
6 2021-07-03 11:47:00    off 0 days 00:00:09
7 2021-07-03 11:47:09     on 0 days 00:00:09
8 2021-07-03 11:47:18    off 0 days 00:00:09
9 2021-07-03 11:47:20     on 0 days 00:00:02
when         datetime64[ns]
status               object
duration    timedelta64[ns]
dtype: object

                 when status        duration
0 2021-07-03 11:46:16    off             NaT
2 2021-07-03 11:46:34    off 0 days 00:00:10
4 2021-07-03 11:46:44    off 0 days 00:00:01
6 2021-07-03 11:47:00    off 0 days 00:00:09
8 2021-07-03 11:47:18    off 0 days 00:00:09
when         datetime64[ns]
status               object
duration    timedelta64[ns]
dtype: object

                 when status        duration  judge
0 2021-07-03 11:46:16    off             NaT   True
2 2021-07-03 11:46:34    off 0 days 00:00:10  False
4 2021-07-03 11:46:44    off 0 days 00:00:01  False
6 2021-07-03 11:47:00    off 0 days 00:00:09  False
8 2021-07-03 11:47:18    off 0 days 00:00:09  False
when         datetime64[ns]
status               object
duration    timedelta64[ns]
judge                  bool
dtype: object

                 when status        duration  judge
2 2021-07-03 11:46:34    off 0 days 00:00:10  False
4 2021-07-03 11:46:44    off 0 days 00:00:01  False
6 2021-07-03 11:47:00    off 0 days 00:00:09  False
8 2021-07-03 11:47:18    off 0 days 00:00:09  False
when         datetime64[ns]
status               object
duration    timedelta64[ns]
judge                  bool
dtype: object

ykishi@dezembro on2off_duration % 
```