我有以下数据集和函数来尝试检测列是否为日期类型。
from dateutil.parser import parse
import pandas as pd
# function
def is_date(string, fuzzy=False):
try:
parse(string, fuzzy=fuzzy)
return True
except ValueError:
return False
# data
df = pd.read_csv('https://data.calgary.ca/api/views/78gh-n26t/rows.csv?accessType=DOWNLOAD')
当我在其中一列上尝试该功能时is_date(crime['Date'])
I get:
TypeError: Parser must be a string or character stream, not Series
如何正确地将列转换为正确的类型,以便能够使用该函数循环遍历所有值?
日期栏:
0 05/01/2020 12:00:00 AM
1 05/01/2020 12:00:00 AM
2 04/01/2020 12:00:00 AM
3 04/01/2020 12:00:00 AM
4 04/01/2020 12:00:00 AM
其他栏目:
Sector Community Name Category
NORTHWEST 02E Assault (Non-domestic)
WEST ASPEN WOODS Street Robbery
NORTHWEST 02E Violence Other (Non-domestic)
NORTH 02K Theft OF Vehicle
NORTHEAST 10E Break & Enter - Commercial
您可以使用applymap
为了达成这个。
In [10]: df
Out[10]:
Sector Community Name Category Date
0 NORTHWEST 02E Assault (Non-domestic) 05/01/2020 12:00:00 AM
1 WEST ASPEN WOODS Street Robbery 05/01/2020 12:00:00 AM
2 NORTHWEST 02E Violence Other (Non-domestic) 05/01/2020 12:00:00 AM
3 NORTH 02K Theft OF Vehicle 05/01/2020 12:00:00 AM
4 NORTHEAST 10E Break & Enter - Commercial 05/01/2020 12:00:00 AM
In [11]: # function
...: def is_date(string, fuzzy=False):
...: try:
...: parse(string, fuzzy=fuzzy)
...: return True
...:
...: except ValueError:
...: return False
...:
In [12]: df = df.astype(str)
In [13]: df[df.columns.tolist()].applymap(is_date)
Out[13]:
Sector Community Name Category Date
0 False False False True
1 False False False True
2 False False False True
3 False False False True
4 False False False True
In [14]: df[df.columns.tolist()].applymap(is_date).any()
Out[14]:
Sector False
Community Name False
Category False
Date True
dtype: bool
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)