In Python, how do you convert seconds since epoch to a `datetime` object?
datetime.datetime.fromtimestamp
will do, if you know the time zone, you could produce the same output as with time.gmtime
>>> datetime.datetime.fromtimestamp(1284286794)
datetime.datetime(2010, 9, 12, 11, 19, 54)
or
>>> datetime.datetime.utcfromtimestamp(1284286794)
datetime.datetime(2010, 9, 12, 10, 19, 54)
Converting Epoch time into the datetime
To convert your time value (float or int) to a formatted string, use:
import time
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))
For example:
import time
my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))
print(my_time)
Python convert a series of seconds since epoch time to datetime series
you can use to_datetime(..., unit='s')
:
df['datetime'] = pd.to_datetime(df['epochtime'], unit='s')
Time comparison:
In [158]: df = pd.DataFrame({'epochtime': pd.date_range('2001-01-01', freq='1S', periods=10**5)}).astype(np.int64)//10**9
In [159]:
In [159]: df.head()
Out[159]:
epochtime
0 978307200
1 978307201
2 978307202
3 978307203
4 978307204
In [160]:
In [160]: len(df)
Out[160]: 100000
In [161]:
In [161]: %timeit df['datetime'] = pd.to_datetime(df['epochtime'], unit='s')
100 loops, best of 3: 16.9 ms per loop
In [162]:
In [162]: %%timeit
.....: for i in range(len(df)):
.....: df['datetime'].iloc[i] = datetime.fromtimestamp(df2['epochtime'].iloc[i])
.....:
c:\envs\py35\lib\site-packages\pandas\core\indexing.py:128: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)
1 loop, best of 3: 54.5 s per loop
Conclusion:
@Natecat,
as you can see 16.9 ms
vs. 54.5 s
In Python, how do you convert a `datetime` object to seconds?
For the special date of January 1, 1970 there are multiple options.
For any other starting date you need to get the difference between the two dates in seconds. Subtracting two dates gives a timedelta
object, which as of Python 2.7 has a total_seconds()
function.
>>> (t-datetime.datetime(1970,1,1)).total_seconds()
1256083200.0
The starting date is usually specified in UTC, so for proper results the datetime
you feed into this formula should be in UTC as well. If your datetime
isn't in UTC already, you'll need to convert it before you use it, or attach a tzinfo
class that has the proper offset.
As noted in the comments, if you have a tzinfo
attached to your datetime
then you'll need one on the starting date as well or the subtraction will fail; for the example above I would add tzinfo=pytz.utc
if using Python 2 or tzinfo=timezone.utc
if using Python 3.
Python - Convert seconds from epoch time into human readable time
a = last_epoch #last epoch recorded
b = time.time() #current epoch time
c = b - a #returns seconds
days = c // 86400
hours = c // 3600 % 24
minutes = c // 60 % 60
seconds = c % 60
Convert python datetime to epoch with strftime
If you want to convert a python datetime to seconds since epoch you could do it explicitly:
>>> (datetime.datetime(2012,4,1,0,0) - datetime.datetime(1970,1,1)).total_seconds()
1333238400.0
In Python 3.3+ you can use timestamp()
instead:
>>> datetime.datetime(2012,4,1,0,0).timestamp()
1333234800.0
Why you should not use datetime.strftime('%s')
Python doesn't actually support %s as an argument to strftime (if you check at http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior it's not in the list), the only reason it's working is because Python is passing the information to your system's strftime, which uses your local timezone.
>>> datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'
Seconds since date (not epoch!) to date
You could try this:
from datetime import datetime, timedelta
t0 = datetime(1981, 1, 1)
seconds = 1000000
dt = t0 + timedelta(seconds=seconds)
print dt
# 1981-01-12 13:46:40
Here t0
is set to a datetime
object representing the "epoch" of 1981-01-01
. This is your reference datetime to which you can add a timedelta
object initialised with an arbitrary number of seconds. The result is a datetime
object representing the required date time.
N.B. this assumes UTC time
How can I convert a datetime object to milliseconds since epoch (unix time) in Python?
It appears to me that the simplest way to do this is
import datetime
epoch = datetime.datetime.utcfromtimestamp(0)
def unix_time_millis(dt):
return (dt - epoch).total_seconds() * 1000.0
Convert timestamp since epoch to datetime.datetime
I would use the time
module
>>> import time
>>> time.gmtime(1346114717972/1000.)
time.struct_time(tm_year=2012, tm_mon=8, tm_mday=28, tm_hour=0, tm_min=45, tm_sec=17, tm_wday=1, tm_yday=241, tm_isdst=0)
This shows the timestamp in UTC/GMT time.
The timestamp is divided by 1000 as the stamps you have provided are in milliseconds since the epoch, not seconds.
Then use strftime
to format like so:
>>> time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(1346114717972/1000.))
'08/28/2012 00:45:17'
Related Topics
What Is the Current Choice for Doing Rpc in Python
How to Access Pandas Groupby Dataframe by Key
In Selenium Web Driver How to Choose the Correct Iframe
How to Add a Calculated Field to a Django Model
How to Get the Largest Integer One Can Use in Python
Testing Floating Point Equality
Verifying Compatibility in Compiling Extension Types, and Using Them with Cdef
How to Run a Function Periodically in Python
How to Convert Strings in a Pandas Data Frame to a 'Date' Data Type
Dummy Variables When Not All Categories Are Present
Random State (Pseudo-Random Number) in Scikit Learn
How to Implement Option Buttons and Change the Button Color in Pygame
How to Get Last Items of a List in Python
Compulsory Usage of If _Name_=="_Main_" in Windows While Using Multiprocessing
Is It Still Necessary to Install Cuda Before Using the Conda Tensorflow-Gpu Package