pandas 的日期/时间类型有如下几种:
Concept
Scalar Class
Array Class
pandas Data Type
Primary Creation Method
Date times
Timestamp
DatetimeIndex
datetime64[ns] or datetime64[ns, tz]
to_datetime or date_range
Time deltas
Timedelta
TimedeltaIndex
timedelta64[ns]
to_timedelta or timedelta_range
Time spans
Period
PeriodIndex
period[freq]
Period or period_range
Date offsets
DateOffset
None
None
DateOffset
本文介绍在处理时点数 (point in time) 一些常用的处理方法,仍然以上一篇的示例数据为例进行讲解。pandas 用 Timestamp 表示时点数,在大多数情况下和 python 的 datetime 类型的使用方法是通用的。
首先获取数据,并且将 DataFrame 的 date 列转换成 datetime 类型:
df1 = pd.read_csv('https://raw.githubusercontent.com/stonewm/python-practice-projects/master/pandas%20sample%20dat