Pandas在时间日期处理的应用

Pandas在时间日期上的应用十分方便,能够快速处理、生成数据,这里总结其部分应用。
常用类型:

常用方法
Timestamp(时间戳) to_datetime / date_range
Timedelta(时间间隔) to_timedelta / timedelta_range
Period(时间段) Period / period_range
DateOffset(时间偏移) DateOffset
1. Timestamp

Timestamp是从标准库的datetime类继承过来,可以参考datetime的应用。

  • 调用pd.Timestamp生成时间戳
    应用datetime函数,可直接将datetime换成Timestamp,如datetime.now → pd.Timestamp.now
>>> pd.Timestamp.now() #生成当前时间
Timestamp('2019-01-31 19:58:18.282574')
>>> pd.Timestamp(2017, 1, 1, 12)
Timestamp('2017-01-01 12:00:00')
>>> a = pd.Timestamp("2019-02-01")
>>> a.day
1
  • 调用to_datetime()转换为时间戳

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)


  • 解释:
    • error: {‘ignore’, ‘raise’, ‘coerce’},默认为‘raise’,无效值则显示异常;ignore,无效值则为NaT;coerce,无效值则返回输入值。
    • dayfirst / yearfirst:eg 10/11/12,dayfirst为日在前,yearfirst为年在前。
    • box: 布尔型,True则返回DatetimeIndex或者行索引,False则返回numpy.darry
    • format:%Y, %y, %m, %d ----年月日, %H(24时制), %I(12时制), %M, %S ----时分秒, %w星期几, %F(%Y-%m-%d简写), %D(%m/%d/%y简写)。
    • unit: (D,s,ms,us,ns) 计算时间D=day, s=second, ms=毫秒, us=微秒, ns=纳秒,根据起始时间计算增加时间,起始时间origin = "unix"(1970-01-01),eg:pd.to_datetime(60,unit = "s")为1970-01-01 00:01:00
    • origin: 默认unix,起始时间为1970-01-01;
    • infer_datetime_format: 默认为False,如果为True且没给出格式,则自动转换为日期类型,可以转换的情况下能够提升速度至5~10倍。

>>> pd.to_datetime("11/10/12",format="%y/%m/%d")
Timestamp('2011-10-12 00:00:00')
>>> 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]
  • date_range()生成时间戳索引

date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)


  • 解释
    • start, end: 起始时间、结束时间
    • period: 整数,周期数
    • freq: 频率字符串,默认为D
    • tz: 时区,如‘Asia/Hong_Kong’
    • closed: {None, ‘left’, ‘right’}, 选择起始、结束时间为开区间,none为全闭,left为左开右闭。
      ps:start, end, period, freq四个参数必须指定三个参数,同样的还有timedelta_range。

>>> pd.date_range("2001-01-01", periods=10, freq='2h20min')
DatetimeIndex(['2001-01-01 00:00:00', '2001-01-01 02:20:00',
               '2001-01-01 04:40:00', '2001-01-01 07:00:00',
               '2001-01-01 09:20:00', '2001-01-01 11:40:00',
               '2001-01-01 14:00:00', '2001-01-01 16:20:00',
               '2001-01-01 18:40:00', '2001-01-01 21:00:00'],
              dtype='datetime64[ns]', freq='140T')

date_range生成的为DatetimeIndex,可以转化为Series或者DataFrame

>>> pd.Series(pd.date_range("2001-01-01", periods=5, freq='2h20min'))
0   2001-01-01 00:00:00
1   2001-01-01 02:20:00
2   2001-01-01 04:40:00
3   2001-01-01 07:00:00
4   2001-01-01 09:20:00
dtype: datetime64[ns]

转化为DataFrame不设定列索引则默认为0

>>> pd.DataFrame({"time":pd.date_range("2001-01-01", periods=5, freq='2h20min')},index=list("abcde"))
time
a   2001-01-01 00:00:00
b   2001-01-01 02:20:00
c   2001-01-01 04:40:00
d   2001-01-01 07:00:00
e   2001-01-01 09:20:00

2.Timedelta

Timedelta相当于Python的datetime.timedelta

  • 调用to_timedelta()转换为时间间隔,time表示两个datetime的时间差

to_timedelta(arg, unit='ns', box=True, errors='raise')


  • 解释
    • unit: 字符串,可以用到(‘Y’, ‘M’, ‘W’, ‘D’, ‘days’, ‘day’, ‘hours’, hour’, ‘hr’, ‘h’, ‘m’, ‘minute’, ‘min’, ‘minutes’, ‘T’, ‘S’, ‘seconds’, ‘sec’, ‘second’, ‘ms’, ‘milliseconds’, ‘millisecond’, ‘milli’, ‘millis’, ‘L’, ‘us’, ‘microseconds’, ‘microsecond’, ‘micro’, ‘micros’, ‘U’, ‘ns’, ‘nanoseconds’, ‘nano’, ‘nanos’, ‘nanosecond’, ‘N’)
    • box: 布尔型,True则返回 Timedelta/TimedeltaIndex,False则返回timedelta64[ns]类型的numpy.darry ,或者返回numpy.timedelta64
>>> >>> pd.to_timedelta('1 days 06:05:01.00003')
Timedelta('1 days 06:05:01.000030')
#box的区别
>>> pd.to_timedelta([2,4,6,8,9], unit='d')
TimedeltaIndex(['2 days', '4 days', '6 days', '8 days', '9 days'], dtype='timedelta64[ns]', freq=None)
>>> pd.to_timedelta([2,4,6,8,9],unit= "D",box=False)
array([172800000000000, 345600000000000, 518400000000000, 691200000000000,
       777600000000000], dtype='timedelta64[ns]')
  • timedelta_range()生成时间间隔索引

timedelta_range(start=None, end=None, periods=None, freq=None, name=None, closed=None)

>>> pd.timedelta_range(start='1 day', periods=4)
TimedeltaIndex(['1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq='D')
3.Period
  • 调用period_range()生成period索引

period_range(start=None, end=None, periods=None, freq=None, name=None)

ps: start, end, periods三个参数必须指定两个参数

>>> pd.period_range(start='2018-01-01', end='2019-01-01', freq='M')
PeriodIndex(['2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06',
             '2018-07', '2018-08', '2018-09', '2018-10', '2018-11', '2018-12',
             '2019-01'],
            dtype='period[M]', freq='M')
4.DateOffset
  • 调用DateOffset()时间偏移

DateOffset(n=1, normalize=False, **kwds)


  • 解释:
    • n: 时间偏移量,默认n=1
    • normalize: 标准化,是否四舍五入,默认为False
    • **kwds: 偏移量参数,如Timedelta(years, months, weeks, days, hours, minutes, seconds, microseconds, nanoseconds),如替换偏移量的值(year, month, week, day, hour, minute, second, microsecond, nanosecond),注意两者区别,months=4为偏移4个月,month=4为四月份

>>> a = pd.Timestamp('2017-01-01 09:10:11')

>>> a + pd.DateOffset(month=3)
Timestamp('2017-03-01 09:10:11')

>>> a + pd.DateOffset(months=3)
Timestamp('2017-04-01 09:10:11')

接下来会分享怎样把Numpy和Pandas应用至具体案例中,关于Numpy的入门教程,有需要的会补充。

你可能感兴趣的:(Pandas在时间日期处理的应用)