timestamp.astype(np.int64)
と pd.to_datetime(unix_timestamp_index)
で相互変換できる。
データ準備
date_index = pd.date_range(start="2022-08-31 10:00:00", end="2022-08-31 11:00:00", freq="1S")
DatetimeIndex(['2022-08-31 10:00:00', '2022-08-31 10:00:01',
'2022-08-31 10:00:02', '2022-08-31 10:00:03',
'2022-08-31 10:00:04', '2022-08-31 10:00:05',
'2022-08-31 10:00:06', '2022-08-31 10:00:07',
'2022-08-31 10:00:08', '2022-08-31 10:00:09',
...
'2022-08-31 10:59:51', '2022-08-31 10:59:52',
'2022-08-31 10:59:53', '2022-08-31 10:59:54',
'2022-08-31 10:59:55', '2022-08-31 10:59:56',
'2022-08-31 10:59:57', '2022-08-31 10:59:58',
'2022-08-31 10:59:59', '2022-08-31 11:00:00'],
dtype='datetime64[ns]', length=3601, freq='S')
pd.Timestamp -> unix timestamp
unix_timestamp_index = date_index.astype(np.int64)
Int64Index([1661940000000000000, 1661940001000000000, 1661940002000000000,
1661940003000000000, 1661940004000000000, 1661940005000000000,
1661940006000000000, 1661940007000000000, 1661940008000000000,
1661940009000000000,
...
1661943591000000000, 1661943592000000000, 1661943593000000000,
1661943594000000000, 1661943595000000000, 1661943596000000000,
1661943597000000000, 1661943598000000000, 1661943599000000000,
1661943600000000000],
dtype='int64', length=3601)
unix timestamp -> pd.Timestamp
date_time_reconstruction = pd.to_datetime(unix_timestamp_index)
DatetimeIndex(['2022-08-31 10:00:00', '2022-08-31 10:00:01',
'2022-08-31 10:00:02', '2022-08-31 10:00:03',
'2022-08-31 10:00:04', '2022-08-31 10:00:05',
'2022-08-31 10:00:06', '2022-08-31 10:00:07',
'2022-08-31 10:00:08', '2022-08-31 10:00:09',
...
'2022-08-31 10:59:51', '2022-08-31 10:59:52',
'2022-08-31 10:59:53', '2022-08-31 10:59:54',
'2022-08-31 10:59:55', '2022-08-31 10:59:56',
'2022-08-31 10:59:57', '2022-08-31 10:59:58',
'2022-08-31 10:59:59', '2022-08-31 11:00:00'],
dtype='datetime64[ns]', length=3601, freq=None)