pandas 在python pandas数据帧中将字符串转换为日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45714149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:15:39  来源:igfitidea点击:

Convert string to date format in python pandas dataframe

pythonpandas

提问by El_Patrón

I have a data set the the following date format in a pandas data frame:

我在 Pandas 数据框中设置了以下日期格式的数据:

warnings = pd.read_csv('output.csv', sep=',')
warnung['from']

7      Di, 15. Aug, 21:52 Uhr
8      Di, 15. Aug, 22:46 Uhr
9      Di, 15. Aug, 22:46 Uhr
10     Di, 15. Aug, 21:52 Uhr
11     Di, 15. Aug, 22:46 Uhr
12     Di, 15. Aug, 21:52 Uhr
13     Di, 15. Aug, 22:46 Uhr
14     Di, 15. Aug, 21:52 Uhr
15     Di, 15. Aug, 22:46 Uhr

Here my question, how can I convert this to a legible date format in pandas. I want to compare if the actual date of today and match this to date from my data-set.

我的问题是,如何将其转换为 Pandas 中清晰的日期格式。我想比较今天的实际日期是否与我的数据集中的日期相匹配。

I would like to have, e.g.

我想要,例如

15.08.2017, 22:46:00

or in a more convenient format. Then I want to compare the actual date against the dates in my data set.

或更方便的格式。然后我想将实际日期与我的数据集中的日期进行比较。

How can I do this within a pandas DataFrame.

如何在 Pandas DataFrame 中执行此操作。

Thanks for any help.

谢谢你的帮助。

回答by jezrael

I think you need to_datetime, but first remove first 4 and last 4 chars by indexing with strand raddfor 2017year:

我认为你需要to_datetime,但首先删除第4个和最后4个字符的索引与STRradd2017一年:

df['new'] = pd.to_datetime(df['from'].str[4:-4].radd('2017-'), format='%Y-%d. %b, %H:%M')
print (df)
                     from                 new
0  Di, 15. Aug, 21:52 Uhr 2017-08-15 21:52:00
1  Di, 15. Aug, 22:46 Uhr 2017-08-15 22:46:00
2  Di, 15. Aug, 22:46 Uhr 2017-08-15 22:46:00
3  Di, 15. Aug, 21:52 Uhr 2017-08-15 21:52:00
4  Di, 15. Aug, 22:46 Uhr 2017-08-15 22:46:00
5  Di, 15. Aug, 21:52 Uhr 2017-08-15 21:52:00
6  Di, 15. Aug, 22:46 Uhr 2017-08-15 22:46:00
7  Di, 15. Aug, 21:52 Uhr 2017-08-15 21:52:00
8  Di, 15. Aug, 22:46 Uhr 2017-08-15 22:46:00

Last for compare with today date use boolean indexingwith datefor convert pandas datetimes to python dates:

最后用今天的日期比较使用boolean indexingdate用于转换大Pandas日期时间到Python日期:

today_date = pd.datetime.today().date()

df1 = df[df['new'].dt.date == today_date]

回答by Clusks

Here's my attempt at it, I think it should work, although I'm not sure on the process you want to use for checking if it's the current date.

这是我的尝试,我认为它应该可以工作,尽管我不确定您要用于检查它是否是当前日期的过程。

The first part will tidy things up slightly and take the string of every row and convert it to a date time object.

第一部分将稍微整理一下并获取每一行的字符串并将其转换为日期时间对象。

The second part of this that does check will spit out a column that gives either True/False based on your system clock for each row. This was done with python 3.5.2.

进行检查的第二部分将吐出一列,根据每行的系统时钟给出真/假。这是用 python 3.5.2 完成的。

import string
import pandas as pd
import datetime

#Converts each string into a datetime object
def convert_date(row):
    trim_date = row[4:-4]
    remove_punc = trim_date.translate(trim_date.maketrans('','',string.punctuation))
    return datetime.datetime.strptime('2017 ' + remove_punc, '%Y %d %b %H%M')

df['datetime_convert'] = df['from'].apply(convert_date)

#Creates column to check if every value matches the current time on your system
def check_is_now(row):
    if str(row) == datetime.datetime.today().strftime('%Y-%m-%d %H:%M:00')::
        return True
    else:
        return False


df['is_now'] = df['datetime_convert'].apply(check_is_now)