[英]Converting dtype('O') to a date DD/MM/YYYY
im a begginer at coding and started a project for my small business.我是编码初学者,并为我的小企业启动了一个项目。
i imported a xlsx file using panda and got the table with dtype('O') at every columns我使用 panda 导入了一个 xlsx 文件,并在每一列中得到了带有 dtype('O') 的表格
but i can't find anywhere a way to get only the date in the format DD/MM/YYYY但我无法在任何地方找到只获取 DD/MM/YYYY 格式的日期的方法
any tips?有小费吗?
i have tried this code我试过这段代码
tabela['dt_nasc'] = pd.to_datetime(tabela['dt_nasc'], format='%m/%d/%Y')
but the results were但结果是
ValueError: time data '1988-10-24 00:00:00' does not match format '%m/%d/%Y' (match)
i also tried another code我也尝试了另一个代码
import datetime
def convert_to_date(x):
return datetime.datetime.strptime(x , '%Y-%m-%d %H:%M:%S')
tabela.loc[:, 'dt_nasc'] = tabela['dt_nasc'].apply(convert_to_date)
# better to use a lambda function
tabela.loc[:, 'dt_nasc'] = tabela['dt_nasc'].apply(lambda x:datetime.datetime.strptime(x , '%Y-%m-%d %H:%M:%S'))
but couldn't find a way to print at format DD/MM/YYYY但找不到以 DD/MM/YYYY 格式打印的方法
Example例子
s = pd.Series({0: '2022-11-29 13:00:00', 1: '2022-11-30 13:48:00'})
s
0 2022-11-29 13:00:00
1 2022-11-30 13:48:00
dtype: object <-- chk dtype
Code代码
object to datetime object 到日期时间
pd.to_datetime(s)
result
0 2022-11-29 13:00:00
1 2022-11-30 13:48:00
dtype: datetime64[ns] <--chk dtype
convert result
to DD-MM-YYYY HH-mm-ss将
result
转换为 DD-MM-YYYY HH-mm-ss
pd.to_datetime(s).dt.strftime('%d-%m-%Y %H:%M:%S')
0 16-11-2022 13:00:00
1 17-11-2022 13:48:00
dtype: object <--chk dtype
convert result
to DD/MM/YYYY将
result
转换为 DD/MM/YYYY
pd.to_datetime(s).dt.strftime('%d/%m/%Y')
0 29/11/2022
1 30/11/2022
dtype: object <-- chk dtype
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.