【问题标题】:Timestamp subtraction must have the same timezones or no timezones时间戳减法必须具有相同的时区或没有时区
【发布时间】:2019-12-15 13:28:15
【问题描述】:

我已经解析了许多日期时间并将它们转换为不同的时区。但是,当尝试使用 df.to_excel 将数据框写入 excel 文件时,我收到以下错误,“时间戳减法必须具有相同的时区或没有时区”。

查看不同的线程,我尝试使用 tz_localize(None) 和 tz_convert 将日期时间转换为 UTC,然后写入文件,但没有奏效。

#C:\Users\cgarrido\AppData\Local\Programs\Python\Python37\Scripts>
import pandas as pd
from pandas import Timestamp
import pytz
from pytz import all_timezones
import datetime
import xlrd
import xlwt

data = pd.read_excel('lab.xls')
data = data.drop_duplicates('Site UP')
data = data.drop(data[data.Duration == 0].index)
data.to_excel('no_duplicates_no_zeroes.xls')
data['Site DOWN'] = pd.to_datetime(data['Site DOWN'])
data['Site UP'] = pd.to_datetime(data['Site UP'])

def conversion_function(x: pd.Series) -> pd.Timestamp:  
    zones = {'Atlantic': 'Canada/Atlantic',
             'Central': 'US/Central',
             'Eastern': "US/Eastern",
             'Mountain': 'US/Mountain',
             'Pacific': 'US/Pacific'}
    raw_time = pd.Timestamp(x[1])
    loc_raw_time = raw_time.tz_localize("US/Pacific")
    return loc_raw_time.tz_convert(zones[x[0]])

data['Adjusted_Down'] = data[['Time_Zone', 'Site DOWN']].apply(conversion_function, axis=1)
data['Adjusted_Up'] = data[['Time_Zone', 'Site UP']].apply(conversion_function, axis=1)

data.to_excel('no_duplicates_no_zeroes.xls')

这是一个数据框示例(在转换日期之前)

Region  Time_Zone   Site DOWN       Site UP         Duration
US      Mountain    07/22/19 01:19  07/22/19 01:23  4
Canada  Central     07/22/19 01:19  07/22/19 01:23  4
US      Eastern     07/21/19 22:50  07/21/19 22:52  2
US      Eastern     07/16/19 11:26  07/16/19 11:29  3
US      Pacific     07/25/19 16:47  07/25/19 16:50  3
US      Pacific     07/22/19 13:13  07/22/19 13:15  2
US      Pacific     07/22/19 01:20  07/22/19 01:22  2
US      Central     06/30/19 11:56  06/30/19 11:58  2
US      Mountain    07/29/19 03:14  07/29/19 03:16  2
US      Mountain    07/22/19 01:19  07/22/19 01:23  4
Canada  Atlantic    07/22/19 01:19  07/22/19 01:21  2
Canada  Eastern     07/03/19 06:51  07/03/19 11:34  283
US      Eastern     07/21/19 16:51  07/21/19 16:53  2
US      Eastern     07/21/19 13:43  07/21/19 16:31  168
Canada  Atlantic    07/22/19 01:19  07/22/19 01:21  2
US      Mountain    07/18/19 01:30  07/18/19 01:58  28
US      Central     07/22/19 01:20  07/22/19 01:22  2
Canada  Central     07/17/19 22:17  07/17/19 22:21  4
Canada  Central     07/15/19 06:14  07/15/19 08:42  148
Canada  Mountain    07/22/19 01:19  07/22/19 01:23  4
Canada  Mountain    07/22/19 01:18  07/22/19 01:21  3
Canada  Central     07/22/19 01:20  07/22/19 01:22  2
Canada  Central     07/17/19 09:26  07/17/19 09:28  2
Canada  Atlantic    07/30/19 18:18  07/31/19 04:44  626
Canada  Atlantic    07/29/19 21:20  07/29/19 21:22  2
Canada  Atlantic    07/25/19 03:41  07/25/19 03:43  2
Canada  Atlantic    07/22/19 01:20  07/22/19 01:23  3
Canada  Atlantic    07/21/19 22:50  07/21/19 22:50  0
Canada  Eastern     07/24/19 01:57  07/24/19 03:55  118

因此,在尝试将数据框保存到 Excel 工作表时出现错误消息。

Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    data.to_excel('final.xls', 'a+')
  File "C:\Users\cgarrido\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\generic.py", line 2127, in to_excel
    engine=engine)
  File "C:\Users\cgarrido\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\formats\excel.py", line 662, in write
    freeze_panes=freeze_panes)
  File "C:\Users\cgarrido\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel.py", line 1720, in write_cells
    val, style)
  File "C:\Users\cgarrido\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwt\Worksheet.py", line 1088, in write
    self.row(r).write(c, label, style)
  File "C:\Users\cgarrido\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwt\Row.py", line 244, in write
    date_number = self.__excel_date_dt(label)
  File "C:\Users\cgarrido\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwt\Row.py", line 99, in __excel_date_dt
    delta = date - epoch
  File "pandas\_libs\tslibs\timestamps.pyx", line 413, in pandas._libs.tslibs.timestamps._Timestamp.__sub__
TypeError: Timestamp subtraction must have the same timezones or no timezones

【问题讨论】:

  • 我没有立即的答案,但它有助于允许重现错误 - 因为需要data 数据帧的一部分 - 您可以提供代码来创建它,或者例如data.to_dict() 结果。这类似于您作为打印输出提供的输出,但允许重新创建错误并解决它。

标签: python pandas date datetime


【解决方案1】:

尝试为tz_localizetz_convert 切换这些参数 - 您应该首先获得一个带有实际时区的时区感知时间,然后将其转换为“美国/太平洋”:

raw_time = pd.Timestamp(x[1])
loc_raw_time = raw_time.tz_localize(zones[x[0]])
return loc_raw_time.tz_convert("US/Pacific").replace(tzinfo=None)

【讨论】:

  • 需要从@cgarrido 知道原始时间指的是什么 - 美国/太平洋的所有时间或本地时区记录的所有时间?
  • 感谢您的回复,试一试,但仍然得到相同的“时间戳减法必须具有相同的时区或没有时区”错误。每个站点的原始时间都记录在美国/太平洋地区,然后转换为当地时间。
  • 我搜索了一下,发现 Excel 不接受包含时区的日期时间值,请尝试使用我编辑的答案。
猜你喜欢
  • 2021-10-17
  • 2020-07-07
  • 2020-06-29
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 2011-12-14
相关资源
最近更新 更多