Pandas中的to_datetime函数用法
import datetime
import pandas as pd
import numpy as np
将字符串转换为日期时间:
pd.to_datetime('2023-09-06')
Timestamp('2023-09-06 00:00:00')
将多个字符串转换为日期时间:
pd.to_datetime(['2023-09-06', '2023-09-07', '2023-09-08'])
DatetimeIndex(['2023-09-06', '2023-09-07', '2023-09-08'], dtype='datetime64[ns]', freq=None)
处理缺失值,将不合法的日期转换为NaT(Not a Timestamp):
pd.to_datetime(['2023-09-06', '2023-09-07', 'invalid_date', '2023-09-08'], errors='coerce')
DatetimeIndex(['2023-09-06', '2023-09-07', 'NaT', '2023-09-08'], dtype='datetime64[ns]', freq=None)
指定日期时间格式:
pd.to_datetime('06/09/23 12:34:56', format='%d/%m/%y %H:%M:%S')
Timestamp('2023-09-06 12:34:56')
处理时间戳(Unix时间戳):
pd.to_datetime(1630899296, unit='s')
Timestamp('2021-09-06 03:34:56')
处理多个日期列,生成DataFrame:
data = {'date1': ['2023-09-06', '2023-09-07', '2023-09-08'],
'date2': ['2023-09-09', '2023-09-10', '2023-09-11']}
df = pd.DataFrame(data)
df['date1'] = pd.to_datetime(df['date1'])
df['date2'] = pd.to_datetime(df['date2'])
df['date1']
0 2023-09-06
1 2023-09-07
2 2023-09-08
Name: date1, dtype: datetime64[ns]
df['date2']
0 2023-09-09
1 2023-09-10
2 2023-09-11
Name: date2, dtype: datetime64[ns]
从DataFrame的多个列中组合日期时间
参数可以是常见的缩写,例如['year','month','day','minute','second','ms','us','ns'],或者是它们的复数形式。
df = pd.DataFrame({'year': [2015, 2016],
'month': [2, 3],
'day': [4, 5]})
df
year | month | day | |
---|---|---|---|
0 | 2015 | 2 | 4 |
1 | 2016 | 3 | 5 |
pd.to_datetime(df)
0 2015-02-04
1 2016-03-05
dtype: datetime64[ns]
标签:06,df,09,用法,pd,2023,datetime,Pandas
From: https://www.cnblogs.com/itelephant/p/17681754.html