在 Python 中转换“.to_datetime”时出现问题
Problems in converting ".to_datetime" in Python
我有以下列表:
l = [<div class="date">8 December 2004</div>,
<div class="date">6 December 2004</div>,
<div class="date">18 October 2004</div>,
<div class="date">9 October 2004</div>,
<div class="date">8 August 2004</div>,
<div class="date">18 June 2004</div>,
<div class="date">23 December 2005</div>,
<div class="date">19 December 2005</div>,
<div class="date">19 December 2005</div>,
<div class="date">15 December 2005</div>]
我想将其转换为具有 to.datetime
格式的 Date
列的数据框。
我尝试了很多解决方案(见下面的一个),但我无法理解它。
pd.to_datetime(pd.DataFrame({'Date':l}), format = '%d %B %Y')
谁能帮帮我?
谢谢!
通过BeautifulSoup
提取标签内的文本,然后转换为日期时间:
from bs4 import BeautifulSoup
df = pd.DataFrame({'Date':[ BeautifulSoup(x, features="lxml").text for x in l]})
df['Date'] = pd.to_datetime(df['Date'], format = '%d %B %Y')
print (df)
Date
0 2004-12-08
1 2004-12-06
2 2004-10-18
3 2004-10-09
4 2004-08-08
5 2004-06-18
6 2005-12-23
7 2005-12-19
8 2005-12-19
9 2005-12-15
如果您使用 BeautifulSoup
抓取它,您应该可以为您的系列调用以下命令。
pd.to_datetime(pd.Series([e.text for e in l]))
但如果它实际上已经是一个字符串,您将需要从 div
中提取日期。然后你可能想要删除 div
标签:
import re
pd.to_datetime(pd.Series([re.sub(r'<\/?div.*?>', '', s) for s in l]))
或者,您可以使用正则表达式提取日期本身,例如 \d{1,2} \w+ \d{4}
。
注意不需要编译。对于像大多数 Pandas 脚本这样的短脚本,根据 re
模块 documentation.
编译和缓存正则表达式
The compiled versions of the most recent patterns passed to re.compile()
and the module-level matching functions are cached, so programs that use only a few regular expressions at a time needn’t worry about compiling regular expressions.
我有以下列表:
l = [<div class="date">8 December 2004</div>,
<div class="date">6 December 2004</div>,
<div class="date">18 October 2004</div>,
<div class="date">9 October 2004</div>,
<div class="date">8 August 2004</div>,
<div class="date">18 June 2004</div>,
<div class="date">23 December 2005</div>,
<div class="date">19 December 2005</div>,
<div class="date">19 December 2005</div>,
<div class="date">15 December 2005</div>]
我想将其转换为具有 to.datetime
格式的 Date
列的数据框。
我尝试了很多解决方案(见下面的一个),但我无法理解它。
pd.to_datetime(pd.DataFrame({'Date':l}), format = '%d %B %Y')
谁能帮帮我?
谢谢!
通过BeautifulSoup
提取标签内的文本,然后转换为日期时间:
from bs4 import BeautifulSoup
df = pd.DataFrame({'Date':[ BeautifulSoup(x, features="lxml").text for x in l]})
df['Date'] = pd.to_datetime(df['Date'], format = '%d %B %Y')
print (df)
Date
0 2004-12-08
1 2004-12-06
2 2004-10-18
3 2004-10-09
4 2004-08-08
5 2004-06-18
6 2005-12-23
7 2005-12-19
8 2005-12-19
9 2005-12-15
如果您使用 BeautifulSoup
抓取它,您应该可以为您的系列调用以下命令。
pd.to_datetime(pd.Series([e.text for e in l]))
但如果它实际上已经是一个字符串,您将需要从 div
中提取日期。然后你可能想要删除 div
标签:
import re
pd.to_datetime(pd.Series([re.sub(r'<\/?div.*?>', '', s) for s in l]))
或者,您可以使用正则表达式提取日期本身,例如 \d{1,2} \w+ \d{4}
。
注意不需要编译。对于像大多数 Pandas 脚本这样的短脚本,根据 re
模块 documentation.
The compiled versions of the most recent patterns passed to
re.compile()
and the module-level matching functions are cached, so programs that use only a few regular expressions at a time needn’t worry about compiling regular expressions.