3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonの文字列型の型変換

Posted at

文字列型の日付を型変換

str_datetime = "26/10/2024 05:58:26 PM"

その1: datetimeを使用

str ⇒ datetime

import datetime

converted_datetime = datetime.datetime.strptime(str_datetime, "%d/%m/%Y %I:%M:%S %p")
print(converted_datetime)
print(type(converted_datetime))

# 2024-10-26 17:58:26
# <class 'datetime.datetime'>

その2:to_datetime()を使用

str ⇒ timestamps

# pip install pandas
import pandas as pd

converted_timestamp = pd.to_datetime(str_datetime, format="mixed", dayfirst=True)
print(converted_timestamp)
print(type(converted_timestamp))

# 2024-10-26 17:58:26
# <class 'pandas._libs.tslibs.timestamps.Timestamp'>

最後に

どこかミスがあれば教えてください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?