Convert complex time string to datetime [duplicate] - python

This question already has answers here:
In Pandas, how to convert a string to a datetime object with milliseconds?
(2 answers)
Closed 2 years ago.
Attempting the following pd.to_datetime('2020-08-29 18:00:09:438') results in an error ValueError: ('Unknown string format:', '2020-08-29 18:00:09:438') .
How would I convert this string to a datetime or timestamp?

Let us pass the correct format
pd.to_datetime('2020-08-29 18:00:09:438', format = '%Y-%m-%d %H:%M:%S:%f')
Out[71]: Timestamp('2020-08-29 18:00:09.438000')

Related

How to convert double values from df to year values/strings? [duplicate]

This question already has answers here:
Convert floats to ints in Pandas?
(11 answers)
Closed last year.
I have a dataframe with year values like: 2014.0, 2013.0... as float values. I would need to convert these values to year format, like 2014, 2013... I tried converting them to string, but I get "2014.0", "2013.0"...
How could I convert them to year, not double or strings?
This is what I tried for string conversion:
df['year']=df['year'].astype(str)
And then:
df['year']= pd.to_datetime(df['year'], format='%Y')
But this gave me results like: 1640995200000 and so on. How could I convert the doubles to reasonable year format?
simply do this !!works!!
df['year']=df['year'].astype(int)
output:
2004
2005
2017
converting to str does this 2014.0 => "2014.0" so that wont help you and converting using datetime will add whole date. so if you need only that particular year do this int remains datatype of that column not datetime objects

How to keep just the date YYYY-MM-DD in pandas.Timestamp.today() function? [duplicate]

This question already has answers here:
How to set a variable to be "Today's" date in Python/Pandas
(11 answers)
Getting today's date in YYYY-MM-DD in Python?
(12 answers)
Closed 2 years ago.
I want to get rid of the time in today() function:
Today= pd.Timestamp.today()
print(Today)
result = 2020-02-28 12:10:11.862889
I just want the 2020-02-28 and get rid of the 12:10:11.862889
Thank's!

Python - convert string to simple YYYY date format [duplicate]

This question already has answers here:
Convert date to months and year
(4 answers)
Closed 4 years ago.
New to Python, and have already spent 30 minutes reviewing old responses, but still can't figure this out.
'Year' variable is a string. Examples: 1990, 2010.
I need to convert to date format, but just with the 4 year "digits".
Tried the following, but none are working:
date1 = datetime.datetime.date('Year', "%Y")
datetime.datetime.strftime('Year', "%Y")
wcData.astype(str).apply(lambda x: pd.to_datetime('Year', format='%Y'))
df.astype(str).apply(lambda x: pd.to_datetime(x, format='%Y%m%d'))
Please help!
You need datetime.datetime.strptime, not strftime:
import datetime
datetime.datetime.strptime("2010", "%Y")
Here's how to remember which one to use:
strPtime means "string -> parse -> time", that is, parse a string to create some kind of object that represents time;
strFtime means "string <- format <- time" (note the reversed arrows), that is, given an object that represents time, create a string representation of it using some format.

How can I sort date array [duplicate]

This question already has answers here:
Sort a Python date string list
(2 answers)
Sort list of date strings
(2 answers)
Closed 5 years ago.
Please see my following code snippet
Input
list_x = ["11/1/2100", "5/12/1999", "19/1/2003", "11/9/2001"]
Output
['5/12/1999', '11/9/2001', '19/1/2003', '11/1/2100']
You can convert your day/month format to day in year format and then compare each element in the list based on the year then the day

Subtract one date from another and get int [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 7 years ago.
How can I subtract on datefield from another and get result in integer?
Like 11.05.2015-10.05.2015 will return 1
I tried
entry.start_devation= each.start_on_fact - timedelta(days=data.calendar_start)
Take the difference between two dates, which is a timedelta, and ask for the days attribute of that timedelta.

Categories

Resources