TypeError:时间戳减法

时间:2017-06-22 20:24:00

标签: python python-2.7 pandas datetime-format python-datetime

我有一个脚本可以收集并收集数据。我遇到了TypeError: Timestamp subtraction must have the same timezones or no timezones错误。我已经查看了有关此错误的其他帖子,但无法为我找到解决方案。

如何绕过此错误。收集数据后,我不会操纵它,我不太明白为什么我不能将此dataframe保存到Excel文档中。有人可以提供帮助吗?

import pandas as pd
import numpy as np
import os
import datetime
import pvlib
from pvlib.forecast import GFS, NAM

#directories and filepaths
barnwell_dir = r'D:\Saurabh\Production Forecasting\Machine Learning\Sites\Barnwell'
barnwell_training = r'8760_barnwell.xlsx'

#constants
writer = pd.ExcelWriter('test' + '_PythonExport.xlsx', engine='xlsxwriter')    
time_zone = 'Etc/GMT+5'
barnwell_list = [r'8760_barnwell.xlsx', 33.2376, -81.3510] 

def get_gfs_processed_data1():
    start = pd.Timestamp(datetime.date.today(), tz=time_zone) #used for testing last week
    end = start + pd.Timedelta(days=6)
    gfs = GFS(resolution='quarter')
    #get processed data for lat/long point
    forecasted_data = gfs.get_processed_data(barnwell_list[1], barnwell_list[2], start, end)
    forecasted_data.to_excel(writer, sheet_name='Sheet1')


get_gfs_processed_data1()

2 个答案:

答案 0 :(得分:2)

当我运行示例代码时,我会在堆栈跟踪结束时从XlsxWriter收到以下警告:

"Excel doesn't support timezones in datetimes. "
TypeError: Excel doesn't support timezones in datetimes. 
Set the tzinfo in the datetime/time object to None or use the
'remove_timezone' Workbook() option

我认为这是相当不言自明的。要从时间戳中删除时区,请按照建议通过remove_timezone选项:

writer = pd.ExcelWriter('test' + '_PythonExport.xlsx',
                        engine='xlsxwriter',
                        options={'remove_timezone': True})

当我进行此更改时,示例运行并生成xlsx文件。请注意,remove_timezone选项需要XlsxWriter> = 0.9.5。

答案 1 :(得分:2)

您可以像这样从所有import ReactDOM from "react-dom"; import { Component } from "react"; import "bootstrap/dist/css/bootstrap.css"; import "./translate.css"; class Translate extends React.Component { render() { return ( <div> <nav className="navbar navbar-expand-sm navbar-light bg-dark align-items-center"> /*Brand Name*/ <a href="Tarnslate.html" className="navbar-brand"> Translate </a> <button className="navbar-toggler" data-toggle="collapse" data-target="#HomepageMenu" > <span className="navbar-toggler-icon"></span> </button> /*Toggle Horizontal Alignment*/ <div className="collapse navbar-collapse " id="HomepageMenu"> /*Navigation Items*/ <div className="navbar-nav"> <a className="nav-link" href="#"> ABOUT US </a> <a className="nav-link" href="#"> PRICING </a> <a className="nav-link" href="#"> WORK WITH US </a> <a className="nav-link" id="sign-in" href="#"> SIGN IN </a> </div> </div> </nav> <div className="main-content"> <h6 id="h6">Tip: Click the highlighted words</h6> <div id="line"></div> <p id="main-text"> Please translate <span id="first">my document</span> from{" "} <span id="second">this language</span> to{" "} <span id="third">these language(s)</span>, <br /> and send the translated version to <span id="last">my email</span>. </p> <div id="pop-up"></div> </div> <div className="bottom"> <div id="flex-container"> <p id="bottom-brand-name">Translate's Official Website</p> /*Social Media Icons*/ <img src="./Images/Facebook.svg" className="social-media-icons" id="facebook" /> <img src="./Images/Twitter.svg" className="social-media-icons" id="twitter" /> <img src="./Images/Linkedin.svg" className="social-media-icons" id="linkedin" /> </div> <p id="copyright">©All Rights Reserved 2019</p> </div> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous" ></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous" ></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous" ></script> </div> ); } } export default Translate; 列中删除时区:

datetime

之后,您可以毫无问题地保存excel

注意:

  

要选择Pandas datetimetz dtypes,请使用“ datetimetz”(0.20.0中的新功能)   或“ datetime64 [ns,tz]”