dt
时间类型格式
使用Series.dt访问器, 需要先将当前Series转换为datetimelike类型的格式, 在pandas中指的就是:
Datetime
Timedelta
Period
格式转化
Datetime
可以在IO时直接将字符串表示的时间格式, 通过一定的设置, 转换成Datetime的类型.
例如如下的数据格式:
data = pd.read_csv(file_path) data.head()Month #Passengers 0 1949-01 112 1 1949-02 118 2 1949-03 132 3 1949-04 129 4 1949-05 121其中的
Month列就符合%Y-%m的格式, 因此读取方法改为:data = pd.read_csv(file_path, parse_dates=["Month"], date_parser=lambda x: pd.datetime.strptime(x, "%Y-%m")) data.head()Month #Passengers 0 1949-01-01 112 1 1949-02-01 118 2 1949-03-01 132 3 1949-04-01 129 4 1949-05-01 121或者直接使用自动推断的方法, 更简单省事, 对于一般的格式都能很好的完成:
data = pd.read_csv(file_path, parse_dates=["Month"], infer_datetime_format=True)另外也可以使用pandas.to_datetime函数, 将指定的一列转为Datetime格式, 方法更灵活.
pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=False)其中比较重要的参数有:
arg: integer, float, string, datetime, list, tuple, 1-d array, Series
要转换的数据, 注意支持多种格式的数据
errors: {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’
遇到无法转换的脏数据如何处理
coerce方法将会对应返回NaT
format: string, default None
infer_datetime_format: boolean, default False
自动推断格式, 不需要给出
format
转换例子如下:
>>> df = pd.DataFrame({'year': [2015, 2016], 'month': [2, 3], 'day': [4, 5]}) >>> pd.to_datetime(df) 0 2015-02-04 1 2016-03-05 dtype: datetime64[ns]>>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore') datetime.datetime(1300, 1, 1, 0, 0) >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce') NaT>>> pd.to_datetime(1490195805, unit='s') Timestamp('2017-03-22 15:16:45') >>> pd.to_datetime(1490195805433502912, unit='ns') Timestamp('2017-03-22 15:16:45.433502912')>>> pd.to_datetime([1, 2, 3], unit='D', origin=pd.Timestamp('1960-01-01')) 0 1960-01-02 1 1960-01-03 2 1960-01-04
Timedelta
使用pandas.to_timedelta函数进行转化.
dt中的重要方法
Series.dt.date
返回一个元素格式为python中的datetime.date对象的numpy array
之后对于其中的每个元素就可以使用
datetime中的strftime等方法进行继续的操作
Series.dt.time
返回一个元素格式为python中的datetime.time对象的numpy array
Series.dt.year
Series.dt.month
Series.dt.day
Series.dt.hour
Series.dt.minute
Series.dt.second
Series.dt.microsecond
Series.dt.nanosecond
Series.dt.week / Series.dt.weekofyear
Series.dt.weekday / Series.dt.dayofweek
Monday=0, Sunday=6
Series.dt.quarter
最后更新于
这有帮助吗?