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.
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()
737809
>>> date.timestamp()
1611088518.608757
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
Thanks.
Try the DataFrame’s .apply
method:
data[95269:95274][['frame.time_WithoutIP']].apply(dateutil.parser.parse)
I got this error
TypeError: Parser must be a string or character stream, not Series
How about now?
data[95269:95274]['frame.time_WithoutIP'].apply(dateutil.parser.parse)
it works great. and saved the results in date.
but now have an error in the next command
date.timestamp()
AttributeError: 'Series' object has no attribute 'timestamp'
The return value from this:
data[95269:95274]['frame.time_WithoutIP'].apply(dateutil.parser.parse)
is a pandas.Series containing datetime objects. You can use the .apply
method again:
from datetime import datetime
data[95269:95274]['frame.time_WithoutIP'].apply(dateutil.parser.parse).apply(datetime.timestamp)
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
:
data[95269:95274]['frame.time_WithoutIP']
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']
print(type(twips))
datetimes = twips.apply(dateutil.parser.parse)
print(type(datetimes))
timestamps = datetimes.apply(datetime.timestamp)
print(type(datetimes))
Cheers,
Cameron Simpson cs@cskk.id.au
BTW, when I said “Applying” here, I meant "using the .apply()
method.
Thank you Cameron