代码之家  ›  专栏  ›  技术社区  ›  Chris

python-将多个字符串datetimes格式转换为特定的日期格式

  •  0
  • Chris  · 技术社区  · 4 年前

    df = pd.DataFrame({'date': {0: '2020-03-21T10:13:08',  1: '2020-03-21T17:43:03',  2: '2020-03-21T13:13:30',  3: '2020-03-21T20:43:02',  4: '3/8/20 5:31',  5: '3/8/20 5:19',  6: '3/22/20 23:45',  7: '3/22/20 23:45',  8: '2/1/2020 11:53',  9: '2/1/2020 10:53',  10: '1/31/2020 15:20',  11: '1/31/2020 10:37',  12: '2020-04-04 23:34:21',  13: '2020-04-04 23:34:21'}}, 
                 index=range(0,14))
    

    我需要将所有这些不同的日期时间字符串转换为日期格式。我使用的方法是:

    1. 找到第一个空格并提取日期

    2. 在给定特定字符串长度的情况下更改其格式(每个字符串长度都有其特定的日期格式,如下所示 format 参数)

    3. df .

    您可以在这里看到这种方法:

    df.loc[df["date"].str.find(" ") == 10, "date"] = pd.to_datetime(df.loc[df["date"].str.find(" ") == 10, "date"].str[0:10])
    df.loc[df["date"].str.find(" ") == -1, "date"] = pd.to_datetime(df.loc[df["date"].str.find(" ") == 10, "date"].str[0:10])
    df.loc[df["date"].str.find(" ") == 6, "date"] = pd.to_datetime(df.loc[df["date"].str.find(" ") == 6, "date"].str[0:6], format="%m/%d/%y")
    df.loc[df["date"].str.find(" ") == 7, "date"] = pd.to_datetime(df.loc[df["date"].str.find(" ") == 7, "date"].str[0:7], format="%m/%d/%y")
    df.loc[df["date"].str.find(" ") == 8, "date"] = pd.to_datetime(df.loc[df["date"].str.find(" ") == 8, "date"].str[0:8], format="%m/%d/%Y")
    df.loc[df["date"].str.find(" ") == 9, "date"] = pd.to_datetime(df.loc[df["date"].str.find(" ") == 9, "date"].str[0:9], format="%m/%d/%Y")
    

    顺便说一下,它必须是可伸缩的(每个格式字符串有很多行)

    2 回复  |  直到 4 年前
        1
  •  2
  •   jezrael    4 年前

    对于我来说,将所有值转换为datetimes,然后使用 Series.dt.floor Series.dt.date 如果输出是python日期:

    df['date'] = pd.to_datetime(df['date']).dt.floor('d')
    #dates
    #df['date'] = pd.to_datetime(df['date']).dt.date
    print (df)
             date
    0  2020-03-21
    1  2020-03-21
    2  2020-03-21
    3  2020-03-21
    4  2020-03-08
    5  2020-03-08
    6  2020-03-22
    7  2020-03-22
    8  2020-02-01
    9  2020-02-01
    10 2020-01-31
    11 2020-01-31
    12 2020-04-04
    13 2020-04-04
    

    df['date'] = pd.to_datetime(df['date'].str[:10].str.split().str[0])
    
        2
  •  1
  •   Gustavo Gradvohl    4 年前
    import pandas as pd
    
    df = pd.DataFrame({'date': {0: '2020-03-21T10:13:08',  1: '2020-03-21T17:43:03',  2: '2020-03-21T13:13:30',  3: '2020-03-21T20:43:02',  4: '3/8/20 5:31',  5: '3/8/20 5:19',  6: '3/22/20 23:45',  7: '3/22/20 23:45',  8: '2/1/2020 11:53',  9: '2/1/2020 10:53',  10: '1/31/2020 15:20',  11: '1/31/2020 10:37',  12: '2020-04-04 23:34:21',  13: '2020-04-04 23:34:21'}}, 
                 index=range(0,14))
    df
        date
    0   2020-03-21T10:13:08
    1   2020-03-21T17:43:03
    2   2020-03-21T13:13:30
    3   2020-03-21T20:43:02
    4   3/8/20 5:31
    5   3/8/20 5:19
    6   3/22/20 23:45
    7   3/22/20 23:45
    8   2/1/2020 11:53
    9   2/1/2020 10:53
    10  1/31/2020 15:20
    11  1/31/2020 10:37
    12  2020-04-04 23:34:21
    13  2020-04-04 23:34:21
    
    df['date'] = pd.to_datetime(df['date'])
    df
        date
    0   2020-03-21 10:13:08
    1   2020-03-21 17:43:03
    2   2020-03-21 13:13:30
    3   2020-03-21 20:43:02
    4   2020-03-08 05:31:00
    5   2020-03-08 05:19:00
    6   2020-03-22 23:45:00
    7   2020-03-22 23:45:00
    8   2020-02-01 11:53:00
    9   2020-02-01 10:53:00
    10  2020-01-31 15:20:00
    11  2020-01-31 10:37:00
    12  2020-04-04 23:34:21
    13  2020-04-04 23:34:21