I get the following error:
ValueError: unconverted data remains: 918+00:00
When I try to convert a datetime like: 2018-11-20 14:31:33.799661918+00:00
v_time=[]
for i in range (1,len(db)+1):
p = (datetime.strptime(db.Time[i], "%Y-%m-%d%H:%M:%S.%f").strftime("%Y-
%m-%d %H:%M:%S"))
v_time.append(p)
db.Time = v_time
Since you anyway only display in seconds precision later, you could either strip simply the nanoseconds and live with microseconds precision (the nanoseconds might be not meaningful anyway)
from datetime import datetime
time_0 = '2018-11-20 14:31:33.799661918+00:00'
time_0_cut = time_0[0:26]
print(datetime.strptime(time_0_cut,
'%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M:%S'))
gives
2018-11-20 14:31:33
or, if you have to keep the nanoseconds precision, use numpy.datetime which is able to convert datetimes down to attoseconds precision
import numpy as np
print(np.datetime_as_string(np.datetime64(time_0)))
print(np.datetime_as_string(np.datetime64(time_0), unit='s')) # display in sec precision
gives
2018-11-20T14:31:33.799661918
2018-11-20T14:31:33
Related
Unable to convert DataFrame column to date time format.
from datetime import datetime
Holidays = pd.DataFrame({'Date':['2016-01-01','2016-01-06','2016-02-09','2016-02-10','2016-03-20'], 'Expenditure':[907.2,907.3,904.8,914.6,917.3]})
Holidays['Date'] = pd.to_datetime(Holidays['Date'])
type(Holidays['Date'])
Output: pandas.core.series.Series
Also tried
Holidays['Date'] = Holidays['Date'].astype('datetime64[ns]')
type(Holidays['Date'])
But same output
Output: pandas.core.series.Series
I think you are getting a bit mixed up. The dtypes of Holidays['Date'] is datetime64[ns]
Here's how I am checking.
from datetime import datetime
import pandas as pd
Holidays = pd.DataFrame({'Date':['2016-01-01','2016-01-06','2016-02-09','2016-02-10','2016-03-20'], 'Expenditure':[907.2,907.3,904.8,914.6,917.3]})
print ('Before converting : ' , Holidays['Date'].dtypes)
Holidays['Date'] = pd.to_datetime(Holidays['Date'])
print ('After converting : ' ,Holidays['Date'].dtypes)
The output is:
Before converting : object
After converting : datetime64[ns]
Thought I will also share some addition information for you around types and dtypes. See more info in this link for types-and-dtypes
I am downloading data from FXCM with fxcmpy, this is what the data looks like:
In the index column I would like only to have the time without the date how can this be done.
This is the code:
import fxcmpy
import pandas as pd
import matplotlib.pyplot as plt
con = fxcmpy.fxcmpy(config_file='fxcm.cfg', server='demo')
# To check if the connection is established
if(con.is_connected):
print('Connection is established')
else:
print('Erro in connecting to the server')
data = con.get_candles('USD/JPY', period='m5', number=500)
con.close()
Assuming that your index is already a DatetimeIndex, simply choose the time part from the index:
data.index = data.index.time
If it is not (say, it is a string), convert it to DatetimeIndex first:
data.index = pd.DatetimeIndex(data.index)
You have to make sure your df['Index'].dtype has type pandas datetime type dtype('<M8[ns]'). Then you use the following format to extract time. Refer to this answer
df['Index'].dt.strftime('%H:%m:%S')
one way is converting object to datetime then extract year from it.
from datetime import datetime as dt
date="2019-11-21 13:10:00"
fmt="%Y-%m-%d %H:%M:%S"
print(dt.strptime(date,fmt).time())
output
13:10:00
Hi I have this DateTime format in our log "2019-09-19T15:12:59.943Z"
I want to convert this to custom DateTime format 2019-09-19 15:12:59
from datetime import datetime
timestamp = "2019-09-19T15:12:59.943Z"
dt_object = datetime.fromtimestamp(timestamp)
print("dt_object =", dt_object)
print("type(dt_object) =", type(dt_object))
which function shall I use for this
thanks
okay
This issue is related to custom DateTime formatting not related to timestamp.
because timestamp in python is an integer value, not a string value.
So you have a custom DateTime format which contains Zulu time format.
and you need to convert this Zulu DateTime format to custom DateTime format.
so, try this python script and its working fine on Python version 3.6
import datetime
d = datetime.datetime.strptime("2019-09-19T15:12:59.943Z","%Y-%m-%dT%H:%M:%S.%fZ")
new_format = "%Y-%m-%d"
d.strftime(new_format)
print(d)
or you can use this online fiddle to check the result
https://pyfiddle.io/fiddle/c7b8e849-c31a-41ba-8bc9-5436d6faa4e9/?i=true
How can I convert date from seconds to date format.
I have a table containing information about lat, long and time.
table
f_table['dt'] = pd.to_datetime(f_table['dt'])
f_table["dt"]
it results like this:
output
but the output is wrong actually the date is 20160628 but it converted to 1970.
My desired output:
24-April-2014
The unit needs to be nanoseconds, so you need to multiply with 1e9
f_table['dt'] = pd.to_datetime(f_table['dt'] * 1e9)
This should work.
#Split your string to extract timestamp, I am assuming a single space between each float
op = "28.359062 69.693673 5.204486e+08"
ts = float(op.split()[2])
from datetime import datetime
#Timestamp to datetime object
dt = datetime.fromtimestamp(ts)
#Datetime object to string
dt_str = dt.strftime('%m-%B-%Y')
print(dt_str)
#06-June-1986
I have a pandas dataframe with columns containing start and stop times in this format: 2016-01-01 00:00:00
I would like to convert these times to datetime objects so that I can subtract one from the other to compute total duration. I'm using the following:
import datetime
df = df['start_time'] =
df['start_time'].apply(lambda x:datetime.datetime.strptime(x,'%Y/%m/%d/%T %I:%M:%S %p'))
However, I have the following ValueError:
ValueError: 'T' is a bad directive in format '%Y/%m/%d/%T %I:%M:%S %p'
This would convert the column into datetime64 dtype. Then you could process whatever you need using that column.
df['start_time'] = pd.to_datetime(df['start_time'], format="%Y-%m-%d %H:%M:%S")
Also if you want to avoid explicitly specifying datetime format you can use the following:
df['start_time'] = pd.to_datetime(df['start_time'], infer_datetime_format=True)
Simpliest is use to_datetime:
df['start_time'] = pd.to_datetime(df['start_time'])