pandas.to_datetime return NaT 从 2270-12-31 开始
pandas.to_datetime return NaT starting from 2270-12-31
我正在使用 python 3.5.3 和 pandas 0.21.0
以下未来日期有效:
pandas.to_datetime('2260-12-31', format='%Y-%m-%d', errors='coerce')
Timestamp('2260-12-31 00:00:00')
但从 2270 开始,它不再被接受:
pandas.to_datetime('2270-12-31', format='%Y-%m-%d', errors='coerce')
NaT
如果我使用 datetime.strptime 它在所有情况下都有效:
datetime.datetime.strptime('2270-12-31', '%Y-%m-%d')
datetime.datetime(2270, 12, 31, 0, 0)
或
datetime.datetime.strptime('9999-12-31-23.59.59.999999', '%Y-%m-%d-%H.%M.%S.%f')
datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
是不是因为pandas return一个时间戳,第二个datetime.datetime?
在 pandas 文档中,我看到以下内容,但我不知道 Timestamp.max 的值是多少。在最好的情况下,使用其他选项 return 初始字符串。
In case when it is not possible to return designated types (e.g. when
any element of input is before Timestamp.min or after Timestamp.max)
return will have datetime.datetime type (or correspoding
array/Series).
有没有办法让它与 to_datetime 一起工作?当您有许多列都具有不同的日期和时间戳模式并且想要将所有列从对象转换为 pandas 数据帧中的 datetime64[ns] 时,这非常方便。
一切正常。这样做的原因是时间戳实际上是引擎盖下的整数。它们代表一个 64 位整数 + 一个偏移时间。在这种情况下,“2270-12-31”距离偏移量太远,无法放入 64 位整数中,因此它表示不是时间。
我正在使用 python 3.5.3 和 pandas 0.21.0
以下未来日期有效:
pandas.to_datetime('2260-12-31', format='%Y-%m-%d', errors='coerce')
Timestamp('2260-12-31 00:00:00')
但从 2270 开始,它不再被接受:
pandas.to_datetime('2270-12-31', format='%Y-%m-%d', errors='coerce')
NaT
如果我使用 datetime.strptime 它在所有情况下都有效:
datetime.datetime.strptime('2270-12-31', '%Y-%m-%d')
datetime.datetime(2270, 12, 31, 0, 0)
或
datetime.datetime.strptime('9999-12-31-23.59.59.999999', '%Y-%m-%d-%H.%M.%S.%f')
datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
是不是因为pandas return一个时间戳,第二个datetime.datetime?
在 pandas 文档中,我看到以下内容,但我不知道 Timestamp.max 的值是多少。在最好的情况下,使用其他选项 return 初始字符串。
In case when it is not possible to return designated types (e.g. when any element of input is before Timestamp.min or after Timestamp.max) return will have datetime.datetime type (or correspoding array/Series).
有没有办法让它与 to_datetime 一起工作?当您有许多列都具有不同的日期和时间戳模式并且想要将所有列从对象转换为 pandas 数据帧中的 datetime64[ns] 时,这非常方便。
一切正常。这样做的原因是时间戳实际上是引擎盖下的整数。它们代表一个 64 位整数 + 一个偏移时间。在这种情况下,“2270-12-31”距离偏移量太远,无法放入 64 位整数中,因此它表示不是时间。