【问题标题】:pandas remove seconds from datetime index熊猫从日期时间索引中删除秒
【发布时间】:2017-02-09 21:13:29
【问题描述】:

我有一个名为“df”的熊猫数据框,如下所示

                       value    
2015-09-27 03:58:30    1.0  
2015-09-27 03:59:30    1.0  
2015-09-27 04:00:30    1.0  
2015-09-27 04:01:30    1.0

我只想抽出几秒钟来得到这个

                       value    
2015-09-27 03:58:00    1.0  
2015-09-27 03:59:00    1.0  
2015-09-27 04:00:00    1.0  
2015-09-27 04:01:00    1.0

我该怎么做?

我试过了

df.index.to_series().apply(datetime.replace(second=0, microsecond=0))

但我总是出错

TypeError: descriptor 'replace' of 'datetime.datetime' object needs an argument

【问题讨论】:

    标签: python pandas indexing seconds


    【解决方案1】:

    您可以使用datetime.replace 来更改第二个属性,如下所示:

    df.index = df.index.map(lambda x: x.replace(second=0))
    

    猜你喜欢
    • 2023-01-12
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 2021-10-11
    相关资源
    最近更新 更多