简体   繁体   English

如何将 pandas dataframe 列中的两种不同日期格式转换为相同格式?

[英]How to convert two different date formats from a pandas dataframe column into same format?

I have two different date formats in a pandas column such as - DD-MM-YYYY and MM/DD/YYYY and I want to convert them into the same format.我在 pandas 列中有两种不同的日期格式,例如 - DD-MM-YYYYMM/DD/YYYY ,我想将它们转换为相同的格式。

I tried using the code -我尝试使用代码 -

data['SALE DATE'] = pd.to_datetime(data['SALE DATE']).dt.strftime('%m/%d/%Y')

but this converts the dates into DD/MM/YYYY and MM/DD/YYYY into the output - data['SALE DATE']但这会将日期转换为DD/MM/YYYYMM/DD/YYYY转换为 output - data['SALE DATE']

在此处输入图像描述

I want a python solution to overcome this problem.我想要一个 python 解决方案来克服这个问题。 Any leads will be very helpful.任何线索都会非常有帮助。

The most intuitive solution is to write a custom conversion function, someting like:最直观的解决方案是编写自定义转换 function,类似于:

def myDateConv(tt):
    sep = tt[2]
    if sep == '-':
        return pd.to_datetime(tt, format='%d-%m-%Y')
    elif sep == '/':
        return pd.to_datetime(tt, format='%m/%d/%Y')
    else:
        return tt

and then pass it as a converter for the column in question:然后将其作为相关列的转换器传递:

df = pd.read_csv('Input.csv', converters={'Date': myDateConv})

I prepared a CSV file, which read with read_csv without any custom converter gave the original content and both columns of object type:我准备了一个 CSV 文件,它使用read_csv读取,没有任何自定义转换器,给出了原始内容和object类型的两列:

         Date Input format
0  03-05-2020   DD-MM-YYYY
1  05/07/2020   MM/DD/YYYY

But reading the same file with the above converter gave:但是用上面的转换器读取相同的文件给出了:

        Date Input format
0 2020-05-03   DD-MM-YYYY
1 2020-05-07   MM/DD/YYYY

with Date column of datetime64[ns] type and both dates from May, just as intended.使用datetime64[ns]类型的Date列和从 5 月开始的两个日期,正如预期的那样。

Or if you have this DataFrame from other source and you want to convert this column, run:或者,如果您有来自其他来源的 DataFrame 并且想要转换此列,请运行:

df.Date = df.Date.apply(myDateConv)

If you are using pandas version 1.xx you can use the following solution:如果您使用的是 pandas 版本 1.xx,您可以使用以下解决方案:

pd.to_datetime(["11-08-2018", "05-03-2016", "08/30/2017", "09/21/2018"], infer_datetime_format=True, dayfirst=True).strftime("%m/%d/%Y")

This gives the following result:这给出了以下结果:

Index(['08/11/2018', '03/05/2016', '08/30/2017', '09/21/2018'], dtype='object')

... the important argument here is dayfirst=True . ...这里的重要论点是dayfirst=True

See pd.to_datetime docs for more.有关更多信息,请参阅pd.to_datetime文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandas to_datetime格式来自同一列的日期格式不同 - pandas to_datetime formats date from same column in different formats 如何将 PySpark/Pandas 数据框中的日期/自定义/通用格式的列值转换为日期格式? - How to convert column values present in date/custom/general formats in a PySpark/Pandas dataframe into a Date Format? Dataframe 具有不同格式的日期值的字符串列。 如何将整列数据转换为单个日期格式? - Dataframe has string column with date values in different formats. how to convert entire column data to single date format? 如何将数据框的日期和时间列转换为熊猫的datetime格式? - how to convert Date and time column of dataframe to datetime format of pandas? 将不同的日期格式转换为数据框列中的单一格式 - Converting different date formats to a single format in a dataframe column 在熊猫数据框中将字符串日期转换为其他格式 - Convert string date to a different format in pandas dataframe 如何合并格式相同但长度索引不同的两个熊猫数据框 - how to combine two pandas dataframe with same format but different length index 将 Pandas 列转换为相同的日期时间格式 - Convert Pandas column into same date time format 如何在pandas中以一种格式转换多种日期格式 - how to convert multiple date formats in one format in pandas 如何将熊猫数据框日期和不同的时间格式连接到单个时间戳? - How to concatenate pandas dataframe date and different time formats to single timestamp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM