第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

對(duì)于 1970-01-01 之前的日期,Windows 上的 datetime timestamp

對(duì)于 1970-01-01 之前的日期,Windows 上的 datetime timestamp

阿晨1998 2021-10-26 15:59:57
我目前正在嘗試通過將日期轉(zhuǎn)換為時(shí)間戳來在數(shù)據(jù)集中生成數(shù)字特征。如果在 Mac 上運(yùn)行,它可以完美運(yùn)行,在 Windows 上它會(huì)拋出:OS Error: [Errno 22] Invalid argument這可能是由于 Windows不支持 1970-01-01 之前的 unix 時(shí)間戳。我有 1955 年以上的日期。這是我的代碼:import timeimport datetimecurrent_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(),但我不知道如何集成它。
查看完整描述

1 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以通過(隱式)使用datetime.timedelta來計(jì)算“格里高利”時(shí)間戳,該時(shí)間戳對(duì)從 1582 年 10 月 15 日至今(或您想使用的其他“紀(jì)元”)的日期有效。


作為函數(shù)的文檔字符串表示,日期字符串會(huì)在默認(rèn)情況下,可以用一個(gè)解析'%Y-%m-%d' strptime式的格式字符串參數(shù),但可以被覆蓋。


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



查看完整回答
反對(duì) 回復(fù) 2021-10-26
  • 1 回答
  • 0 關(guān)注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)