当我遇到ValueError时,我试图将DataFrame列(字符串)转换为datetime格式.这是使用字符串而不是DataFrame列的可复制代码(和错误):
I was trying to convert a DataFrame column (string) into datetime format when I encountered a ValueError. Here is a reproducible code (and the error) using a string instead of an DataFrame column:
>>> import pandas as pd >>> pd.to_datetime('2007-08-17', format='%Y-%m-%d') #Works fine Timestamp('2007-08-17 00:00:00') >>> pd.to_datetime('2557-08-17', format='%Y-%m-%d') # Throws Error ValueError: time data '2557-08-17' does match format specified这是一个真正的问题,还是我做错了什么?该如何解决?
Is this a genuine issue or am I doing something wrong? How can this be fixed?
推荐答案pandas.pydata/pandas-docs/stable/timeseries.html#timeseries-oob
以下是表示超出界限的数据的方法(从Timestamp('1677-09-22 00:12:43.145225')到Timestamp('2262-04-11 23:47:16.854775807')).
Here are ways to represent data outside of the bounds (which are from Timestamp('1677-09-22 00:12:43.145225') to Timestamp('2262-04-11 23:47:16.854775807') ).
建议在TimeStamp范围之外进行计算的方法是使用PeriodIndex.
The recommended way to do computations outside of the TimeStamp bounds is to use a PeriodIndex.
更多推荐
Python 2.7:Pandas日期时间不适用于将来的日期吗?
发布评论