19

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!

smci
  • 32,567
  • 20
  • 113
  • 146
tblznbits
  • 6,602
  • 6
  • 36
  • 66
  • related: [Converting datetime.date to UTC timestamp in Python](http://stackoverflow.com/q/8777753/4279) – jfs May 05 '16 at 16:02

4 Answers4

42

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
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 2
    I'm sorry, I might have misspoke. You're right that epoch for Unix systems is January 1st, 1970. I'm working with Excel sheets, so the "epoch" is technically January 1st, 1900, but in practice is December 30th, 1899 in order to correct for it not being a leap year. Nevertheless, your solution works. – tblznbits Jul 28 '15 at 21:44
  • 1
    Ah, I see. In any case, you can see that the epoch date is easily changed. You can use whatever reference date you want in place of `1970, 1, 1`. – TigerhawkT3 Jul 28 '15 at 21:48
  • 1
    @brittenb: related: [How to convert a given ordinal number (from Excel) to a date](http://stackoverflow.com/q/29387137/4279) – jfs May 05 '16 at 16:01
  • 1
    I like this answer. A little nicer to read: `from datetime import datetime; (datetime.today() - datetime.fromtimestamp(0)).days`. – bers Nov 03 '22 at 16:14
5
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
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
4

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.

bakhtiya
  • 89
  • 5
  • Alternatively using `from time import time`: `time() // (24 * 60 * 60)`. That’s `17526.0` today. I used Python 3.6, but in Python 2.7 you can achieve the same by importing `from __future__ import division` (or better import all future imports). – Dawn Drescher Dec 26 '17 at 14:33
  • 1
    I think you need to be careful with this, leap years and leap seconds can get in the way – Harry Jones Jun 21 '21 at 11:31
1

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.

Contango
  • 76,540
  • 58
  • 260
  • 305