Convert datetime.datetime object to days since epoch in Python

后端 未结 3 1527
没有蜡笔的小新
没有蜡笔的小新 2021-01-04 18:34

I\'ve got a pandas.Series object that might look like this:

import pandas as pd
myVar = pd.Series([\"VLADIVOSTOK 690090\", \"MAHE\", NaN, NaN, \         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 19:00

    You can convert this to seconds since epoch first, then divide it out by the amount of seconds in a day (86,400 seconds in a day). Please note the integer division here - will not return a float.

    from datetime import datetime
    now = datetime.now()
    seconds = now.strftime("%s") # seconds since epoch
    days = int(seconds) / 86400 # days since epoch
    

    I added the import and now as an example of a datetime object I can play with.

提交回复
热议问题