【发布时间】:2017-04-04 14:57:29
【问题描述】:
我认为这一定是熊猫的失败,有熊猫系列(v.18.1 和 19 也是),如果我为系列分配日期,第一次添加为 int(错误),第二次添加为日期时间(正确),我无法理解原因。
例如使用此代码:
import datetime as dt
import pandas as pd
series = pd.Series(list('abc'))
date = dt.datetime(2016, 10, 30, 0, 0)
series["Date_column"] =date
print("The date is {} and the type is {}".format(series["Date_column"], type(series["Date_column"])))
series["Date_column"] =date
print("The date is {} and the type is {}".format(series["Date_column"], type(series["Date_column"])))
输出是:
The date is 1477785600000000000 and the type is <class 'int'>
The date is 2016-10-30 00:00:00 and the type is <class 'datetime.datetime'>
如您所见,第一次它总是将值设置为 int 而不是 datetime。
有人可以帮助我吗? 非常感谢您, 哈维。
【问题讨论】:
-
我不知道是什么导致了这种行为,但是在将日期添加到字符串列时应该小心。您知道您添加的是一行,而不是一列,对吧?
-
这对我来说闻起来像一个错误,
Series支持混合 dtype,因此看起来日期时间在初始分配时被强制为 int,但随后覆盖相同的索引标签位置会产生预期的行为。我会在github 上发布问题
标签: python datetime pandas series