This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
Closed 3 years ago.
I want to concatenate two dataframes, but they each have two columns that are datetime objects. One is formatted YYYY-MM-DD HH:mm:SS while in the other dataframe it is formateed MM/DD/YEAR HH:mm:SS. Is there way I can convert one format to the other, I am not picky on which one I end up with in the end.
start_time
2018-12-31 23:59:18
and
start_time
4/1/2017 00:13:24
Thanks!
You can convert the format like this
import datetime
date_time_str = '2018-12-31 23:59:18'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
date_time_str2 = date_time_obj.strftime('%d/%m/%Y %H:%M:%S')
print(date_time_str2)
Output :
31/12/2018 23:59:18
Related
This question already has answers here:
Converting date/time in YYYYMMDD/HHMMSS format to Python datetime
(3 answers)
How do I get the day of week given a date?
(30 answers)
Closed last month.
How do I convert 20230102 to Monday?
Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd.
Parse with strptime and format with strftime:
>>> from datetime import datetime
>>> n = 20230102
>>> datetime.strptime(str(n), "%Y%m%d").strftime("%A")
'Monday'
See strftime() and strptime() Format Codes for documentation of the % strings.
You can convert number string into weekday using "datetime" module
import datetime
def get_weekday(date):
date = datetime.datetime.strptime(date, '%Y%m%d')
return date.strftime('%A')
print(get_weekday('20230102'))
This is how you can achieve your desired output.
You can do it with weekday() method.
from datetime import date import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()] #Friday
This question already has answers here:
Convert String with month name to datetime
(2 answers)
Closed 6 months ago.
So I have a date in this format "August 10, 2022". I need to reformat the date in this way "2022-08-10". How do I do that in python ?.
For this, you can use datetime, specifically on this behaviour.
from datetime import datetime
a = "August 10, 2022"
b = datetime.strptime(a, '%B %d, %Y') # Converts to datetime format
c = b.strftime('%Y-%m-%d') # Converts from datetime to desired format
print(c)
# output
2022-08-10
This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
Closed 9 months ago.
I have
5/7/2022 12:57(m/d/yyy)
5/7/2022 13:00 PM(m/d/yyy) time formats.
There are two types of time formats in a column of excel file which I have downloaded.
I want to convert it to '%Y-%m-%d %H:%M:%S'.
(The column is in string format).
I guess you have your file loaded from excel to dataframe.
df['date_col'] = pd.to_datetime(df['date_col'], format='%Y-%m-%d %H:%M:%S')
from dateutil.parser import parse
datestring = "5/7/2022 12:57"
dt = parse(datestring)
print(dt.strftime('%Y-%m-%d %H:%M:%S')) #2022-05-07 12:57:00
You can turn string input to datetime by doing this:
from datetime import datetime
example1 = "5/7/2022 12:57"
example2 = "5/7/2022 13:00 PM"
datetime_object1 = datetime.strptime(example1, "%m/%d/%Y %H:%M")
datetime_object2 = datetime.strptime(example2, "%m/%d/%Y %H:%M %p")
and then you can represent the datetime variable with a string:
formatted_datetime1 = datetime_object1.strftime("%Y-%m-%d, %H:%M:%S")
formatted_datetime2 = datetime_object1.strftime("%Y-%m-%d, %H:%M:%S")
You can try using pandas.Series.dt.strftime method, that will allow you to convert a field into the specified date_format, in this case %Y-%m-%d %H:%M:%S.
df['Column'] = df['Column'].dt.strftime('%Y-%m-%d %H:%M:%S')
This question already has answers here:
Python: How to convert datetime format? [duplicate]
(2 answers)
Closed 1 year ago.
Have dd-mm-yy in "date" column
05-01-15
need yyyy-mm-dd
2015-01-05
Solved with
df['date'] = pd.to_datetime(df.date, format='%d-%m-%y', errors='coerce')
Has it another solution?
from datetime import datetime
now = datetime.now()
date_time = now.strftime("%Y-%m-%d")
print("date and time:",date_time)
Or have a closer look at strftime documentation.
df['date'] = pd.to_datetime(df['date'])
Will transform your custom format to pandas recognized datetime, which you will be able to use for calculations.
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I know this has been asked a few times, but my scenario is a little different... The objective I need to accomplish is to convert a string of digits '20150425' (which happens to be a date), into a date format such as, '2015-04-25'. I need this because I am trying to compare date objects in my code, but have one variable type represented as a string.
Example below:
date = '20150425' ## want to convert this string to date type format
# conversion here
conv_date = '2015-04-25' ## format i want it converted into
Hope this is clear. Should not be difficult, just do not know how to do it.
This works
from datetime import datetime
date = '20150425'
date_object = datetime.strptime(date, '%Y%m%d')
date_object
>>> datetime.datetime(2015,4,25,0,0)
Assuming the date strings will always be 8 characters:
date = '20150425'
fdate = "{}-{}-{}".format(date[0:4], date[4:6], date[6:]) # 2015-04-25
Alternatively, you can go the "heavier" route and use the actual datetime class:
from datetime import datetime
date = '20150425'
dt = datetime.strptime(date, "%Y%m%d")
dt.strftime("%Y-%m-%d") # 2015-04-25