LoginSignup
0
0

More than 1 year has passed since last update.

pandasでExcelのシリアル値をdatetime型に変換する

Last updated at Posted at 2021-07-03

timedeltaを使わなくてもできると知ったのでメモ。

import pandas as pd

df = pd.DataFrame({'date': [44378, 44379, 44380]})
df['date'] = pd.to_datetime(df['date'], unit='D', origin='1899/12/30')

print(df['date'])
出力
0   2021-07-01
1   2021-07-02
2   2021-07-03
Name: date, dtype: datetime64[ns]

1900/3/1より前の日付がある場合は

df = pd.DataFrame({'date': [1, 59, 61]})
df[df['date'] < 61] += 1
df['date'] = pd.to_datetime(df['date'], unit='D', origin='1899/12/30')

print(df['date'])
出力
0   1900-01-01
1   1900-02-28
2   1900-03-01
Name: date, dtype: datetime64[ns]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0