Python 在执行时间序列分析时,我得到了将字符串转换为日期时间时的错误日期时间

Python 在执行时间序列分析时,我得到了将字符串转换为日期时间时的错误日期时间,python,pandas,datetime,time-series,Python,Pandas,Datetime,Time Series,我有日期列中的数据,我想转换成DateTime,出现如下错误 Month Sales of shampoo over a three year period 0 1-01 266.0 1 1-02 145.9 2 1-03 183.1 3 1-04 119.3 4 1-05 180.3 pd.to_datetime(data['Month']) 错误:- OutOfBoundsDatetime: Out of bounds nan

我有日期列中的数据,我想转换成DateTime,出现如下错误

    Month   Sales of shampoo over a three year period
0   1-01    266.0
1   1-02    145.9
2   1-03    183.1
3   1-04    119.3
4   1-05    180.3

pd.to_datetime(data['Month'])
错误:-

OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:00

在这种情况下我应该做什么:-

我想你应该试试:

pd.to_datetime(data['Month'], format="%y-%m")

我认为你有零填充问题。如果使用“01”而不是“1”,则可以将对象转换为DateTime

就这样,

x=['1-01','1-02','1-03','1-04','1-05']
y=[266.0,145.9,183.1,119.3,180.3]
data=pd.DataFrame(list(zip(x,y)),columns=['month','sth'])
data['month']='0'+data['month']
data['month']=pd.to_datetime(data['month'],format='%y-%m')
data['month']=data['month'].dt.strftime('%y-%m') # to convert 2-digits

“月”列的格式是什么?它是“月日”吗?这里的
1
代表年份,
02
代表当年的月份这里的
1
代表年份,05和06等代表月份。我改变了答案。我想这就是你想要的。OP的输入似乎是“1-01”,而不是“01-01”。因此,您可能需要添加
数据['month']='0'+数据['month']