课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
10分钟

时间点 Timestamp

时间点:Timestamp对象从Pythondatetime类继承,它表示时间轴上的一个点。

  pd.Timestamp(ts_input=<object object at 0x0000000001E8F340>, freq=None, tz=None, 
  unit=None, year=None, month=None, day=None, hour=None, minute=None, 
  second=None, microsecond=None, tzinfo=None, offset=None)

参数:

  • ts_input:一个datetime-like/str/int/float,该值将被转换成Timestamp
  • freq:一个字符串或者DateOffset,给出了偏移量
  • tz:一个字符串或者pytz.timezone对象,给出了时区
  • unit:一个字符串。当ts_input为整数或者浮点数时,给出了转换单位
  • offset:废弃的,推荐使用freq
  • 其他的参数来自于datetime.datetime。它们要么使用位置参数,要么使用关键字参数,但是不能混用

属性有:

  • year/month/day/hour/minute/second/microsecond/nanosecond,这些属性都为整数
  • tzinfo:时区信息(默认为None),它是一个datetime.tzinfo对象
  • dayofweek/dayofyear/days_in_mounth/freqstr/quarter/weekofyear/...
  • value:保存的是UTC时间戳(自UNIX纪元1970年1月1日以来的纳秒数),该值在时区转换过程中保持不变

类方法有:

  • combine(date, time):通过datetime.datedatetime.time创建一个Timestamp
  • fromtimestamp(ts):通过时间戳创建一个Timestamp
  • now(tz=None):创建一个指定时区的当前时间。
  • doday(tz=None):创建一个指定时区的当前时间。
  • utcfromtimestamp(ts):从时间戳创建一个UTC Timestamp,其tzinfo=None
  • utcnow():创建一个当前的UTC Timestamp,其tzinfo=UTC

方法有:

  • .astimezone(tz)/.tz_convert(tz):将一个tz-aware Timestamp转换时区
  • .isoformat(sep='T'):返回一个ISO 8601格式的字符串。
  • .normalize():将Timestamp调整到午夜(保留tzinfo
  • replace(**kwds):调整对应值,返回一个新对象
  • .to_period(self, freq=None):返回一个Period对象`
  • .tz_localize(self, tz, ambiguous='raise', errors='raise'):将一个tz-naive Timestamp ,利用tz转换为一个tz-aware Timestamp
  • .to_pydatetime(...):转换为python datetime对象
  • .to_datetime64(...):转换为numpy.datetime64对象
  • datetime.date/datetime.datetime继承而来的方法

默认情况下,pands中的Timestamptz-naive,即tz字段为NoneTimestamp提供了方便的时区转换功能。如果tz非空,则是tz-aware Timestamp。不同时区的时间可以比较,但是naive Timestamplocalized Timestamp无法比较。

Timestamp的减法,要求两个Timestamp要么都是同一个时区下,要么都是tz-naive的。