函数签名
class pandas.Timestamp(ts_input=<object object>, freq=None, tz=None, unit=None, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, nanosecond=None, tzinfo=None, *, fold=None)
说明
Pandas replacement for python datetime.datetime object.
Timestamp is the pandas equivalent of python’s Datetime and is interchangeable with it in most cases. It’s the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.
在pandas中,Timestamp类用于替换python中的Datetime类。由此可见,多数时间对象我们都可以参考python时间对象的处理方式。同时,也用于组成时间索引(DatatimeIndex)类。
# 1. 构造Timestamp
# ts_input参数支持4种格式,datetime-like, str, int, float
# 1.1 datetime-like
time_str = "2020-08-01 10:20:00"
time_dt = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
ts1 = pd.Timestamp(time_dt)
print(ts1)
# 1.2 str
ts2 = pd.Timestamp(time_str)
print(ts2)
# 1.3 int, 指的是时间戳
timestamp = time_dt.timestamp()
print(timestamp)
print(datetime.fromtimestamp(timestamp))
ts3 = pd.Timestamp(timestamp)
print(ts3)
# 1.4 float, float格式的时间戳
timestamp_f = time_dt.timestamp()
ts4 = pd.Timestamp(timestamp_f)
print(ts4)
未完待续
网友评论