How do I convert a string to date time in Python ? I have the string 09082010 .how do I convert to 09-08-2010?.Thank you.
from datetime import datetime
mydate = datetime.strptime('09082010', '%d%m%Y')
If all of your date strings will be in the format mmddyyyy like the example you provided then this should work for you.
import datetime
d="09012010"
mm=d[:2]
dd=d[2:4]
yr=d[4:]
date = datetime.date(day=dd, month=mm, year=yr)
Related
I have a bq table withhas a column timestamp as a string, with format 20090630 16:36:23:880, how can I convert it to a proper timestamp ?
parse_datetime('%Y%m%d %H:%M:%E3S', '20090630 16:36:23.880')
I am trying to create a new datetime column from existing columns (one datetime column and another integer column) in a pandas data frame. Here is my code:
import datetime
df['start_date'] = pd.to_datetime(df['start_date'])
df['end_date'] = df['start_date'] + pd.Timedelta(df.total_waiting_days, unit='D')
But I got the following errors:
ValueError: Value must be Timedelta, string, integer, float, timedelta or convertible
What did I do wrong here and how do I fix this? Thanks!
Seems like you want to convert whole column to Timedelta
df['end_date'] = df['start_date'] + df.total_waiting_days.apply(lambda x :pd.Timedelta(x, unit='D'))
I have two datetime columns in a pandas dataframe: time_col & override_col. If override_col isn't null I want to replace the corresponding time_col record with the override_col record. The following code does this but it converts the override_col records to a long and not a datetime... then the time_col is a mix of datetime and long dtypes.
df.loc[~df['override_col'].isnull(), 'time_col'] = df[~df['override_col'].isnull()]['override_col']
Any ideas why this is happening?!
I have three variables stored as number, string and string, as shown below.
load_id = 100
t_date = '2014-06-18'
p_date = '19-JUN-14 10.51.45.378196'
I would like to insert them into a SQL Server table using Python 2.7. The SQL Server table structure is as follows
load_id = float
t_date = date
p_date = timestamp
In Oracle, we tend to use TO_DATE or TO_TIMESTAMP to convert the string to DATE or TIMESTAMP field.
I would like to know how I can do similar conversion while inserting into an SQL Server table.
Thanks in advance.
convert with :
import datetime
import calendar
thedate=datetime.datetime.strptime(p_date,'%d-%b-%y %H.%M.%S.%f')
thetimestamp=calendar.timegm(thedate.utctimetuple())
https://community.toadworld.com/platforms/sql-server/b/weblog/archive/2012/04/18/convert-datetime-to-timestamp
DECLARE #DateTimeVariable DATETIME
SELECT #DateTimeVariable = GETDATE()
SELECT #DateTimeVariable AS DateTimeValue,
CAST(#DateTimeVariable AS TIMESTAMP) AS DateTimeConvertedToTimestampCAST
SELECT CAST(CAST(#DateTimeVariable AS TIMESTAMP) AS DATETIME) AS
TimestampToDatetime
Do the conversion with SQL instead of trying to get Python to match the SQL format.
Neither format matches yours, however the DATETIME type should be adequate.
I need the date in a regular form.
Here when I use the below code:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
date = QDate.currentDate()
print date
I get the below output:
PyQt4.QtCore.QDate(2014, 5, 23)
I want the date to be displayed in '/' or '.' eg.(5.23.2014)
Please suggest how to do it.
Check the docs:
date = QDate.currentDate().toString("dd.MM.yyyy")
print date
2014.5.23
To change:
date = QDate.currentDate().toString("MM.dd.yyyy")
print date
05.23.2014