Python 将系列转换为日期时间

Python 将系列转换为日期时间,python,python-3.x,pandas,Python,Python 3.x,Pandas,D=[“10Aug49”、“21 Jan45”、“15 Sep47”、“13 Jun52”],将其转换为熊猫日期,确保年份为1900而不是2000年。到目前为止,我有这个转换和打印熊猫日期的代码,但世纪是2000年,我想要1900年 import pandas as pd from datetime import datetime Dae = pd.Series(["10Aug49","21Jan45","15Sep47","13Jun52"]) x =[] for i in Dae:

D=[“10Aug49”、“21 Jan45”、“15 Sep47”、“13 Jun52”],将其转换为熊猫日期,确保年份为1900而不是2000年。到目前为止,我有这个转换和打印熊猫日期的代码,但世纪是2000年,我想要1900年

import pandas as pd

from datetime import datetime

Dae = pd.Series(["10Aug49","21Jan45","15Sep47","13Jun52"])
x =[]

for i in Dae:
    x = datetime.strptime(i,"%d%b%y")
    print(x)

我觉得最好先更正年份,然后转换为日期时间:

# identify the year as the last group of digits and prepend 19
corrected_dates = Dae.str.replace('(\d+)$',r'19\1')

# convert to datetime
pd.to_datetime(corrected_dates)
输出:

0   1949-08-10
1   1945-01-21
2   1947-09-15
3   1952-06-13
dtype: datetime64[ns]

我觉得最好先更正年份,然后转换为日期时间:

# identify the year as the last group of digits and prepend 19
corrected_dates = Dae.str.replace('(\d+)$',r'19\1')

# convert to datetime
pd.to_datetime(corrected_dates)
输出:

0   1949-08-10
1   1945-01-21
2   1947-09-15
3   1952-06-13
dtype: datetime64[ns]
from datetime导入日期
作为pd进口熊猫
从日期时间导入日期时间
Dae=pd.系列([“10Aug49”、“2014年1月21日”、“47年9月15日”、“52年6月13日])
x=[]
新列表=[]
对于Dae中的i:
i=datetime.strtime(i,“%d%b%y”).date()
如果date.today()
from datetime导入日期
作为pd进口熊猫
从日期时间导入日期时间
Dae=pd.系列([“10Aug49”、“2014年1月21日”、“47年9月15日”、“52年6月13日])
x=[]
新列表=[]
对于Dae中的i:
i=datetime.strtime(i,“%d%b%y”).date()

如果date.today()
pd.to_datetime(Dae)
pd.to_datetime(Dae,格式='%d%b%y')
?pd.to_datetime(Dae,格式='%d%b%y')输出:0 2049-08-10 1 2045-01-21 2 2047-09-15 3 2052-06-13数据类型:datetime64[ns]
pd.to_datetime(Dae)
输出:0 2049-08-10 1 2045-01-21 2 2047-09-15 3 2052-06-13数据类型:datetime64[ns]pd.to_datetime(Dae.str.replace(“(\d+)(\d+)”,r“\1-\2-19\3”)。您能解释一下吗?虽然可以工作,但很难理解。@DhruvNautiyal如果有帮助,请查看更新的答案。pd.to_datetime(Dae.str.replace(“(\d+)(\d+)(\d+),“r”\1-\2-19\3')。您好,您能解释一下吗?虽然可以,但很难理解。@DhruvNautiyal如果有帮助,请查看更新的答案。