pandas.Timestamp#

class pandas.Timestamp(ts_input=<object object>, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None, *, nanosecond=None, tz=None, unit=None, fold=None)#

Python datetime.datetime 对象的 Pandas 替代。

Timestamp 是 Python 的 Datetime 的 Pandas 等价物,在大多数情况下可以与之互换。它是构成 DatetimeIndex 和 Pandas 中其他时间序列定向数据结构的项目所使用的类型。

Parameters:
ts_inputdatetime-like, str, int, float

要转换为Timestamp的值。

year, month, dayint
hour, minute, second, microsecondint, optional, default 0
tzinfodatetime.tzinfo,可选,默认为 None
nanosecondint, optional, default 0
tzstr, pytz.timezone, dateutil.tz.tzfile 或 None

Timestamp 将具有的时间的时区。

unitstr

如果 ts_input 的类型为 int 或 float,则用于转换的单位。有效值为 ‘D’、’h’、’m’、’s’、’ms’、’us’ 和 ‘ns’。例如,’s’ 表示秒,’ms’ 表示毫秒。

对于浮点输入,结果将以纳秒为单位存储,并且 unit 属性将设置为 'ns'

fold{0, 1}, 默认为 None,仅关键字参数

由于夏令时,在从夏令时切换到冬令时时,一个时钟时间可能会出现两次;fold 描述了 datetime-like 对应于时钟第一次(0)或第二次(1)到达模糊时间。

Notes

构造函数本质上有三种调用约定。主要形式接受四个参数。它们可以通过位置或关键字参数传递。

另外两种形式模仿了 datetime.datetime 的参数。它们可以通过位置或关键字参数传递,但不能混合使用 keduanya。

Examples

使用主要调用约定:

这会将 datetime-like 字符串转换为

>>> pd.Timestamp('2017-01-01T12')
Timestamp('2017-01-01 12:00:00')

这会将表示 Unix 纪元的浮点数(单位为秒)转换为

>>> pd.Timestamp(1513393355.5, unit='s')
Timestamp('2017-12-16 03:02:35.500000')

这会将表示 Unix 纪元的整数(单位为秒)和特定时区的转换为

>>> pd.Timestamp(1513393355, unit='s', tz='US/Pacific')
Timestamp('2017-12-15 19:02:35-0800', tz='US/Pacific')

使用模仿 datetime.datetime API 的另外两种形式:

>>> pd.Timestamp(2017, 1, 1, 12)
Timestamp('2017-01-01 12:00:00')
>>> pd.Timestamp(year=2017, month=1, day=1, hour=12)
Timestamp('2017-01-01 12:00:00')

Attributes

asm8 

返回 numpy datetime64 格式(以纳秒为单位)。

day 

day_of_week 

返回星期几。

day_of_year 

返回一年中的第几天。

dayofweek 

返回星期几。

dayofyear 

返回一年中的第几天。

days_in_month 

返回月份中的天数。

daysinmonth 

返回月份中的天数。

fold 

hour 

is_leap_year 

如果年份是闰年,则返回 True。

is_month_end 

检查日期是否为月份的最后一天。

is_month_start 

检查日期是否为月份的第一天。

is_quarter_end 

检查日期是否为季度的最后一天。

is_quarter_start 

检查日期是否为季度的第一天。

is_year_end 

如果日期是该年的最后一天,则返回 True。

is_year_start 

如果日期是该年的第一天,则返回 True。

max 

microsecond 

min 

minute 

month 

nanosecond 

quarter 

返回一年中的季度。

resolution 

second 

tz 

tzinfo 的别名。

tzinfo 

unit 

与 self._creso 关联的缩写。

value 

week 

返回一年中的周数。

weekofyear 

返回一年中的周数。

year 

Methods

as_unit (unit[, round_ok])

将底层的 int64 表示转换为给定的单位。

astimezone (tz)

将时区感知的 Timestamp 转换为另一个时区。

ceil (freq[, ambiguous, nonexistent])

返回一个新的 Timestamp,其值向上取整到此分辨率。

combine (date, time)

组合日期和时间,得到具有相同日期和时间字段的 datetime。

ctime ()

返回 ctime() 风格的字符串。

date ()

返回具有相同年、月、日的 date 对象。

day_name ([locale])

返回具有指定区域设置的 Timestamp 的日期名称。

dst ()

返回夏令时 (DST) 调整。

floor (freq[, ambiguous, nonexistent])

返回一个新的 Timestamp,其值向下取整到此分辨率。

fromisocalendar 

int, int, int -> 使用 ISO 年、周数和星期几构造日期。

fromisoformat

string -> 从大多数 ISO 8601 格式的字符串解析日期时间。

fromordinal (ordinal[, tz])

从序数(proleptic Gregorian ordinal)构造时间戳。

fromtimestamp (ts)

根据 POSIX 时间戳将 timestamp[, tz] 转换为 tz 的本地时间。

isocalendar ()

返回一个命名元组,包含 ISO 年、周数和星期几。

isoformat ([sep[, timespec]])

按 ISO 8601 格式返回时间。

isoweekday ()

返回日期所代表的星期几。

month_name ([locale])

使用指定的 locale 返回 Timestamp 的月份名称。

normalize ()

将 Timestamp 规范化到午夜,保留 tz 信息。

now ([tz])

返回新的 Timestamp 对象,表示 tz 的当前本地时间。

replace ([year[, month[, day[, hour[, minute[, ...]]]]])

实现 datetime.replace,处理纳秒。

round (freq[, ambiguous[, nonexistent]])

将 Timestamp 四舍五入到指定的精度。

strftime (format)

返回 Timestamp 的格式化字符串。

strptime (string, format)

函数未实现。

time ()

返回时间对象,具有相同的时间,但 tzinfo=None。

timestamp ()

返回 POSIX 时间戳(float 类型)。

timetuple ()

返回时间元组,与 time.localtime() 兼容。

timetz ()

返回时间对象,具有相同的时间和 tzinfo。

to_datetime64 ()

返回具有相同精度的 numpy.datetime64 对象。

to_julian_date ()

将 TimeStamp 转换为儒略日。

to_numpy ([dtype[, copy]])

将 Timestamp 转换为 NumPy datetime64。

to_period ([freq])

返回此时间戳是其观测值的周期。

to_pydatetime ([warn])

将 Timestamp 对象转换为本地 Python datetime 对象。

today ([tz])

返回本地时区的当前时间。

toordinal ()

返回序数(proleptic Gregorian ordinal)。

tz_convert (tz)

将时区感知的 Timestamp 转换为另一个时区。

tz_localize (tz[, ambiguous[, nonexistent]])

将 Timestamp 本地化到某个时区。

tzname ()

返回时区名称。

utcfromtimestamp (ts)

从 POSIX 时间戳构造一个时区感知的 UTC 日期时间。

utcnow ()

返回一个新的 Timestamp,表示 UTC 的日期和时间。

utcoffset ()

返回 UTC 偏移量。

utctimetuple ()

返回 UTC 时间元组,与 time.localtime() 兼容。

weekday ()

返回日期所代表的星期几。