简体   繁体   English

将 dtype('O') 转换为日期 DD/MM/YYYY

[英]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') 的表格

me sheet我的表

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-ssresult转换为 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/YYYYresult转换为 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.

相关问题 将长日期时间转换为日期 (dd/mm/yyyy) - Converting long datetime into date (dd/mm/yyyy) Pandas - 将日期列从 dd/mm/yy hh:mm:ss 转换为 yyyy-mm-dd hh:mm:ss - Pandas - Converting date column from dd/mm/yy hh:mm:ss to yyyy-mm-dd hh:mm:ss pandas 数据框将复杂的日期格式转换为日期 dd/mm/yyyy hh:mm - pandas dataframe converting complex date-format to date dd/mm/yyyy hh:mm 熊猫将日期字符串从mm / d / yyyy和mm / dd / yyyy格式转换为dd.mm.yyyy - Pandas transform date string from format mm/d/yyyy and mm/dd/yyyy to dd.mm.yyyy 将日期格式从 csv. 文件中的 python 从 YYYY-MM-DD HH:MM:SS+00:00 转换为 YYYY-MM-DD - Converting a date format from a csv. file in python from YYYY-MM-DD HH:MM:SS+00:00 to YYYY-MM-DD 将日期列拆分为 YYYY.MM.DD - Split date column into YYYY.MM.DD 在pandas数据帧中将不同的日期时间格式转换为MM / DD / YYYY格式 - Converting different date time formats to MM/DD/YYYY format in pandas dataframe 日期格式 yyyy-mm-ddTHH:mm+HH:mm 到 mm/dd/yyyy python - date format yyyy-mm-ddTHH:mm+HH:mm to mm/dd/yyyy python Pandas,格式日期从 dd/mm/yyyy 到 MMM dd/yy - Pandas, format date from dd/mm/yyyy to MMM dd/yy 将python中索引的日期格式从yyyy-dd-mm更改为yyyy-mm-dd - Change date format for an index in python from yyyy-dd-mm to yyyy-mm-dd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM