Conveting timestamp into integer or float

Hello all,
I have this timestamp 2021 21:35:18.608757000 and need to convert it into integer or float. I tried to use datetime function put it gives me an error of invalid syntax.

any help please.

I assume this timestamp is a string? Use dateutil to parse it:

import dateutil.parser

date = dateutil.parser.parse("2021 21:35:18.608757000")

Next, what kind of integer or float representation do you want? you can use .toordinal() to get the number of days since the start of common era time reckoning, or .timestamp() to get a POSIX timestamp as float.

>>> date.toordinal()
>>> date.timestamp()

Thanks for your help. Actually I have a column in a dataset that I need to convert. As seen in the attached picture.

i tried this command following what you suggested

date = dateutil.parser.parse(data[95269:95274][['frame.time_WithoutIP']])

but I got an error

TypeError: Parser must be a string or character stream, not DataFrame


Try the DataFrame’s .apply method:


I got this error

TypeError: Parser must be a string or character stream, not Series

How about now?


it works great. and saved the results in date.
but now have an error in the next command


AttributeError: 'Series' object has no attribute 'timestamp'

The return value from this:


is a pandas.Series containing datetime objects. You can use the .apply method again:

from datetime import datetime

Oh, many thanks it works perfectly now. I was struggling in this since one week.

Try to visualise what result you’re getting.

dateutil.parser.parse is a function to parse a single string and
return a single datetime. Applying it to a Series gets you a new
Series with converted values.

Likewise, datetime.timestamp computes single UNIX timestamp from a
single datetime. Applying it to a Series again gets you a Series
containing timestamps.

So you have a Series:


You can’t give that as an argument to dateutil.parser.parse because it
doesn’t know how to work with it. Instead, one must use Series.apply
to call it on each value in the Series. That returns a new Series.

And again, datetime.timestamp converts a single adtetime instance to
a timestamp. It also doesn’t accept a Series, and again you need to
use Series.apply to call it on every value.

You could break this stuff up a bit to see what’s going on:

 twips = data[95269:95274]['frame.time_WithoutIP']
 datetimes = twips.apply(dateutil.parser.parse)
 timestamps = datetimes.apply(datetime.timestamp)

Cameron Simpson

BTW, when I said “Applying” here, I meant "using the .apply() method.

  • Cameron