pandas中Timestamp类用法详解

yipeiwu_com5年前Python基础

由于网上关于Timestamp类的资料比较少,而且官网上面介绍的很模糊,本文只是对如何创建Timestamp类对象进行简要介绍,详情请读者自行查阅文档

以下有两种方式可以创建一个Timestamp对象:

1. Timestamp()的构造方法

import pandas as pd
from datetime import datetime as dt
p1=pd.Timestamp(2017,6,19)
p2=pd.Timestamp(dt(2017,6,19,hour=9,minute=13,second=45))
p3=pd.Timestamp("2017-6-19 9:13:45")

print("type of p1:",type(p1))
print(p1)
print("type of p2:",type(p2))
print(p2)
print("type of p3:",type(p3))
print(p3)

输出:

('type of p1:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 00:00:00
('type of p2:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 09:13:45
('type of p3:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 09:13:45

2. to_datetime()方法

import pandas as pd
from datetime import datetime as dt

p4=pd.to_datetime("2017-6-19 9:13:45")
p5=pd.to_datetime(dt(2017,6,19,hour=9,minute=13,second=45))

print("type of p4:",type(p4))
print(p4)
print("type of p5:",type(p5))
print(p5)

输出:

('type of p4:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 09:13:45
('type of p5:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 09:13:45

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python简单操作sqlite3的方法示例

本文实例讲述了Python简单操作sqlite3的方法。分享给大家供大家参考,具体如下: import sqlite3 def Test1(): #con =sqlite3.co...

完美解决python3.7 pip升级 拒绝访问问题

完美解决python3.7 pip升级 拒绝访问问题

python3.7 pip升级 拒绝访问 解决方案 pip install --upgrade pip --user ps:下面看下python中的for循环加强 #先执行外...

深入理解Python中range和xrange的区别

【听图阁-专注于Python设计】已经给大家介绍过range和xrange的区别的基础知识,有兴趣的朋友可以参阅:  python中xrange和range的区别 python...

Python调用C语言的实现

Python中的ctypes模块可能是Python调用C方法中最简单的一种。ctypes模块提供了和C语言兼容的数据类型和函数来加载dll文件,因此在调用时不需对源文件做任何的修改。也正...

Python 监测文件是否更新的方法

主要逻辑是判断文件的最后修改时间与创建时间是否在秒级别上一致,此代码适用于Python 2. import time import os #Read fime name FileN...