Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datetime.datetime object to days since epoch in Python

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

import pandas as pd
myVar = pd.Series(["VLADIVOSTOK 690090", "MAHE", NaN, NaN, "VLADIVOSTOK 690090", "2000-07-01 00:00:00"])

myVar[5] is parsed as a datetime.datetime object when the data is read into Python via pandas. I'm assuming that converting this value to the number of days since epoch (36708) isn't difficult at all. I'm just new to Python and don't know how to do it. Thanks in advance!

like image 876
tblznbits Avatar asked Jul 28 '15 21:07

tblznbits


People also ask

How do I convert datetime to epoch time?

Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.

How do I convert datetime to days in Python?

Python datetime. date(year, month, day) :MINYEAR <= year <= MAXYEAR. 1 <= month <= 12. 1 <= day <= number of days in the given month and year.

How do you convert datetime to seconds since epoch Python?

To convert a datetime to seconds, subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds() method of a timedelta object to get the number of seconds since the epoch.


4 Answers

I'm not sure where you're getting 36,708 days since the epoch (it's only been 16,644 days since January 1, 1970), but datetime.timedelta objects (used in date arithmetic) have a days attribute:

>>> import datetime
>>> (datetime.datetime.utcnow() - datetime.datetime(1970,1,1)).days
16644
like image 57
TigerhawkT3 Avatar answered Sep 29 '22 13:09

TigerhawkT3


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.

like image 35
bakhtiya Avatar answered Sep 29 '22 13:09

bakhtiya


myVar = pd.Series(["VLADIVOSTOK 690090", "MAHE", "NaN", "NaN", "VLADIVOSTOK 690090", "2000-07-01 00:00:00"])

myVar[5] = pd.to_datetime(myVar[5]) - pd.datetime(1970,1,1)

print(myVar)
0     VLADIVOSTOK 690090
1                   MAHE
2                    NaN
3                    NaN
4     VLADIVOSTOK 690090
5    11139 days 00:00:00
dtype: object
like image 24
Padraic Cunningham Avatar answered Sep 29 '22 11:09

Padraic Cunningham


For a Pandas Dataframe:

df_train["DaysSinceEpoch"] = [i.days for i in df_train["date"] - datetime.datetime(1970, 1, 1)]

Assuming that you want days since Unix Epoch of 1970-01-01 and you have a column of Pythonic datetime64[ns].

And see my other answer with the exact reverse.

like image 42
Contango Avatar answered Sep 29 '22 13:09

Contango