【问题标题】:How to convert numpy.timedelta64 to minutes如何将 numpy.timedelta64 转换为分钟
【发布时间】:2014-12-21 08:45:54
【问题描述】:

我在 Pandas DataFrame 中有一个日期时间列,我想将其转换为分钟或秒。

例如:我想将00:27:00 转换为 27 分钟。

example = data['duration'][0]
example

结果:numpy.timedelta64(1620000000000,'ns')

实现这一目标的最佳方法是什么?

【问题讨论】:

  • 我想你的意思是27 mins,而不是87 mins? o_O
  • 一般情况下,你也想要小数部分吗?例如。如果输入的 timedelta 是 00:27:45,你想要 27 还是 27.75?
  • 我对分钟更感兴趣,因为我的数据不包括秒,所以我不需要分数。
  • 我认为您可以通过执行以下行来获取分钟数:mins = np.array([data['duration']], dtype = "timedelta64[m]")[0]。如何将我在这个数组中收到的值附加到我的原始数据框中?
  • 在此处查看文档:在 0.15 中非常容易 pandas.pydata.org/pandas-docs/stable/…

标签: numpy pandas string-to-datetime


【解决方案1】:

使用array.astype()安全地转换数组的类型:

>>> import numpy as np
>>> a = np.timedelta64(1620000000000,'ns')
>>> a.astype('timedelta64[m]')
numpy.timedelta64(27,'m')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-14
    • 2014-09-18
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多