【问题标题】:pandas how to truncate minutes, seconds pandas.tslib.Timestamp熊猫如何截断分钟,秒 pandas.tslib.Timestamp
【发布时间】:2016-07-08 16:13:10
【问题描述】:

我使用 Cloudera VM 5.2 和 pandas 0.18.0。

我有以下数据

adclicksDF = pd.read_csv('/home/cloudera/Eglence/ad-clicks.csv',
               parse_dates=['timestamp'],
       skipinitialspace=True).assign(adCount=1)

adclicksDF.head(n=5)
Out[107]: 
            timestamp  txId  userSessionId  teamId  userId  adId   adCategory  \
0 2016-05-26 15:13:22  5974           5809      27     611     2  electronics   
1 2016-05-26 15:17:24  5976           5705      18    1874    21       movies   
2 2016-05-26 15:22:52  5978           5791      53    2139    25    computers   
3 2016-05-26 15:22:57  5973           5756      63     212    10      fashion   
4 2016-05-26 15:22:58  5980           5920       9    1027    20     clothing   



   adCount  
0        1  
1        1  
2        1  
3        1  
4        1  

数据类型字段是

for col in adclicksDF:
    print(col)
    print(type(adclicksDF[col][1]))


timestamp
<class 'pandas.tslib.Timestamp'>
txId
<class 'numpy.int64'>
userSessionId
<class 'numpy.int64'>
teamId
<class 'numpy.int64'>
userId
<class 'numpy.int64'>
adId
<class 'numpy.int64'>
adCategory
<class 'str'>
adCount
<class 'numpy.int64'>

我想截断时间戳中的分钟和秒。

我试过了

adclicksDF["timestamp"] = pd.to_datetime(adclicksDF["timestamp"],format='%Y-%m-%d %H')

adclicksDF.head(n=5)
Out[110]: 
            timestamp  txId  userSessionId  teamId  userId  adId   adCategory  \
0 2016-05-26 15:13:22  5974           5809      27     611     2  electronics   
1 2016-05-26 15:17:24  5976           5705      18    1874    21       movies   
2 2016-05-26 15:22:52  5978           5791      53    2139    25    computers   
3 2016-05-26 15:22:57  5973           5756      63     212    10      fashion   
4 2016-05-26 15:22:58  5980           5920       9    1027    20     clothing   

   adCount  
0        1  
1        1  
2        1  
3        1  
4        1  

这不会截断分钟和秒。

如何截断分钟和秒?

【问题讨论】:

    标签: python pandas timestamp truncate seconds


    【解决方案1】:

    你可以使用:

    adclicksDF["timestamp"] = pd.to_datetime(adclicksDF["timestamp"])
                                .apply(lambda x: x.replace(minute=0, second=0))
    
    
    print (adclicksDF)
                timestamp  txId  userSessionId  teamId  userId  adId   adCategory
    0 2016-05-26 15:00:00  5974           5809      27     611     2  electronics
    1 2016-05-26 15:00:00  5976           5705      18    1874    21       movies
    2 2016-05-26 15:00:00  5978           5791      53    2139    25    computers
    3 2016-05-26 15:00:00  5973           5756      63     212    10      fashion
    4 2016-05-26 15:00:00  5980           5920       9    1027    20     clothing
    
    print (type(adclicksDF.ix[0, 'timestamp']))
    <class 'pandas.tslib.Timestamp'>
    

    如果需要输出为string,请使用dt.strftime

    adclicksDF["timestamp"] = pd.to_datetime(adclicksDF["timestamp"]).dt.strftime('%Y-%m-%d %H')
    print (adclicksDF)
           timestamp  txId  userSessionId  teamId  userId  adId   adCategory
    0  2016-05-26 15  5974           5809      27     611     2  electronics
    1  2016-05-26 15  5976           5705      18    1874    21       movies
    2  2016-05-26 15  5978           5791      53    2139    25    computers
    3  2016-05-26 15  5973           5756      63     212    10      fashion
    4  2016-05-26 15  5980           5920       9    1027    20     clothing
    
    print (type(adclicksDF.ix[0, 'timestamp']))
    <class 'str'>
    

    编辑:

    更好的解决方案是使用dt.floor 就像回答Alex

    【讨论】:

    • 看看 Alex 的答案 - 您可以将其添加到您的答案中,因为它是一种更优雅、更快的解决方案。
    • @pansen - 我不会这样做,因为无法复制其他答案。但我同意这是更好的解决方案。
    • 对,你是对的。但是您可以参考该解决方案作为遇到此问题的新用户的提示。
    【解决方案2】:

    pd.Timestamp 从 0.18 开始就有floor 解析方法

    adclicksDF["timestamp"] = adclicksDF.timestamp.dt.floor('h')
    

    【讨论】:

    • 这应该是首选的解决方案,因为它更优雅(避免使用 apply 并使用内置的 pandas floor)并且速度大约快 50 倍。
    【解决方案3】:

    试试:

    pd.to_datetime(adclicksDF.timestamp).dt.strftime('%Y-%m-%d %H')
    

    分配后:

    adclicksDF.timestamp = pd.to_datetime(adclicksDF.timestamp).dt.strftime('%Y-%m-%d %H')
    
    adclicksDF
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-16
      • 2014-11-30
      • 2018-03-24
      • 1970-01-01
      • 2021-07-01
      • 2014-08-29
      • 1970-01-01
      相关资源
      最近更新 更多