6
6

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 3 years have passed since last update.

Pandasのpd.to_datetimeで時刻のみ抜き出す方法

Last updated at Posted at 2020-05-19

Pythonを勉強してたところ、pd.to_datetimeで時刻だけ抜き出す方法が見つからなかったので記載

問題点

Pandasのpd.to_datetimeでは文字をTimestampにできるが

[コード]
s = pd.Series(["20:00", "21:00"])
t = pd.to_datetime(s, format='%H:%M')
t
[出力]
0   1900-01-01 20:00:00
1   1900-01-01 21:00:00

そのまま日付だけを変換すると1900-01-01という日付までついてくる

解決策

.dt.timeをつける

[コード]
s = pd.Series(["20:00", "21:00"])
t = pd.to_datetime(s, format='%H:%M').dt.time
t
[出力]
0    20:00:00
1    21:00:00
6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?