【问题标题】:AttributeError: 'numpy.timedelta64' object has no attribute 'total_seconds' with PandasAttributeError:“numpy.timedelta64”对象在 Pandas 中没有属性“total_seconds”
【发布时间】:2020-06-14 16:11:21
【问题描述】:

我正在尝试将 sniffTime 转换为秒数。我已经看过Convert timedelta64[ns] column to seconds in Python Pandas DataFrame 但是那个解决方案不起作用。我认为也许熊猫线可能有问题。

print(sniffTime)
print(type(sniffTime))

输出:

821693000 nanoseconds
<class 'numpy.timedelta64'>

错误

AttributeError: 'numpy.timedelta64' object has no attribute 'total_seconds'

在线:

df['PerSec']=df['PerSec'].div(sniffTime.total_seconds())

【问题讨论】:

    标签: python pandas numpy


    【解决方案1】:

    您可以将 numpy 标量转换为Timedelta,因此可以使用Timedelta.total_seconds

    df['PerSec']=df['PerSec'].div(pd.Timedelta(sniffTime).total_seconds())
    

    【讨论】:

      猜你喜欢
      • 2017-11-26
      • 2017-02-12
      • 2019-07-19
      • 2015-09-06
      • 2019-07-03
      • 1970-01-01
      • 2017-03-14
      • 2019-09-25
      • 2014-01-10
      相关资源
      最近更新 更多