【问题标题】:how to detect datetime field in dataframe using regular expression in python如何使用python中的正则表达式检测数据框中的日期时间字段
【发布时间】:2020-11-18 11:59:42
【问题描述】:

我正在尝试返回数据框中类型为 datetime 的字段,然后将字段名称替换为“date”,以便将 datetime 拆分为年份和月份。

当我运行代码时它崩溃并显示以下错误:

   df = df.rename(columns={converteddate[0]: 'date'})
UnboundLocalError: local variable 'converteddate' referenced before assignment

代码:

import pandas as pd

df = pd.DataFrame({'event_type': ['watch movie ', 'stay at home', 'swimming','camping','meeting'], 
               'date': ['8/11/2020', '2/13/2020', '7/04/2020','1/22/2020','7/28/2020'],
                'event_mohafaza':['loc1','loc3','loc2','loc5','loc4'],
                 ' number_person ':[24,39,20,10,33],})
        
non_numeric_cols = [col for col, col_type in df.dtypes.iteritems() if col_type == 'object']
if len(non_numeric_cols) > 0:
         mask = df.astype(str).apply(lambda x : x.str.match('[0-3]?[0-9]-[0-3]?[0-9]-(?:[0-9]{2})?[0-9]{2}$').any())
            
         if mask.any() == True:
               df.loc[:,mask] = df.loc[:,mask].apply(pd.to_datetime,dayfirst=False)
               converteddate = [col for col in df.columns if df[col].dtype == 'datetime64[ns]']
         df = df.rename(columns={converteddate[0]: 'date'})
         if "date" in df.columns:
               df['year_month'] = df['date'].map(lambda x: x.strftime('%Y/%m'))

【问题讨论】:

    标签: python dataframe datetime strftime


    【解决方案1】:

    如果你修改你的正则表达式匹配有效:

    import pandas as pd
    
    df = pd.DataFrame({'event_type': ['watch movie ', 'stay at home', 'swimming','camping','meeting'], 
                   'date': ['8/11/2020', '2/13/2020', '7/04/2020','1/22/2020','7/28/2020'],
                    'event_mohafaza':['loc1','loc3','loc2','loc5','loc4'],
                     ' number_person ':[24,39,20,10,33],})
    
    print(df)
            
    non_numeric_cols = [col for col, col_type in df.dtypes.iteritems() if col_type == 'object']
    if len(non_numeric_cols) > 0:
        #mask = df.astype(str).apply(lambda x : x.str.match('[0-3]?[0-9]-[0-3]?[0-9]-(?:[0-9]{2})?[0-9]{2}$').any())
        mask = df.astype(str).apply(lambda x : x.str.match('^([1-9]|1[0-9]|2[0-9]|3[0-1])(.|-|/)([1-9]|1[0-2])(.|-|/)20[0-9][0-9]$').any())
                
        if mask.any() == True:
            df.loc[:,mask] = df.loc[:,mask].apply(pd.to_datetime,dayfirst=False)
            converteddate = [col for col in df.columns if df[col].dtype == 'datetime64[ns]']
        df = df.rename(columns={converteddate[0]: 'date'})
        if "date" in df.columns:
            df['year_month'] = df['date'].map(lambda x: x.strftime('%Y/%m'))
            
        print(df)
    

    产生

         event_type       date event_mohafaza   number_person 
    0  watch movie   8/11/2020           loc1               24
    1  stay at home  2/13/2020           loc3               39
    2      swimming  7/04/2020           loc2               20
    3       camping  1/22/2020           loc5               10
    4       meeting  7/28/2020           loc4               33
         event_type       date event_mohafaza   number_person  year_month
    0  watch movie  2020-08-11           loc1               24    2020/08
    1  stay at home 2020-02-13           loc3               39    2020/02
    2      swimming 2020-07-04           loc2               20    2020/07
    3       camping 2020-01-22           loc5               10    2020/01
    4       meeting 2020-07-28           loc4               33    2020/07
    

    您没有替换“日期”列,而是添加了“年月”列(我保持原样)。

    【讨论】:

    • 你的解决方案是对的,谢谢,但你能解释一下正则表达式模式吗?
    • 它非常接近你所拥有的,对日期数字的限制是相同的。我真正添加的是更通用的分隔符 (.|-|/),它允许任何分隔(但对于空格,也可以添加)。就是这样,你已经很接近了。
    猜你喜欢
    • 2019-01-13
    • 2020-08-16
    • 1970-01-01
    • 2018-12-25
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    相关资源
    最近更新 更多