Python 将数字转换为特殊日期时间
日期1:20061201Python 将数字转换为特殊日期时间,python,python-2.7,python-3.x,pandas,numpy,Python,Python 2.7,Python 3.x,Pandas,Numpy,日期1:20061201 日期2:01/12/2006 如何使用Python中的pandas将date1转换为date2(日/月/年)格式?谢谢!Date1和Date2是csv文件中的两列。最简单的方法可能是使用以下提供的日期解析: 您可以使用以下方法将此转换应用于数据帧/系列中的所有行: from datetime import datetime def convert_date(d): return datetime.strptime(str(d), "%Y%m%d") df['Da
日期2:01/12/2006
如何使用Python中的pandas将date1转换为date2(日/月/年)格式?谢谢!Date1和Date2是csv文件中的两列。最简单的方法可能是使用以下提供的日期解析: 您可以使用以下方法将此转换应用于数据帧/系列中的所有行:
from datetime import datetime
def convert_date(d):
return datetime.strptime(str(d), "%Y%m%d")
df['Date2'] = df.Date1.apply(convert_date)
这将在数据框df
中添加一个Date2
列,它是Date1
列的日期时间表示形式
然后,您可以使用strftime
再次序列化日期:
def serialize_date(d):
return d.strftime(d, "%d/%m/%Y")
df['Date2'] = df.Date2.apply(serialize_date)
或者,您可以通过字符串操作来完成这一切:
def reformat_date(d):
year = d // 10000
month = d % 10000 // 100
day = d % 100
return "{day}/{month}/{year}".format(day=day, month=month, year=year)
df['Date2'] = df.Date1.apply(reformat_date)
如果您使用的是
pandas
并且想要返回时间戳
对象,那么这比使用strtime
提供的解析机制要快得多
import datetime
A=datetime.datetime.strptime('20061201','%Y%m%d')
A.strftime('%m/%d/%Y')
pd.to_datetime('20061201')
Timestamp('2006-12-01 00:00:00')
如果你想要回一根绳子
str(pd.to_datetime('20061201').date())
'2006-12-01'
假设您有一个数据帧
df
df = pd.DataFrame(dict(Date1=['20161201']))
然后,您可以在矢量化形式中使用相同的技术
作为时间戳
df.assign(Date2=pd.to_datetime(df.Date1))
Date1 Date2
0 20161201 2016-12-01
df.assign(Date2=pd.to_datetime(df.Date1).dt.date.astype(str))
Date1 Date2
0 20161201 2016-12-01
作为字符串
df.assign(Date2=pd.to_datetime(df.Date1))
Date1 Date2
0 20161201 2016-12-01
df.assign(Date2=pd.to_datetime(df.Date1).dt.date.astype(str))
Date1 Date2
0 20161201 2016-12-01
您可以在这里使用apply和lambda函数 假设您有一个名为
df
的数据集,如下所示:
id date1
0 20061201
2 20061202
您可以使用如下代码:
df['date2'] = df['date1'].apply(lambda x: x[6:] + '/' + x[4:6] + '/' + x[:4])
结果将是:
id date1 date2
0 20061201 01/12/2016
2 20061202 02/12/2016
数据:
In [151]: df
Out[151]:
Date
0 20061201
1 20170530
In [152]: pd.to_datetime(df.Date, format='%Y%m%d').dt.strftime('%d/%m/%Y')
Out[152]:
0 01/12/2006
1 30/05/2017
Name: Date, dtype: object
In [153]: df.Date.astype(str).str.replace('(\d{4})(\d{2})(\d{2})', r'\3/\2/\1')
Out[153]:
0 01/12/2006
1 30/05/2017
Name: Date, dtype: object
选项1:
In [151]: df
Out[151]:
Date
0 20061201
1 20170530
In [152]: pd.to_datetime(df.Date, format='%Y%m%d').dt.strftime('%d/%m/%Y')
Out[152]:
0 01/12/2006
1 30/05/2017
Name: Date, dtype: object
In [153]: df.Date.astype(str).str.replace('(\d{4})(\d{2})(\d{2})', r'\3/\2/\1')
Out[153]:
0 01/12/2006
1 30/05/2017
Name: Date, dtype: object
选项2:
In [151]: df
Out[151]:
Date
0 20061201
1 20170530
In [152]: pd.to_datetime(df.Date, format='%Y%m%d').dt.strftime('%d/%m/%Y')
Out[152]:
0 01/12/2006
1 30/05/2017
Name: Date, dtype: object
In [153]: df.Date.astype(str).str.replace('(\d{4})(\d{2})(\d{2})', r'\3/\2/\1')
Out[153]:
0 01/12/2006
1 30/05/2017
Name: Date, dtype: object
注意:如果处理真正大的数据集,strtime的速度相当慢(因为它会做很多额外的处理)。但是我需要日/月/年输出格式。谢谢!将序列化添加到所需格式和字符串操作版本。@andrew,欢迎您!请考虑最有帮助的答案和支持所有有用的答案。