2

I can't seem to get a time stamp into a unix timestamp format and assume I'm missing some points. Looking for some help.

I have a column within a df that is a datetime object in the format: YYYY-MM-DD HH:MM:SS and I need a new column with the unix stamp.

I'm bringing the csv into python with:

gps = filepath here   
dateparse= lambda x: pd.datetime.strptime(x, '%Y%m%d %H:%M:%S')
gps_dat = pd.read_csv(gps, parse_dates=['date_stamp'],date_parser=dateparse)

Anytime I try to change this column into a unix stamp I'm getting an error about wrong format or "datetime.datetime" has no attribute "datetime" with this:

gps_dat['unix']=datetime.datetime(gps_dat['date_stamp'])

Should I be using

calendar.timegm(tuple)

I'm still learning so any help would be greatly appreciated!

8
  • you probably imported datetime.datetime instead of just datetime, so when you call datetime.datetime you're really calling datetime.datetime.datetime, which isn't defined. Sorry to sound a bit like Xzibit, but... :) Commented May 9, 2016 at 13:45
  • I think that definitely fixed part of the problem thanks! When I use the: gps_dat['unix']=datetime.datetime(gps_dat['date_stamp']) Now I get error "cannot convert the series to <class 'int>" Commented May 9, 2016 at 13:49
  • what does gps_dat['date_stamp'] look like? Commented May 9, 2016 at 14:09
  • gps_dat['date_stamp'] is a column within my dataframe: 2015-04-16 15:23:20 Commented May 9, 2016 at 14:20
  • I mean, is it already a datetime object, or is it a string? If it's already a datetime object, you just need to use gps_dat['unix'] = gps_dat['date_stamp']. If it's a string instead, use gps_dat['unix'] = datetime.strptime(gps_dat['date_stamp'], '%Y-%m-%d %H:%M:%S') Commented May 9, 2016 at 14:26

3 Answers 3

1

UPDATE: if you want to convert datetime column to UNIX timestamp:

gps_dat['unix']=gps_dat['date_stamp'].astype(np.int64) // 10**9

NOTE: but it must be of datetime type, not string/object

Old answer: parsing from UNIX timestamp to datetime

try to change your parser function like this:

dateparse= lambda x: pd.to_datetime(x, unit='s')

this will instruct to_datetime() that you're using UNIX timestamp format

Sign up to request clarification or add additional context in comments.

2 Comments

When I parse with this function I'm still getting: 2015-04-21 23:32:25 format.
THIS WORKED!! Thanks to everyone for their input. It's very appreciated.
1

You can use faster solution with list comprehension:

print gps_dat
  nam  code date1          date_stamp
0   a     1   1/1 2012-10-08 18:15:05
1   b     3   3/4 2012-10-08 18:15:05

gps_dat['unix'] = [t.value // 10 ** 9 for t in gps_dat['date_stamp']]
print gps_dat
  nam  code date1          date_stamp        unix
0   a     1   1/1 2012-10-08 18:15:05  1349720105
1   b     3   3/4 2012-10-08 18:15:05  1349720105

Timings:

In [46]: %timeit gps_dat['date_stamp'].astype(np.int64) // 10**9
1000 loops, best of 3: 204 µs per loop

In [47]: %timeit [t.value // 10 ** 9 for t in gps_dat['date_stamp']]
The slowest run took 4.99 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 24.2 µs per loop

Comments

0

Edit: I've never used Pandas, but it looks like this is the kind of function that you should call:

gps_dat['unix'] = gps_dat.apply(lambda row: time.mktime(row['date_stamp'].timetuple()), axis=col_number)

where col_number is the index of your date_stamp column (assuming it was correctly parsed as a datetime).

Original answer (when I didn't know Pandas was involved):

Replace that

gps_dat['unix']=datetime.datetime(gps_dat['date_stamp'])

line with

gps_dat['unix'] = time.mktime(gps_dat['date_stamp'].timetuple())

And add

import time

to your imports. Note that some considerations apply to time zones, so according to your requirements you might want to add some logic to, say, convert to UTC.

3 Comments

I got: " 'Series object has no attribute 'timetuple' "
ok so that's a pandas.Series, not a datetime. I added a pandas tag so that people familiar with the library can help. You might want to check what gps_dat['date_stamp'].data contains by printing it out
Using this I'm getting a "unhashable type:list" error. Thanks for all your help, by the way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.