[英]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 将来可能会在没有任何警告的情况下更改。
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))
....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.