gpt4 book ai didi

python - 根据另一列的数据更改时区

转载 作者:太空宇宙 更新时间:2023-11-03 20:35:29 24 4
gpt4 key购买 nike

这是我的示例数据框:

                     Office  Design  ...          SiteLog Duration
0 DQFEMOZM - 2141 ZMI_PE ... 6/28/2019 7:59 6
1 DQFEMOZM - 2141 ZMI_PE ... 6/28/2019 7:47 5
2 DQFEMOZM - 2141 ZMI_PE ... 6/27/2019 4:58 2
3 DQFEMOZM - 2141 ZMI_PE ... 6/27/2019 4:52 2
4 YMTSZUXXQN - 1031 ZMI_PE ... 6/3/2019 4:10 4
6 YMTSZUXXQN - 1031 ZMI_PE ... 6/2/2019 22:36 6
9 UTUXMW - 1046 ZMI_PE ... 6/26/2019 20:01 336
10 UTUXMW - 1046 ZMI_PE ... 6/26/2019 14:16 828
11 UTUXMW - 1046 ZMI_PE ... 6/14/2019 16:33 2
12 UTUXMW - 1046 ZMI_PE ... 6/14/2019 15:07 2
14 GMUH-FZAB XMHMX - 2114 ZMI_PE ... 6/25/2019 5:35 3
15 TSGADANXDMY - 1215 ZMI_PE ... 6/9/2019 3:10 3
16 TSGADANXDMY - 1215 ZMI_PE ... 6/8/2019 19:03 2
17 TSGADANXDMY - 1215 ZMI_PE ... 6/8/2019 3:59 2
18 PDARPQY - 1154 ZMI_PE ... 6/30/2019 7:06 1
19 PDARPQY - 1154 ZMI_PE ... 6/18/2019 5:04 216
21 MSGMEEUEEUY - 2027 ZMI_PE ... 6/27/2019 17:36 2
23 MSGMEEUEEUY - 2027 ZMI_PE ... 6/4/2019 9:32 11
25 MSGMEEUEEUY - 2027 ZMI_PE ... 6/2/2019 22:37 4
26 MSGMEEUEEUY - 2027 ZMI_PE ... 6/2/2019 22:25 2
28 MSGMEEUEEUY - 2027 ZMI_PE ... 5/29/2019 23:24 2

所有示例站点日志均采用 PST 格式。我想做的是获取某些行,例如办公室“DQFEMOZM - 2141”,并将站点日志时间戳更改为 EST。

我尝试过使用 tz_localize 和 tz_convert 函数,但无法让它们工作。

import pandas as pd
from pytz import all_timezones
data = pd.read_csv('lab.csv')
data = data.drop_duplicates('SiteLog')
data = data.drop(data[data.Duration == 0].index)
DQFEMOZM = data[data.Office == 'DQFEMOZM - 2141'].index
DQFEMOZM = DQFEMOZM.tz_localize('America/Los_Angeles')
DQFEMOZM = DQFEMOZM.tz_convert('America/New_York')

我收到的部分错误消息:

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
DQFEMOZM = DQFEMOZM.tz_convert('America/New_York')
AttributeError: 'Int64Index' object has no attribute 'tz_convert'

最佳答案

您正在将索引分配给data,使其成为Int64。您可以像下面这样完成任务:

from pandas import Timestamp
import pandas as pd

data = pd.read_csv("/Users/user/Desktop/Book6.csv")
data = data.drop_duplicates("SiteLog")

for office, datetime in zip(data["Office"], data["SiteLog"]):
if office == "DQFEMOZM - 2141":
raw_time = Timestamp(datetime)
print(raw_time)
loc_raw_time = raw_time.tz_localize("America/Los_Angeles")
print(loc_raw_time)
new_raw_time = loc_raw_time.tz_convert("America/New_York")
print(new_raw_time)

例如,我使用前两行:

                     Office  Design  ...          SiteLog Duration
0 DQFEMOZM - 2141 ZMI_PE ... 6/28/2019 7:59 6
1 DQFEMOZM - 2141 ZMI_PE ... 6/28/2019 7:47 5

代码输出是(供您引用,我只是打印您传输的所有时区),

2019-06-28 07:59:00
2019-06-28 07:59:00-07:00
2019-06-28 10:59:00-04:00
2019-06-28 07:47:00
2019-06-28 07:47:00-07:00
2019-06-28 10:47:00-04:00

关于python - 根据另一列的数据更改时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57192170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com