From the official documentation of pandas.to_datetime we can say,
unit : string, default ‘ns’
unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number. This will be based off the origin. Example, with unit=’ms’ and origin=’unix’ (the default), this would calculate the number of milliseconds to the unix epoch start.
So when I try like this way,
import pandas as pd df = pd.DataFrame({'time': [pd.to_datetime('2019-01-15 13:25:43')]}) df_unix_sec = pd.to_datetime(df['time'],unit='ms',origin='unix') print(df) print(df_unix_sec) time 0 2019-01-15 13:25:43 0 2019-01-15 13:25:43 Name: time, dtype: datetime64[ns]
Output is not changing for the later one. Every time it is showing the datetime value not number of milliseconds to the unix epoch start for the 2nd one. Why is that? Am I missing something?
To convert a datetime to seconds, subtracts the input datetime from the epoch time.
Unix is an operating system originally developed in the 1960s. Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC.
DateTime to Unix timestamp in UTC Timezone now function returns the current time in the UTC timezone. In the time module, the timegm function returns a Unix timestamp. The timetuple() function of the datetime class returns the datetime's properties as a named tuple. To obtain the Unix timestamp, use print(UTC).
The getTime method returns the number of milliseconds since the Unix Epoch (1st of January, 1970 00:00:00). To get a Unix timestamp, we have to divide the result from calling the getTime() method by 1000 to convert the milliseconds to seconds. What is this?
I think you misunderstood what the argument is for. The purpose of origin='unix'
is to convert an integer timestamp to datetime
, not the other way.
pd.to_datetime(1.547559e+09, unit='s', origin='unix') # Timestamp('2019-01-15 13:30:00')
Here are some options:
Conversely, you can get the timestamp by converting to integer (to get nanoseconds) and divide by 109.
pd.to_datetime(['2019-01-15 13:30:00']).astype(int) / 10**9 # Float64Index([1547559000.0], dtype='float64')
Pros:
Cons:
Pandas docs recommend using the following method:
# create test data dates = pd.to_datetime(['2019-01-15 13:30:00']) # calculate unix datetime (dates - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s') [out]: Int64Index([1547559000], dtype='int64')
Pros:
Cons:
pd.Timestamp
If you have a single date string, you can use pd.Timestamp
as shown in the other answer:
pd.Timestamp('2019-01-15 13:30:00').timestamp() # 1547559000.0
If you have to cooerce multiple datetimes (where pd.to_datetime
is your only option), you can initialize and map:
pd.to_datetime(['2019-01-15 13:30:00']).map(pd.Timestamp.timestamp) # Float64Index([1547559000.0], dtype='float64')
Pros:
Cons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With