2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pandasでUTCの日時の文字列を読み込んで、JSTに変換する

Posted at

tz_convert()pandas.Series型に直接適用できないのがハマりポイント。

# datetime列に文字列で日時を登録する
df = pd.DataFrame([{"datetime":"2022-07-03 00:00:00"},{"datetime":"2022-07-03 01:00:00"},{"datetime":"2022-07-03 02:00:00"}]) 

# datetune列の内容をDatetime型かつaware(UTC)で、"datetime_utc"列に入れる 
df["datetime_utc"] = pd.to_datetime(df["datetime"], utc=True) 

# JSTに変換したデータを"datetime_jst"列に入れる
df["datetime_jst"] = df["datetime_utc"].dt.tz_convert('Asia/Tokyo')

書いてから気付きましたが、Python3のタイムゾーンの扱い的に'Asia/Tokyo'の部分は環境依存かもしれません。

参考にしたページ

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?