我认为这一定是pandas的一个错误。在使用pandas Series(版本18.1和19)时,如果我给Series分配一个日期,第一次它会被添加为int类型(错误),第二次它会被添加为datetime类型(正确)。我无法理解原因。
例如,使用以下代码:
输出结果为:
例如,使用以下代码:
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。
有人可以帮助我吗?非常感谢您的帮助,Javi。
Series
支持混合数据类型,所以看起来日期时间在初始赋值时被强制转换为整数,但是在相同的索引标签位置上进行覆盖赋值会产生预期的行为。我会在 github 上发布一个问题。 - EdChum