截断日期时间对象熊猫

时间:2018-07-30 18:42:58

标签: python pandas datetime

我想从datetime对象中截断时间。我的最小示例如下:

    date  
 s1  7/26/2018 9:31       
 s2  7/26/2018 9:32      
 s3  7/26/2018 9:33       
 s4  7/26/2018 9:34      
 s5  7/26/2018 9:35       
 s6  7/26/2018 9:36     

data = [{'date': '7/26/2018 9:31'},{'date': '7/26/2018 9:32'}, 
{'date': '7/26/2018 9:33'},{'date': '7/26/2018 9:34'},
{'date': '7/26/2018 9:35'},{'date': '7/26/2018 9:36'}]

df = pd.DataFrame(data, index=['s1', 's2','s3','s4','s5','s6'])

我想从datetime对象中截断时间。我的目标看起来像这样:

    date  
 s1  7/26/2018       
 s2  7/26/2018       
 s3  7/26/2018        
 s4  7/26/2018       
 s5  7/26/2018        
 s6  7/26/2018      

3 个答案:

答案 0 :(得分:2)

方法1

您可以将其转换为日期时间,然后使用dt.strftime来设置所需的格式:

df['date'] = pd.to_datetime(df['date']).dt.strftime('%m/%d/%Y')

>>> df
          date
s1  07/26/2018
s2  07/26/2018
s3  07/26/2018
s4  07/26/2018
s5  07/26/2018
s6  07/26/2018

方法2

您可以使用正则表达式提取所需的字符串:

df['date'] = df['date'].str.extract('(\d+/\d+/\d+)')

>>> df
         date
s1  7/26/2018
s2  7/26/2018
s3  7/26/2018
s4  7/26/2018
s5  7/26/2018
s6  7/26/2018

答案 1 :(得分:2)

您也可以使用str.split:

方法3:

df.date = df.date.str.split().str[0]

时间

%timeit pd.to_datetime(df['date']).dt.strftime('%m/%d/%Y')
%timeit df['date'].str.extract('(\d+/\d+/\d+)')
%timeit df.date.str.split().str[0]

976 µs ± 5.08 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
322 µs ± 5.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
192 µs ± 1.41 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

答案 2 :(得分:2)

您只需要:

pd.to_datetime(df.date).dt.date

输出:

s1    2018-07-26
s2    2018-07-26
s3    2018-07-26
s4    2018-07-26
s5    2018-07-26
s6    2018-07-26
Name: date, dtype: object