【问题标题】:How to convert a date range object to an integer in Python如何在 Python 中将日期范围对象转换为整数
【发布时间】:2017-08-14 21:29:36
【问题描述】:

我正在尝试遍历数据框中的单个列“YEAR”,该数据框中的对象显示为“1998”和“2006-2008”。对于第二个示例,我想将它们转换为两者之间的整数平均值,例如“2007”。我该怎么做呢?

【问题讨论】:

  • 对于像2006-2007 这样的东西,你想要什么?或者这永远不会发生?
  • 你写过什么代码吗?
  • 如果是小数,我宁愿四舍五入到最接近的整数。
  • 我正在尝试实现我在@petermortenson 的另一篇文章中找到的代码basic linksh = open(fname) for line in h: if line.strip(): [int(next(h).strip()) for _ in range(4)] # list of integers

标签: python-3.x loops iteration


【解决方案1】:
import arrow

def date_to_int(value):
    time_zone_diff = 8 * 60 * 60
    return arrow.get(value).timestamp - time_zone_diff

【讨论】:

    猜你喜欢
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    相关资源
    最近更新 更多