美文网首页
02pandas——Timestamp

02pandas——Timestamp

作者: 转身丶即天涯 | 来源:发表于2020-08-17 15:22 被阅读0次

函数签名

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)

未完待续

相关文章

网友评论

      本文标题:02pandas——Timestamp

      本文链接:https://www.haomeiwen.com/subject/zfdqjktx.html