惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
博客园_首页
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
V
Visual Studio Blog
小众软件
小众软件
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
T
Tor Project blog
Jina AI
Jina AI
GbyAI
GbyAI
C
Comments on: Blog
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
A
Arctic Wolf
有赞技术团队
有赞技术团队
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
Security Latest
Security Latest
Webroot Blog
Webroot Blog
C
Cisco Blogs
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
K
Kaspersky official blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
H
Hacker News: Front Page
D
Darknet – Hacking Tools, Hacker News & Cyber Security
D
Docker
P
Palo Alto Networks Blog
The Register - Security
The Register - Security
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志

博客园 - 隐客

商品期权的保证金计算 写个chrome插件屏蔽某些视频,防止孩子看些不正常的视频 获取个股信息的东财数据 AI量化qlib学习笔记四:测试模型 AI量化qlib学习笔记三:训练模型 AI量化qlib学习笔记二:转换数据 AI量化qlib学习笔记 一:清洗数据 python 打包工具 python: 与通达信联动 随手写了街机一键发招的代码 使用pybind11封装c++的dll,供python调用 用py-spy对python线程查看cpu等资源 占用和消耗 通过selenium获取性能日志中的response的body Windows Server 2012无法安装 .NET3.5-安装角色或功能失败,找不到源文件 vscode 扩展商店打不开的解决办法 通过1分钟生成其它线的bar配置文件 用numpy读取结构化二进制文件 微信pc防撤回修改笔记 把本地vscode项目代码传到gitee上
关于在python中时间的转换
隐客 · 2023-04-24 · via 博客园 - 隐客

这个时间在不同的情况下,可能有些差异

先说pandas下面的Series,可能是时间类型(该时间类型为pandas下的,跟python的datetime下的不是同种类型),但带有时区,一般为了后续方便统一换成相同时区或没有时区的

import pandas as pd

# 创建一个带有时区信息的 Series
s = pd.Series(pd.date_range('2022-01-01 00:00:00', periods=3, freq='H', tz='Asia/Shanghai'))

# 将 Series 转换为不带时区信息的 Series
s_without_tz = s.dt.tz_localize(None)

print('带有时区信息的 Series:')
print(s)

print('\n不带时区信息的 Series:')
print(s_without_tz)

 否则可能报错:代码中报错Can only use .dt accessor with datetimelike values

如果不处理时区问题,可能提示:can't compare offset-naive and offset-aware datetimes

判断有没有时区,一个是datetime下的,一个是pandas下面的

from datetime import datetime, timezone, timedelta

# 创建带有时区信息的日期时间对象
dt_with_tz = datetime(2022, 4, 25, 12, 0, 0, tzinfo=timezone(timedelta(hours=8)))

# 判断日期时间对象是否具有时区信息
if dt_with_tz.tzinfo is not None:
    print('dt_with_tz has timezone information')
else:
    print('dt_with_tz does not have timezone information')
import pandas as pd

# 创建带有时区信息的日期时间对象
dt_with_tz = pd.Timestamp('2022-04-25 12:00:00', tz='Asia/Shanghai')

# 判断日期时间对象是否具有时区信息
if dt_with_tz.dt.tz is not None:
    print('dt_with_tz has timezone information')
else:
    print('dt_with_tz does not have timezone information') 

去掉时区,也分pandas和datetime

 import pandas as pd

# 创建一个带有时区信息的 DataFrame
df = pd.DataFrame({'timestamp': pd.date_range('2022-04-25 12:00:00', periods=3, tz='Asia/Shanghai')})

# 显示带有时区信息的时间列
print(df['timestamp'])

# 将时间列中的时区信息去掉
df['timestamp'] = df['timestamp'].dt.tz_localize(None)

# 显示去掉时区信息后的时间列
print(df['timestamp'])
import datetime

# 创建一个带有时区信息的 datetime 对象
dt_with_tz = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8)))

# 将带有时区信息的 datetime 对象转换为没有时区信息的对象
dt_without_tz = dt_with_tz.astimezone(datetime.timezone.utc).replace(tzinfo=None)

print(dt_with_tz)  # 输出: 2022-05-07 13:28:15.183422+08:00
print(dt_without_tz)  # 输出: 2022-05-07 05:28:15.183422

  实际上,不是简单直接去掉时区,如果时区不同,可能还要把时间加上去,下面是个例子把pd中的内容更新示例

import pandas as pd

# 创建一个 DataFrame
df = pd.DataFrame({'col1': [1, 2, 3, 4, 5]})

# 查看 col1 列的值
print(df['col1'])  # 输出: 1  2  3  4  5

# 对 col1 列的所有值都加上 1
df['col1'] = df['col1'].apply(lambda x: x + 1)

# 查看加法计算后的结果
print(df['col1'])  # 输出: 2  3  4  5  6

 在更新之前要获取tzinfo中的时差,然后加上去即可

import datetime

dt = datetime.datetime.now(datetime.timezone.utc)
tz_offset = dt.tzinfo.utcoffset(None)
print(tz_offset) # 输出:0:00:00

大概是这样

data1["eob"]=data1["eob"].apply(lambda x: x.astimezone(timezone.utc).replace(tzinfo=None) if (x.tzinfo.utcoffset(None)==None)else (x + x.tzinfo.utcoffset(None)).astimezone(timezone.utc).replace(tzinfo=None)    )