Pandas Series: string date to epoch unix seconds
NickName:Jenna Kwon Ask DateTime:2016-12-05T12:12:14

Pandas Series: string date to epoch unix seconds

I have a Pandas Dataframe where one column is in a string date format as below

0               time
1  September 20 2016  
2  September 20 2016     
3  September 19 2016     
4  September 16 2016

What would be a succinct way for replacing time to be in epoch unix seconds?

Copyright Notice:Content Author:「Jenna Kwon」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40966975/pandas-series-string-date-to-epoch-unix-seconds

Answers
monkut 2016-12-05T05:13:52

You can modify the values of a column using the Series' apply method by giving it a function containing the actions you want to perform on each of the values.\nFor handling datetimes you can use dateutil.parser.parse to parse arbitrary strings into datetime objects.\nimport datetime\nimport pandas as pd\nfrom dateutil.parser import parse\n\ns = pd.Series(['September 20 2016',\n'September 20 2016',\n'September 19 2016',\n'September 16 2016'])\ndf = pd.DataFrame(s)\n\ndef dt2epoch(value):\n d = parse(value)\n return d.timestamp()\n \ndf[0].apply(dt2epoch) # apples given function to each value of column\n\nResult:\n0 1474329600\n1 1474329600\n2 1474243200\n3 1473984000\nName: 0, dtype: float64\n",


More about “Pandas Series: string date to epoch unix seconds” related questions

Pandas Series: string date to epoch unix seconds

I have a Pandas Dataframe where one column is in a string date format as below 0 time 1 September 20 2016 2 September 20 2016 3 September 19 2016 4 September 16 2016 ...

Show Detail

How do I get the Unix epoch in seconds for a date without time?

In Go, how do I get the Unix epoch in seconds for a date without time? For example, if I want the Unix epoch seconds for the current time, I can use: today := time.Now().Unix() For instance, if the

Show Detail

Convert UNIX epoch to Date object

I'm plotting and performing calculations on uniformly distributed time series. The timestamps are currently stored as integers representing the number of seconds since the UNIX epoch (e.g. 13520683...

Show Detail

Python Pandas Series of Datetimes to Seconds Since the Epoch

Following in the spirit of this answer, I attempted the following to convert a DataFrame column of datetimes to a column of seconds since the epoch. df['date'] = (df['date']+datetime.timedelta(ho...

Show Detail

Pandas: Using Unix epoch timestamp as Datetime index

My application involves dealing with data (contained in a CSV) which is of the following form: Epoch (number of seconds since Jan 1, 1970), Value 1368431149,20.3 1368431150,21.4 .. Currently i re...

Show Detail

Hive function to get epoch milliseconds or convert epoch milliseconds to date without converting milliseconds to seconds

Is there a way to get date in epoch milliseconds or vice versa in HIVE? There are function like "unix_timestamp()" and "from_unixtime()" but they work with seconds. P.S : My use...

Show Detail

Date time string to epoch: pandas dataframe

One of my pandas df has date time string column. The format is given below: TimeStamp value 11/12/2015 10:07:34 AM 24.5 11/12/2015 10:07:35 AM 55.1 so on I tried to convert the

Show Detail

Python convert a series of seconds since epoch time to datetime series

Is there any fast way to convert a series of seconds since epoch time to datetime object series? I use: for i in range(len(df)): df['datetime'].iloc[i] = datetime.fromtimestamp(df['epochtime'...

Show Detail

Convert from UNIX Epoch to Date String

How do I convert from a unix epoch integer to date string?

Show Detail

Use number of seconds since the UNIX Epoch as date format in logging

The docs for logging.basicConfig say: datefmt - Use the specified date/time format, as accepted by time.strftime(). However, if I have a look at the docs for time.strftime(), the UNIX Epoch tim...

Show Detail