Python CSV Finding average time taken - python

I am trying to find the average time taken. The value 'x' will allow me to get the time taken for every row there is, but how am I able to find the average time taken for all the rows. I will think it is something like x divided by count, but I am not able to find a solution to this... any pros out there can help me??
import datetime,time,csv
from itertools import islice
from Tkinter import Tk #python GUI programming
from tkFileDialog import askopenfilename
from collections import Counter
from datetime import datetime
import pandas
Tk().withdraw()
category_list=[]
description_list=[]
reported_date=[]
acknowledged_date=[]
count = 0
# hard code all possible date formats
date_formats = ['%m/%d/%Y %H:%M', '%-d-%b-%y', '%d/%m/%Y %h:%M %p', '%d/%m/%Y %H:%M', '%A, %d %B %Y %H:%M','%A, %d %B %Y %H:%M','%A %d %B %Y %H%M',"%d/%m/%Y %H:%M %p"," %d/%m/%Y %H:%M %p", '%d-%b-%y' ,
'%d.%m.%Y', '%d %b %Y %H%M hrs', '%d %b %Y %H%M', '%d-%m-%y', '%d-%b-%y', '%b-%d-%y', '%d-%a-%y','%e-%a-%y','%b %d %Y %H%M hrs','%d/%b/%Y %m:%M %p','%A, %e %B %Y %H:%M',' %d/%m/%Y %h:%M','%d-%b-%y','%m/%d/%Y %H:%M:%S %p']
#file = askopenfilename() #ask user which file to open
#f = open(file,'r')
with open('Feedback and Complaints_Sample Dataset.csv', 'rb') as f:
reader = csv.reader(f)
header = next(reader) #read 2nd line onwards
data= [] #make a list called data
for row in reader: #assign data in every column and name them respectively
for format in date_formats:
try:
reported_on = datetime.strptime(row[0], format) #try and get the dates
acknowledged_on = datetime.strptime(row[12], format) #try and get the dates
x= acknowledged_on-reported_on #time taken to acknowledge
#acknowledged_date.append(acknowledged_on)
#reported_date.append(reported_on)
count += 1
break # if correct format, dont test any other formats
except ValueError:
pass # if incorrect format, try other formats`enter code here`

Subtracting two datetime objects creates a timedelta object. You need to keep a total time, so create a timedelta object, and for each x add it to your total.
At the end, you can then divide your total_time by your count:
import csv
from itertools import islice
from datetime import datetime, timedelta
count = 0
total_time = timedelta()
# hard code all possible date formats
date_formats = ['%m/%d/%Y %H:%M', '%-d-%b-%y', '%d/%m/%Y %h:%M %p', '%d/%m/%Y %H:%M', '%A, %d %B %Y %H:%M','%A, %d %B %Y %H:%M','%A %d %B %Y %H%M',"%d/%m/%Y %H:%M %p"," %d/%m/%Y %H:%M %p", '%d-%b-%y' ,
'%d.%m.%Y', '%d %b %Y %H%M hrs', '%d %b %Y %H%M', '%d-%m-%y', '%d-%b-%y', '%b-%d-%y', '%d-%a-%y','%e-%a-%y','%b %d %Y %H%M hrs','%d/%b/%Y %m:%M %p','%A, %e %B %Y %H:%M',' %d/%m/%Y %h:%M','%d-%b-%y','%m/%d/%Y %H:%M:%S %p']
with open('Feedback and Complaints_Sample Dataset.csv', 'rb') as f:
reader = csv.reader(f)
header = next(reader) #read 2nd line onwards
for row in reader:
for format in date_formats:
try:
reported_on = datetime.strptime(row[0], format) #try and get the dates
acknowledged_on = datetime.strptime(row[12], format) #try and get the dates
x = acknowledged_on - reported_on #time taken to acknowledge
total_time += x
count += 1
break # if correct format, don't test any other formats
except ValueError:
pass # if incorrect format, try other formats`enter code here`
print "Total time taken:", total_time
print "Average time taken:", total_time / count
Note: your logic for the date_formats implies that both dates in a single row will always share the same date format.

Related

Date String in Datetime Format Python

Here is my error I do not manage to solve
ValueError: time data '1/31/2021 22:59' does not match format '%d/%m/%Y %H:%M:%S'
Here is my code
90% of the time my String date I need to convert goes in my try part and It works, I have a problem with my second part.
def StringToDateTime(DateString):
from datetime import datetime
try:
return datetime.strptime(DateString, '%Y-%m-%d %H:%M:%S')
except:
DateString = str(DateString)+':00'
return datetime.strptime(DateString, '%d/%m/%Y %H:%M:%S')
The error you're seeing is due to the str not having a seconds value -- the %S of the datetime format string.
Change the format string so it doesn't have the seconds placeholder, and it should work as expected:
try:
# Remove the %S from the format string here
return datetime.strptime(DateString, '%Y-%m-%d %H:%M')
except:
DateString = str(DateString)+':00'
return datetime.strptime(DateString, '%d/%m/%Y %H:%M:%S')
Or, if you want to alter the DateString as you do in your except clause:
# Add the seconds to the date string
DateString = f"{DateString}:00"
try:
return datetime.strptime(DateString, '%Y-%m-%d %H:%M:%S')
except:
return datetime.strptime(DateString, '%d/%m/%Y %H:%M:%S')

Convert hour string containing pm to datetime

I want to convert a string that contains PM notation to a datetime type. Thanks!
datetime.strptime('8/18/2020 11:08:54 PM', '%m/%d%Y %I:%M:%S %p' )
I am getting this error
ValueError: time data '8/18/2020 11:08:54 PM' does not match format '%m/%d%Y %I:%M:%S %p'
You can use this code. It's very simple.
r = datetime.strptime('8/18/2020 11:08:54 PM', '%m/%d/%Y %I:%M:%S %p')
Or
import dateparser
r = dateparser.parse('8/18/2020 11:08:54 PM')
print(r)
Here is an answer using the builtin datetime module:
from datetime import datetime
date_str = '8/18/2020 11:08:54 PM'
fmt = '%m/%d/%Y %I:%M:%S %p'
parsed_date = datetime.strptime(date_str, fmt)

How to get current GMT time in this very format "Sun, 22 Sep 2019 09:18:13 GMT" in python

Even tho it's not used anymore in favor of UTC, the endpoint i am trying to query requires an hmac signature with a date header in this format : "Sun, 22 Sep 2019 09:18:13 GMT"
This deprecated method new Date().toGMTString(); makes the js sdk works, i need an equivalent in python.
time.strftime("%a, %d %b %Y %I:%M:%S %Z", time.gmtime()) returns the full name (i.e. CENTRAL EUROPE TIME) while time.strftime("%a, %d %b %Y %I:%M:%S %z", time.gmtime()) returns "Sun, 22 Sep 2019 09:18:13 +0100".
The closest i got istime.strftime("%a, %d %b %Y %I:%M:%S GMT", time.gmtime()) which in theory print the right string, but i keep getting this error anyway: "{\"message\":\"HMAC signature cannot be verified, a valid date or x-date header is required for HMAC Authentication\"}"
import datetime
print(datetime.datetime.now(datetime.timezone.utc).strftime('%a, %d %b %Y %H:%M:%S GMT'))
This works for me :)
Source of the formattings
Have a look here: https://www.programiz.com/python-programming/datetime/strftime
Be sure that you use %Z and not %z.
from datetime import datetime
import time
now = datetime.now() # current date and time
date_time = time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.gmtime())
# output: date and time: Sun, 22 Sep 2019 10:01:53 GMT
print("date and time in GMT:",date_time)
# output: Local: Sun, 22 Sep 2019 10:01:53 AM UTC
print("Local: " + time.strftime("%a, %d %b %Y %I:%M:%S %p %Z\n"))

Formating a german date cause unconverted data remains:am

I have a string "Mai 2, 2018 6:35:52 AM".
I set the local to german (only one i can get to work is deu_deu)
when trying to then format I get the data remains error.
import pandas as pd
import locale
locale.setlocale(locale.LC_ALL, 'deu_deu')
input_file_path = 'C:\\Users\\xxx'
customer_file_path = pd.read_csv(input_file_path,sep='\t', warn_bad_lines=True, error_bad_lines=False)
customer_file_path['RECEIVED_DATE'] = customer_file_path['RECEIVED_DATE'].str.replace(',', '')
customer_file_path['RECEIVED_DATE'] = customer_file_path['RECEIVED_DATE'].str.replace('AM', 'am')
customer_file_path['RECEIVED_DATE'] = customer_file_path['RECEIVED_DATE'].str.replace('PM', 'pm')
customer_file_path['RECEIVED_DATE'] = pd.to_datetime(customer_file_path['RECEIVED_DATE'] ,format='%b %d %Y %I:%M:%S %p')
customer_file_path['RECEIVED_DATE'] = pd.to_datetime(customer_file_path['RECEIVED_DATE'] ,format='%b %d %Y %I:%M:%S %p').dt.strftime('%Y-%m-%d %H:%M:%S.%f')
Either one fails with the same message:
getlocale returns
locale.setlocale(locale.LC_ALL, 'deu_deu')
Out[167]: 'German_Germany.1252'

Python: timezone.localize() not working

I am having some issues getting timezone.localize() to work correctly. My goal is to grab today's date and convert it from CST to EST. Then finally format the datetime before spitting it out. I am able to format the date correctly, but the datetime is not changing from CST to EST. Additionally when I format the date I don't see the text representation of the timezone included.
Below I have listed out a simple program I created to test this out:
#! /usr/bin/python
#Test script
import threading
import datetime
import pexpect
import pxssh
import threading
from pytz import timezone
import pytz
est = timezone('US/Eastern')
curtime = est.localize(datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y"))
#test time change
#curtime = datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y")
class ThreadClass(threading.Thread):
def run(self):
#now = (datetime.datetime.now() + datetime.timedelta(0, 3600))
now = (datetime.datetime.now())
print "%s says Hello World at time: %s" % (self.getName(), curtime)
for i in range(3):
t = ThreadClass()
t.start()
.localize() takes a naive datetime object and interprets it as if it is in that timezone. It does not move the time to another timezone. A naive datetime object has no timezone information to be able to make that move possible.
You want to interpret now() in your local timezone instead, then use .astimezone() to interpret the datetime in another timezone:
est = timezone('US/Eastern')
cst = timezone('US/Central')
curtime = cst.localize(datetime.datetime.now())
est_curtime = curtime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y")
def run(self):
print("%s says Hello World at time: %s" % (self.getName(), est_curtime))
Use cst.localize to make a naive datetime into a timezone-aware datetime.
Then use astimezone to convert a timezone-aware datetime to another timezone.
import pytz
import datetime
est = pytz.timezone('US/Eastern')
cst = pytz.timezone('US/Central')
curtime = cst.localize(datetime.datetime.now())
curtime = curtime.astimezone(est)

Categories

Resources