将日期时间导入Excel

时间:2018-06-30 15:34:16

标签: pandas openpyxl nsepy

下面lock行(接近结尾处)的错误:

def change_to_date(string):
    seq = (string[:2],string[2:5],string[5:])
    return '-'.join(seq)

pt['DATE'] = pt['DATE'].apply(change_to_date)

错误是:

option.py

这是文件ws.cell(row=i,column=j).value=c

raise ValueError("Cannot convert {0!r} to Excel".format(value))

ValueError: Cannot convert [Date

2014-12-01    8555.9

Name: Close, dtype: float64] to Excel

1 个答案:

答案 0 :(得分:0)

我没有使用nsepy.derivativesopenpyxl,所以我无法验证或确认。但是错误会指出您的大部分情况:

了解您的错误

在Excel中无法完全/正确地理解在get_expiry_date行中从expiry=get_expiry_date(year=xyear,month=xmonth)获得的正确的Python datetime对象。

您可以在openpyxl代码中更深入地了解如何管理日期,但是除非加以限制,只需将datetime对象转换为所需格式的字符串,然后再将其推出Excel。 / p>

解决方案

for c in data:之后,您可以添加:

if isinstance(c, datetime.datetime):
    d = d.strftime("%Y-%m-%d")

这会将d转换为类似"2018-06-30"(年,月,日)格式的字符串。您可以将其更改为您喜欢的任何格式...我更喜欢这里给出的亚洲风格,因为它完全不含糊。

PS。我认真编辑了您的问题,使其更易于管理。问题越容易阅读/理解,您得到的答复就会越多/越好。