Windows上日期为1970-01-01之前的datetime timestamp()的变通办法 [英] Workaround for datetime timestamp() on windows for dates preceeding 1970-01-01

查看:278
本文介绍了Windows上日期为1970-01-01之前的datetime timestamp()的变通办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试通过将日期转换为时间戳来在数据集中生成数字特征。如果在Mac上运行,它将完美运行,在Windows上会抛出

I am currently trying to generate numerical features in a dataset by converting the dates to timestamps. If run on Mac, it works flawlessly, on windows it throws an

OS Error: [Errno 22] Invalid argument

可能是由于Windows 不支持1970-01-01之前的unix时间戳。我的约会日期从1955年开始。这是我的代码:

which is probably due to windows not supporting unix timestamps from before 1970-01-01. I have dates from 1955 upwards. Here is my code:

import time
import datetime

current_timestamp = time.time()
df.loc[:, "FEATURE_num"] = df["FEATURE"].apply(lambda d: datetime.datetime.strptime(d, '%Y-%m-%d').timestamp() if isinstance(d, str) else current_timestamp)

我在某处看到建议使用 datetime.timedelta(),但我不知道如何集成它。

I somewhere saw suggested to maybe use datetime.timedelta(), but I couldn't figure out how to integrate it.

推荐答案

您可以(隐式)使用 datetime.timedelta 来计算 Gregorian对于从1582年10月15日到现在的日期(或您要使用的其他时代)有效的时间戳。

You could do it by (implicitly) using datetime.timedelta to calculate a "Gregorian" timestamp that would be valid for dates from 1582-Oct-15 to the present (or some other "epoch" you would like to use).

如函数的文档字符串所示,日期字符串默认情况下,将使用'%Y-%m-%d' strptime 之类的格式字符串参数,但这可以是

As the function's docstring indicates, date strings will, by default, be parsed using a '%Y-%m-%d' strptime-like format string parameter, but that can be overridden.

from datetime import datetime


GREGORIAN_EPOCH = datetime.strptime('1582-10-15', '%Y-%m-%d')


def gregorian_timestamp(date, format='%Y-%m-%d'):
    """ Calculate timestamp using start of Gregorian calender as epoch.

        The date parameter can be either a string or a datetime.datetime
        object. Strings will be parsed using the '%Y-%m-%d' format by default
        unless a different one is specfied via the optional format parameter.
    """
    try:
        date = datetime.strptime(date, format)
    except TypeError:
        pass
    return (date - GREGORIAN_EPOCH).total_seconds()  # The timedelta in seconds.


if __name__ == '__main__':

    current_date = datetime.now()
    timestamp = gregorian_timestamp(current_date)
    print('gregorian timestamp:', timestamp)  # -> gregorian timestamp: 13768250461.136208

    timestamp = gregorian_timestamp('1970-01-01')
    print('gregorian timestamp:', timestamp)  # -> gregorian timestamp: 12219292800.0

    timestamp = gregorian_timestamp('1955-02-28')
    print('gregorian timestamp:', timestamp)  # -> gregorian timestamp: 11750918400.0

    timestamp = gregorian_timestamp('1582-10-15')
    print('gregorian timestamp:', timestamp)  # -> gregorian timestamp: 0.0

这篇关于Windows上日期为1970-01-01之前的datetime timestamp()的变通办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆