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
Hello,
I have a similar issue 'TypeError: float() argument must be a string or a real number, not âPeriodâ"
and I tried import dateutil.parser + your advices in the grey box but still canât figure the pb.
here are my initial data
errors
Out[42]:
Date
2021-01-04 0.164665
2021-01-05 0.173659
2021-01-06 0.151123
2021-01-07 0.156050
2021-01-08 0.161853
2024-03-15 0.231166
2024-03-18 0.231014
2024-03-19 0.238065
2024-03-20 0.249634
2024-03-21 0.252894
Freq: D, Name: Close, Length: 809, dtype: float64
and I tried to get a graph from the following
train_end = datetime(2023,4,1)
test_end = datetime(2024,4,1)
train_data = errors[:train_end]
test_data = errors[train_end+timedelta(days=1):test_end]
model2 = AutoReg(train_data, lags=3)
model2_fit = model2.fit()
pred_start_date = test_data.index[0]
pred_end_date = test_data.index[-1]
predictions = model2_fit.predict(start=pred_start_date, end=pred_end_date)
plt.plot(predictions)
would you have any guideline ? thank you in advance