简体   繁体   English

DataFrame 包含一列具有以下类型的日期:“'5-15-2019'”和 05152021。我想提取它的模式

[英]DataFrame contains a column of dates which are having these types: "'5-15-2019'" and 05152021.I want to extract pattern of it

DataFrame contains dates which are having these types: "21-10-2021" and 29052021.I want to extract pattern of it. DataFrame 包含具有以下类型的日期:“21-10-2021”和 29052021。我想提取它的模式。 for example '5-15-2019',it needs to produce '%d-%m-%Y' '05152021' it needs to produce '%d%m%Y'例如 '5-15-2019',它需要生成 '%d-%m-%Y' '05152021' 它需要生成 '%d%m%Y'

i tried in this way:我这样试过:

search6=[]
for val in list(df.apply(lambda x:re.search('(?:[1-9]|[12][0-9]|3[01])[-](?:[1-9]|10|11|12])[-]\d{2,4}',str(x)))):
if val:
li=val.group()
search6.append(li)
print(search6)

output: i got a list of those patterns.i need to get pattern '%d-%m-%Y' and Similarly i need to get pattern for '%d%m%Y' also.how i need to do it? output:我得到了这些模式的列表。我需要获取模式“%d-%m-%Y”,同样我还需要获取“%d%m%Y”的模式。我需要怎么做? can any body help me.Thank you任何人都可以帮助我。谢谢

You can use the internal pandas method pandas._libs.tslibs.parsing.guess_datetime_format .您可以使用内部 pandas 方法pandas._libs.tslibs.parsing.guess_datetime_format Be careful, this is not part of the public API, so the function might change without any warning in the future.请注意,这不是公共 API 的一部分,因此 function 将来可能会在没有任何警告的情况下更改。

option 1选项1
from pandas._libs.tslibs.parsing import guess_datetime_format
s = pd.Series(['21-10-2021', '29052021', '5-15-2019', '05152021', '20000101', '01-01-2001'])

s.map(lambda x: guess_datetime_format(x, dayfirst=True))
option 2选项 2

....YYYY dates are not supported. ....YYYY日期。 For those you need to cheat by adding dashes temporarily:对于那些你需要通过临时添加破折号来作弊的人:

def parse(x):
    out = guess_datetime_format(x, dayfirst=True)
    if out is None and x.isdigit() and len(x)==8:
        out = (guess_datetime_format(f'{x[:2]}-{x[2:4]}-{x[4:]}',
                                     dayfirst=True)
               .replace('-', '')
              )
    return out

s.map(parse)

Example:例子:

         date   option1   option2
0  21-10-2021  %d-%m-%Y  %d-%m-%Y
1    29052021      None    %d%m%Y
2   5-15-2019  %m-%d-%Y  %m-%d-%Y
3    05152021      None    %m%d%Y
4    20000101    %Y%m%d    %Y%m%d
5  01-01-2001  %d-%m-%Y  %d-%m-%Y

暂无
暂无

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

相关问题 将包含整数的数据框列转换为日期 - convert dataframe column which contains integers to dates 如何使用 pandas 从遵循重复模式的 dataframe 列中提取数字? - How do I extract numbers from a dataframe column which follows a recurring pattern using pandas? 我想将 dataframe 转换为列表列表,其中包含第一个列表中的列名和其他列表中的数据 - i want to convert dataframe into list of list which contains column name in first list and data in others dataframe 中的一列具有字典列表我想将其转换为单独的列 - a column in a dataframe having list of dictionary i want to convert that into seperate column 如何从不同格式的列中提取日期? - How can i extract dates from the column which are in different format? 我想在我的数据帧中将1-May-19和5/1/2019转换为1/5/2019 - i want to convert 1-May-19 and 5/1/2019 to 1/5/2019 in my dataframe 在数据框中,我有一个这样的列。 9(05) X(5) X(15) X(15) X(15) S9(07) S9(2)V9(2) 我想把它们分成四个不同的列 - In a dataframe I had a column like this. 9(05) X(5) X(15) X(15) X(15) S9(07) S9(2)V9(2) I want to split them into four different into columns 包含不同数据类型的数据框总和 - Sum dataframe column that contains different data types 从 dataframe 列中提取格式日期 - Extract format dates from dataframe column 一个 dataframe 有字典,我想要 dataframe 这样字典的键变成行和值是列 - a dataframe having dictionary , i want dataframe in such a way that dictionary's key becomes the rows and value being the column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM