Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为数据帧中的时间戳数据设置时区_Python_Pandas - Fatal编程技术网

Python 为数据帧中的时间戳数据设置时区

Python 为数据帧中的时间戳数据设置时区,python,pandas,Python,Pandas,我有一个经常变化的数据帧,看起来像这样: date name time timezone 0 2016-08-01 aaa 0900 Asia/Tokyo 1 2016-08-04 bbb 1200 Europe/Berlin 2 2016-08-05 ccc 1400 Europe/London 日期、时间和时区指的是通常用于海外地点的交货日期,其名称为客户公司的名称 计划是获取该数据并创建一个datetime\u local列,

我有一个经常变化的数据帧,看起来像这样:

         date name  time       timezone
0  2016-08-01  aaa  0900     Asia/Tokyo
1  2016-08-04  bbb  1200  Europe/Berlin
2  2016-08-05  ccc  1400  Europe/London
日期、时间和时区指的是通常用于海外地点的交货日期,其名称为客户公司的名称

计划是获取该数据并创建一个
datetime\u local
列,该列包含数据帧的
timezone
列中显示的时区。然后,我想在此基础上添加一列
datetime\u london
,其中包含该日期和时间,但以伦敦的时间和日期表示

我已经掌握了大部分方法,但在调用
tz_localize
时,我最终遇到了
ValueError:序列的真值是不明确的。使用a.empty、a.bool()、a.item()、a.any()或a.all()
,这表明我没有正确使用时区处理列

关于如何进行有什么建议吗

mydf = pd.DataFrame(data={'date':['2016-08-01','2016-08-04','2016-08-05'],
                          'time':['0900','1200','1400'],
                          'timezone':['Asia/Tokyo','Europe/Berlin','Europe/London'],
                          'name':['aaa','bbb','ccc']}
)
print(mydf)
mydf["datetime"] = mydf["date"].map(str) + " " + mydf["time"]
mydf.datetime = pd.to_datetime(mydf.datetime)
mydf.index = mydf.datetime
print(mydf)
mydf["datetime_local"] = mydf.datetime
mydf.datetime_local.tz_localize(mydf.timezone)
输出:

         date name  time       timezone
0  2016-08-01  aaa  0900     Asia/Tokyo
1  2016-08-04  bbb  1200  Europe/Berlin
2  2016-08-05  ccc  1400  Europe/London
         date name  time       timezone         datetime  \
0  2016-08-01  aaa  0900     Asia/Tokyo  2016-08-01 0900   
1  2016-08-04  bbb  1200  Europe/Berlin  2016-08-04 1200   
2  2016-08-05  ccc  1400  Europe/London  2016-08-05 1400   

              datetime_local  
0  2016-08-01 09:00:00+09:00  
1  2016-08-04 12:00:00+02:00  
2  2016-08-05 14:00:00+01:00  
         date name  time       timezone         datetime  \
0  2016-08-01  aaa  0900     Asia/Tokyo  2016-08-01 0900   
1  2016-08-04  bbb  1200  Europe/Berlin  2016-08-04 1200   
2  2016-08-05  ccc  1400  Europe/London  2016-08-05 1400   

              datetime_local           datetime_london  
0  2016-08-01 09:00:00+09:00 2016-08-01 01:00:00+01:00  
1  2016-08-04 12:00:00+02:00 2016-08-04 11:00:00+01:00  
2  2016-08-05 14:00:00+01:00 2016-08-05 14:00:00+01:00  
添加
datetime\u local

mydf["datetime"] = mydf["date"].map(str) + " " + mydf["time"]
mydf['datetime_local'] = mydf.apply(convert_to_local_time, axis=1)
print(mydf)
mydf['datetime_london'] = mydf.apply(convert_to_london_time, axis=1)
print('After adding datetime_london:')
print(mydf)
输出:

         date name  time       timezone
0  2016-08-01  aaa  0900     Asia/Tokyo
1  2016-08-04  bbb  1200  Europe/Berlin
2  2016-08-05  ccc  1400  Europe/London
         date name  time       timezone         datetime  \
0  2016-08-01  aaa  0900     Asia/Tokyo  2016-08-01 0900   
1  2016-08-04  bbb  1200  Europe/Berlin  2016-08-04 1200   
2  2016-08-05  ccc  1400  Europe/London  2016-08-05 1400   

              datetime_local  
0  2016-08-01 09:00:00+09:00  
1  2016-08-04 12:00:00+02:00  
2  2016-08-05 14:00:00+01:00  
         date name  time       timezone         datetime  \
0  2016-08-01  aaa  0900     Asia/Tokyo  2016-08-01 0900   
1  2016-08-04  bbb  1200  Europe/Berlin  2016-08-04 1200   
2  2016-08-05  ccc  1400  Europe/London  2016-08-05 1400   

              datetime_local           datetime_london  
0  2016-08-01 09:00:00+09:00 2016-08-01 01:00:00+01:00  
1  2016-08-04 12:00:00+02:00 2016-08-04 11:00:00+01:00  
2  2016-08-05 14:00:00+01:00 2016-08-05 14:00:00+01:00  
添加
datetime\u london

mydf["datetime"] = mydf["date"].map(str) + " " + mydf["time"]
mydf['datetime_local'] = mydf.apply(convert_to_local_time, axis=1)
print(mydf)
mydf['datetime_london'] = mydf.apply(convert_to_london_time, axis=1)
print('After adding datetime_london:')
print(mydf)
输出:

         date name  time       timezone
0  2016-08-01  aaa  0900     Asia/Tokyo
1  2016-08-04  bbb  1200  Europe/Berlin
2  2016-08-05  ccc  1400  Europe/London
         date name  time       timezone         datetime  \
0  2016-08-01  aaa  0900     Asia/Tokyo  2016-08-01 0900   
1  2016-08-04  bbb  1200  Europe/Berlin  2016-08-04 1200   
2  2016-08-05  ccc  1400  Europe/London  2016-08-05 1400   

              datetime_local  
0  2016-08-01 09:00:00+09:00  
1  2016-08-04 12:00:00+02:00  
2  2016-08-05 14:00:00+01:00  
         date name  time       timezone         datetime  \
0  2016-08-01  aaa  0900     Asia/Tokyo  2016-08-01 0900   
1  2016-08-04  bbb  1200  Europe/Berlin  2016-08-04 1200   
2  2016-08-05  ccc  1400  Europe/London  2016-08-05 1400   

              datetime_local           datetime_london  
0  2016-08-01 09:00:00+09:00 2016-08-01 01:00:00+01:00  
1  2016-08-04 12:00:00+02:00 2016-08-04 11:00:00+01:00  
2  2016-08-05 14:00:00+01:00 2016-08-05 14:00:00+01:00  
试试这个:

In [12]: mydf.apply(lambda x: x.datetime_local.tz_localize(x.timezone), axis=1)
Out[12]:
datetime
2016-08-01 09:00:00    2016-08-01 09:00:00+09:00
2016-08-04 12:00:00    2016-08-04 12:00:00+02:00
2016-08-05 14:00:00    2016-08-05 14:00:00+01:00
dtype: object

虽然前面的答案非常有效,但当面对这个处理大数据的问题时,apply方法有点低效(对于10^6个数量级的行)。“应用”方法逐行处理该量表需要我10-15分钟。如果与数据帧中的行数相比,时区列的唯一值的比率非常小,则此代码的效率要高得多:

for tz in df['timezone'].unique():    #iterates over each unique value of timezone in the dataframe
    mask = df['timezone'] == tz       #selects all rows with current unique tz value
    df.loc[mask,'datetime_local'] = pd.DatetimeIndex(df.loc[mask, 'datetime']).tz_localize('UTC').tz_convert(tz)
最后一行代码将
日期时间
列转换为
日期时间索引
,这使得
日期时间
UTC和
tz变得幼稚
(如果还没有)。因此,需要使用
tz_localize('UTC')
,因为
tz_convert
不适用于
tz naive
日期时间

我知道这是一个老问题,但我真的需要一个更快的解决方案,我想我的答案可能会帮助我后面的人