how to convert a string date into datetime format in python?
The particular format for strptime
:
datetime.datetime.strptime(string_date, "%Y-%m-%d %H:%M:%S.%f")
#>>> datetime.datetime(2013, 9, 28, 20, 30, 55, 782000)
How to convert string date to datetime format for graphing in python
You can use datetime.datetime.strptime -
import datetime
string = '30.06.2019 07:00:00.000,1.13760,1.13760,1.13760,1.13760,0'.split(',')[0]
d = datetime.datetime.strptime(string, '%d.%m.%Y %H:%M:%S.%f')
or more to the point if you already have the comma-separated fields,
times.append(datetime.datetime.strptime(row[0], '%d.%m.%Y %H:%M:%S.%f')
I would recommend not using time
as the name of a variable since that will conflict with the time module which you may have imported. That kind of thing can really be hard to chase down.
Convert a date (string) into datetime format
Datetime documentation has a table of possible formats for strptime and strftime, and %m
is labeled as 'Month as a zero-padded decimal number' (i.e. 01,02,...), which is clearly not what you need.
What you do need it %B
'Month as locale’s full name' (i.e. January, February, …, December (en_US); Januar, Februar, …, Dezember (de_DE))
So, your format should be:
from datetime import datetime
datetime.strptime('April 29, 2021', '%B %d, %Y')
How to convert string to datetime format in pandas python?
Use to_datetime
. There is no need for a format string since the parser is able to handle it:
In [51]:
pd.to_datetime(df['I_DATE'])
Out[51]:
0 2012-03-28 14:15:00
1 2012-03-28 14:17:28
2 2012-03-28 14:50:50
Name: I_DATE, dtype: datetime64[ns]
To access the date/day/time component use the dt
accessor:
In [54]:
df['I_DATE'].dt.date
Out[54]:
0 2012-03-28
1 2012-03-28
2 2012-03-28
dtype: object
In [56]:
df['I_DATE'].dt.time
Out[56]:
0 14:15:00
1 14:17:28
2 14:50:50
dtype: object
You can use strings to filter as an example:
In [59]:
df = pd.DataFrame({'date':pd.date_range(start = dt.datetime(2015,1,1), end = dt.datetime.now())})
df[(df['date'] > '2015-02-04') & (df['date'] < '2015-02-10')]
Out[59]:
date
35 2015-02-05
36 2015-02-06
37 2015-02-07
38 2015-02-08
39 2015-02-09
How to convert a date string to different format
I assume I have import datetime
before running each of the lines of code below
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
prints "01/25/13"
.
If you can't live with the leading zero, try this:
dt = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print '{0}/{1}/{2:02}'.format(dt.month, dt.day, dt.year % 100)
This prints "1/25/13"
.
EDIT: This may not work on every platform:
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
Parse date string and change format
datetime
module could help you with that:
datetime.datetime.strptime(date_string, format1).strftime(format2)
For the specific example you could do
>>> import datetime
>>> datetime.datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')
'15/02/2010'
>>>
How to convert string to datetime?
Here to parse a string to date time, then you can:
def convert(s):
return datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ')
someobject = convert('2017-08-15T13:34:35Z')
print(someobject.isoformat())
Convert string Jun 1 2005 1:33PM into datetime
datetime.strptime
parses an input string in the user-specified format into a timezone-naive datetime
object:
>>> from datetime import datetime
>>> datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
datetime.datetime(2005, 6, 1, 13, 33)
To obtain a date
object using an existing datetime
object, convert it using .date()
:
>>> datetime.strptime('Jun 1 2005', '%b %d %Y').date()
date(2005, 6, 1)
Links:
strptime
docs: Python 2, Python 3strptime
/strftime
format string docs: Python 2, Python 3strftime.org format string cheatsheet
Notes:
strptime
= "string parse time"strftime
= "string format time"
Related Topics
Unicodedecodeerror When Redirecting to File
How to Use 'Else' in a List Comprehension
How to Escape Special Characters of a String with Single Backslashes
How to Draw Intersecting Planes
How to Do Multiple Arguments to Map Function Where One Remains the Same
Convert to Binary and Keep Leading Zeros
What Do the Python File Extensions, .Pyc .Pyd .Pyo Stand For
Increment a Python Floating Point Value by the Smallest Possible Amount
How to Use the 'JSON' Module to Read in One JSON Object at a Time
How to Start and Stop a Thread
How to Set Up a Virtual Environment for Python in Visual Studio Code
How to Disable a Pylint Warning
How to Compare Multiple Variables to the Same Value
How to Handle Elements Inside Shadow Dom from Selenium
Calculate Average of Every X Rows in a Table and Create New Table