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=<object object>, unit=None, fold=None)[源码]#

Pandas 替换 Python datetime.datetime 对象。

Timestamp 是 pandas 中与 Python 的 `datetime` 等效的类,在大多数情况下可以互换使用。它是构成 `DatetimeIndex` 以及 pandas 中其他时间序列相关数据结构的条目的类型。

参数:
ts_inputdatetime-like, str, int, float

要转换为 Timestamp 的值。

yearint

年份的值。

monthint

月份的值。

dayint

日期的值。

hourint, optional, default 0

小时的值。

minuteint, optional, default 0

分钟的值。

secondint, optional, default 0

秒的值。

microsecondint, optional, default 0

微秒的值。

tzinfodatetime.tzinfo, optional, default None

时区信息。

nanosecondint, optional, default 0

纳秒的值。

tzstr, zoneinfo.ZoneInfo, pytz.timezone, dateutil.tz.tzfile 或 None

Timestamp 所对应的时间的时区。

unitstr

当 `ts_input` 为 `int` 或 `float` 类型时用于转换的单位。有效值包括 ‘W’, ‘D’, ‘h’, ‘m’, ‘s’, ‘ms’, ‘us’, 和 ‘ns’。例如,‘s’ 表示秒,‘ms’ 表示毫秒。

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

fold{0, 1}, default None, keyword-only

由于夏令时(DST),在从夏令时切换到标准时时,同一个时钟时间可能会出现两次;`fold` 参数描述了该日期时间对应的是时钟第一次(0)还是第二次(1)到达这个模糊的时间点。

另请参阅

Timedelta

表示一个持续时间,即两个日期或时间之间的差值。

datetime.datetime

Python datetime.datetime 对象。

注意

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

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

示例

使用主要调用约定

这会将日期时间格式的字符串转换为 Timestamp

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

这会将一个代表 Unix epoch 的浮点数(单位为秒)转换为 Timestamp

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

这会将一个代表 Unix epoch 的整数(单位为周)转换为 Timestamp

>>> pd.Timestamp(1535, unit='W')
Timestamp('1999-06-03 00:00:00')

这会将一个代表 Unix epoch 的整数(单位为秒)转换为 Timestamp,并指定了特定的时区

>>> 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')

属性

asm8

返回具有相同精度的 NumPy datetime64 格式。

day

返回 Timestamp 的日期。

星期几

返回星期几。

一年中的第几天

返回一年中的第几天。

星期几

返回星期几。

一年中的第几天

返回一年中的第几天。

days_in_month

返回月份的天数。

daysinmonth

返回月份的天数。

fold

返回 Timestamp 的 fold 值。

hour

返回 Timestamp 的小时。

是否闰年

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

is_month_end

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

is_month_start

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

is_quarter_end

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

is_quarter_start

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

is_year_end

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

is_year_start

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

max

microsecond

返回 Timestamp 的微秒。

min

minute

返回 Timestamp 的分钟。

month

返回 Timestamp 的月份。

nanosecond

返回 Timestamp 的纳秒。

季度

返回 Timestamp 的季度。

resolution

second

返回 Timestamp 的秒。

tz

tzinfo 的别名。

tzinfo

返回 Timestamp 的时区信息。

unit

与 self._creso 相关联的缩写。

value

返回 Timestamp 的值。

week

返回一年中的周数。

weekofyear

返回一年中的周数。

year

返回 Timestamp 的年份。

Methods

as_unit(unit[, round_ok])

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

astimezone(tz)

将带时区 Timestamp 转换为另一个时区。

ceil(freq[, ambiguous, nonexistent])

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

combine(date, time)

将日期和时间合并为一个 Timestamp 对象。

ctime()

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

date()

返回具有相同年、月、日的 datetime.date

day_name([locale])

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

dst()

返回夏令时 (DST) 调整。

floor(freq[, ambiguous, nonexistent])

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

fromisocalendar

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

fromisoformat

string -> 从 ISO 8601 格式字符串构造 datetime 对象。

fromordinal(ordinal[, tz])

从前生公历序数构造时间戳。

fromtimestamp(ts[, tz])

从 POSIX 时间戳创建 Timestamp 对象。

isocalendar()

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

isoformat([sep, timespec])

按 ISO 8601 格式化时间。

isoweekday()

返回日期表示的星期几。

month_name([locale])

返回具有指定区域设置的 Timestamp 的月份名称。

normalize()

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

now([tz])

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

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

实现 datetime.replace,处理纳秒。

round(freq[, ambiguous, nonexistent])

将 Timestamp 四舍五入到指定的分辨率。

strftime(format)

返回 Timestamp 的格式化字符串。

strptime(date_string, format)

将字符串参数转换为日期时间。

time()

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

timestamp()

返回 POSIX 时间戳(浮点数)。

timetuple()

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

timetz()

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

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()

返回前生公历序数。

tz_convert(tz)

将带时区 Timestamp 转换为另一个时区。

tz_localize(tz[, ambiguous, nonexistent])

将 Timestamp 本地化到某个时区。

tzname()

返回时区名称。

utcfromtimestamp(ts)

从 POSIX 时间戳构造带时区的 UTC 日期时间。

utcnow()

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

utcoffset()

返回 UTC 偏移量。

utctimetuple()

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

weekday()

返回日期表示的星期几。