I have the following list of strings that looks like this
dates_list = ['7.0:2.0', '6.0:32.0', '6.0:16.0', '6.0:16.0', '6.0:17.0', '6.0:2.0', '6.0:1.0']
I want to plot this into a graph but I need to convert into date time of minutes and seconds (%M:%S)
I tried using strptime
for i in dates_list:
datetime_object = datetime.strptime(i,'%M:%S')
I get the following error:
ValueError: time data '7.0:2.0' does not match format '%M:%S'
Is there a way to handle floats or do I have to convert the strings to int and remove the decimals?
This will allow you to introduce any decimal:
from datetime import datetime
for i in dates_list:
time_list = i.split(':')
minute_decimal = time_list[0].split('.')[1]
second_decimal = time_list[1].split('.')[1]
datetime_object = datetime.strptime(i,'%M.{0}:%S.{1}'.format(minute_decimal,second_decimal)).time()
Try this, you need to correctly specify the format
import datetime as dt
dates_list = ['7.0:2.0', '6.0:32.0', '6.0:16.0', '6.0:16.0', '6.0:17.0', '6.0:2.0', '6.0:1.0']
for i in dates_list:
datetime_object = dt.datetime.strptime(i,'%M.0:%S.0').time()
print(datetime_object)
I would do this with pd.to_datetime(). However, it would add unnecessary date information which you can then remove with strftime:
[x.strftime("%M:%S") for x in pd.to_datetime(dates_list,format='%M.0:%S.0')]
Returning:
['07:02', '06:32', '06:16', '06:16', '06:17', '06:02', '06:01']
Related
Is there a way to convert a string date that is stored in some non-traditional custom manner into a date using datetime (or something equivalent)? The dates I am dealing with are S3 partitions that look like this:
year=2023/month=2/dayofmonth=3
I can accomplish this with several replaces but im hoping to find a clean single operation to do this.
You might provide datetime.datetime.strptime with format string holding text, in this case
import datetime
dt = datetime.datetime.strptime("year=2023/month=2/dayofmonth=3","year=%Y/month=%m/dayofmonth=%d")
d = dt.date()
print(d) # 2023-02-03
you can do that converting your string into a date object using "datetime" combined with strptime() method.
The strtime() takes two arguments, the first is the string to be parsed, and the second a string with the format.
Here's an example:
from datetime import datetime
# your string
date_string = "year=2023/month=2/dayofmonth=3"
# parse the string into a datetime object
date = datetime.strptime(date_string, "year=%Y/month=%m/dayofmonth=%d")
# print the datetime object
print(date)
I have some columns which has time in string format like '01:19:55' and so on. I want to convert this string into time format. For that, I am trying to use the code like below:
col_list2=['clm_rcvd_tm','gtwy_rcvd_tm']
pandas_df_1=df2.toPandas()
for x in col_list2:
pandas_df_1[x]=pd.to_datetime(pandas_df_1[x].replace(" ",""),format='%H:%M:%S').dt.time
As an output, these clumns are returning decimal values. (Ex:0.26974537037037)
Any help will be appreciated.
If you have a string in your pandas dataframe as you mentioned i.e;
01:19:55 so that you can easily convert this by using python datetime module
For example;
import datetime
str = "01:19:55"
dt = datetime.strptime(str, '%H:%M:%S') # for 24 hour format
dt = datetime.strptime(str, '%I:%M:%S') # for 12 hour format
for references have a look
Python Datetime formating
How can one make 2020/09/06 15:59:04 out of 06-09-202015u59m04s.
This is my code:
my_time = '06-09-202014u59m04s'
date_object = datetime.datetime.strptime(my_time, '%d-%m-%YT%H:%M:%S')
print(date_object)
This is the error I receive:
ValueError: time data '06-09-202014u59m04s' does not match format '%d-%m-%YT%H:%M:%S'
>>> from datetime import datetime
>>> my_time = '06-09-202014u59m04s'
>>> dt_obj = datetime.strptime(my_time,'%d-%m-%Y%Hu%Mm%Ss')
Now you need to do some format changes to get the answer as the datetime object always prints itself with : so you can do any one of the following:
Either get a new format using strftime:
>>> dt_obj.strftime('%Y/%m/%d %H:%M:%S')
'2020/09/06 14:59:04'
Or you can simply use .replace() by converting datetime object to str:
>>> str(dt_obj).replace('-','/')
'2020/09/06 14:59:04'
As your error says what you give does not match format - %d-%m-%YT%H:%M:%S - means you are expecting after year: letter T hour:minutes:seconds when in example show it is houruminutesmsecondss without T, so you should do:
import datetime
my_time = '06-09-202014u59m04s'
date_object = datetime.datetime.strptime(my_time, '%d-%m-%Y%Hu%Mm%Ss')
print(date_object)
Output:
2020-09-06 14:59:04
You need to always make sure that your desired date format should match up with your required format.
from datetime import datetime
date_object = datetime.strptime("06-09-202015u59m04s", '%d-%m-%Y%Hu%Mm%Ss')
print(date_object.strftime('%Y/%m/%d %H:%M:%S'))
Output
2020/09/06 15:59:04
I would like to convert two lists of strings into lists with one same time format.
Here are our two lists:
# first list: strings with UTC format
firstlist = ['2013-08-16T07:35:01Z','2012-11-17T17:07:49Z','2012-11-09T23:24:13Z']
# second list: strings with day-month-year format
secondlist = ['04-06-2016','31-10-2018','12-04-2019']
I would like to convert these two lists and get the same format year-month-day for each item:
['2013-08-16','2012-11-17','2012-11-09'] # expected for the first list
['2016-06-04','2018-10-31','2019-04-12'] # expected for the second list
I tried with just one item per list:
import time
time.strptime("2013-08-16T07:35:01Z", "%d %m %y")
time.strptime("04-06-2016", "%d %m %y")
But I get an error:
ValueError: time data '2013-08-16T07:35:01Z' does not match format '%d %m %y'
I found these two documentations: time and datetime.
But I am still really confused. There is probably something wrong with my method, but I struggle to find the right one for this case.
strptime returns a struct_time object, given a string to parse. You need to specify the date/time format of the actual input string:
import time
# string to time struct
a = time.strptime("2013-08-16T07:35:01Z", "%Y-%m-%dT%H:%M:%SZ")
b = time.strptime("04-06-2016", "%d-%m-%Y")
Then, use strftime to format the struct_time object into a string:
# time struct to formatted string
a_formatted = time.strftime('%Y-%m-%d', a)
b_formatted = time.strftime('%Y-%m-%d', b)
print(a_formatted,b_formatted)
Output:
2013-08-16 2016-06-04
This does it
import dateutil.parser
firstlist = list (map (lambda x: str (dateutil.parser.parse (x).date()), firstlist))
secondlist = list (map (lambda x: str (dateutil.parser.parse (x).date()), secondlist))
Use dateutil.parser.parse to convert to datetime.
Source: here
You can use dateutil parser to parse almost any time format.
import datetime
from dateutil.parser import parse
firstlist = ['2013-08-16T07:35:01Z','2012-11-17T17:07:49Z','2012-11-09T23:24:13Z']
secondlist = ['04-06-2016','31-10-2018','12-04-2019']
new_firstlist = [datetime.datetime.strftime(parse(dt), "%Y-%m-%d") for dt in firstlist]
new_secondlist = [datetime.datetime.strftime(parse(dt), "%Y-%m-%d") for dt in secondlist]
print(new_firstlist)
print(new_secondlist)
I have an column in excel which has dates in the format ''17-12-2015 19:35". How can I extract the first 2 digits as integers and append it to a list? In this case I need to extract 17 and append it to a list. Can it be done using pandas also?
Code thus far:
import pandas as pd
Location = r'F:\Analytics Materials\files\paymenttransactions.csv'
df = pd.read_csv(Location)
time = df['Creation Date'].tolist()
print (time)
You could extract the day of each timestamp like
from datetime import datetime
import pandas as pd
location = r'F:\Analytics Materials\files\paymenttransactions.csv'
df = pd.read_csv(location)
timestamps = df['Creation Date'].tolist()
dates = [datetime.strptime(timestamp, '%d-%m-%Y %H:%M') for timestamp in timestamps]
days = [date.strftime('%d') for date in dates]
print(days)
The '%d-%m-%Y %H:%M'and '%d' bits are format specififers, that describe how your timestamp is formatted. See e.g. here for a complete list of directives.
datetime.strptime parses a string into a datetimeobject using such a specifier. dateswill thus hold a list of datetime instances instead of strings.
datetime.strftime does the opposite: It turns a datetime object into string, again using a format specifier. %d simply instructs strftime to only output the day of a date.