datetime,Timestamp和datetime64之间转换

引入工具包

1
2
3
import datetime
import numpy as np
import pandas as pd

 

总览 

1
2
3
from IPython.display import Image
from IPython.core.display import HTML
Image(url= "https://i.stack.imgur.com/uiXQd.png")

 

一.datetime库

datetime标准库有四个主要对象

时间 - 仅限时间,以小时,分钟,秒和微秒为单位
日期 - 仅年,月和日
datetime - 时间和日期的所有组成部分
timedelta - 最大天数的时间量

(1)datetime创建

 

(2)datetime转Timestamp

(3)datetime转np.datetime64

 

二.pandas库

(1)创建Timestamp

(2)Timestamp转datetime

从时间戳中获取日期时间

(3)Timestamp转np.datetime64[s]

 

 

三.numpy库

NumPy没有单独的日期和时间对象,只有一个datetime64对象来表示单个时刻。 datetime模块的datetime对象具有微秒精度(百万分之一秒)。 NumPy的datetime64对象允许您将其精度从小时数一直设置为阿秒(10 ^ -18)。 它的构造函数更灵活,可以采用各种输入。

(1)创建numPy的datetime64和timedelta64对象

(2)np.datetime64[s]转datetime64[ns]

 

(3)np.datetime64[s]转datetime.datetime

 

(4)np.datetime64[s]转Timestamp

 

 

参考文献:

【1】字符串转各种日期格式

【2】pandas时间戳索引:Datetimeindex

【3】Pandas日期数据处理:如何按日期筛选、显示及统计数据

【4】python - 在datetime,Timestamp和datetime64之间转换

 

posted @   nxf_rabbit75  阅读(32574)  评论(1编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
一.datetime库(1)datetime创建(2)datetime转Timestamp(3)datetime转np.datetime64二.pandas库(1)创建Timestamp(2)Timestamp转datetime(3)Timestamp转np.datetime64[s]三.numpy库(1)创建numPy的datetime64和timedelta64对象(2)np.datetime64[s]转datetime64[ns](3)np.datetime64[s]转datetime.datetime(4)np.datetime64[s]转Timestamp
点击右上角即可分享
微信分享提示