版本0.15.2中的pandas to_excel不适用于datetime对象 [英] pandas to_excel in version 0.15.2 not working for datetime objects

查看:345
本文介绍了版本0.15.2中的pandas to_excel不适用于datetime对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一个 datetime 对象写入Excel的一个简单示例不起作用,因为我将熊猫版本更改为0.15.2。我缺少什么吗?

A simple example of writing a datetime object to Excel is not working since I changed my pandas version to 0.15.2. Am I missing something?

import pandas as pd
import datetime

df = pd.DataFrame([datetime.datetime.today()])
df.to_excel('test.xlsx')

年,月和日正确显示,小时等我得到零。如果我把它写到.csv,结果就OK了。可能是0.15.2的错误?

Year, month and day are correctly displayed, for hour and so on I get zeros. If I write it to .csv the result is OK. Possibly a bug in 0.15.2?

推荐答案

正如@joris在几天前所说的那样,现在有一个问题, c $ c> to_excel 因为一些 Timedelta 更改。请参阅 https://github.com/pydata/pandas/issues/9139

As @joris commented some days ago, there is an issue for now with to_excel because of some Timedelta changes. see https://github.com/pydata/pandas/issues/9139

您可以使用 openpyxl 作为XLSX writer引擎使用以下方式临时修复此问题:

You can temporally fix this using openpyxl as XLSX writer engine using:

df.to_excel('test.xlsx', engine='openpyxl')

使用熊猫0.16这个bug现在是固定的,所以你可以做

With Pandas 0.16 this bug is now fixed and so you can do

import pandas as pd
import datetime

df = pd.DataFrame([datetime.datetime.today()])
df.to_excel('test.xlsx')

它将创建一个名为 test.xlsx 的Excel文件,如

It will create an Excel file named test.xlsx like

  |    A
--+---------------------
1 |    0
2 |0   2015-07-13 10:27:34

这篇关于版本0.15.2中的pandas to_excel不适用于datetime对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆