I am quite new to python and already struggling with an easy task like importing the timestamps of a series of measurement from an excel list.
The excel file has one column for date and one for time. I need the data for further calculation like time difference etc.
I tried to different ways how to get the data. So far my codes looks like this:
method with pyexcel
import pyexcel as pe
import datetime
import time
from datetime import time
import timestring
for n in range(len(users)):
sheet = pe.get_sheet(file_name=users[n],name_columns_by_row=0)
sheet = sheet.to_array()
data_meas = np.array(sheet)
for row in range(len(data_meas)):
print(type(row))
input_time = data_meas[row,1]
input_date = data_meas[row,0]
times = [datetime.datetime.strptime(input_date, input_time, "%d %b %Y %H:%M:%S")]
I get this error for the last line:
TypeError: strptime() takes exactly 2 arguments (3 given)
method with xlrd
import xlrd
from datetime import time
inputdata = xlrd.open_workbook('file.xls')
sheet = inputdata.sheet_by_index(0)
for row in sheet:
input_date=sheet.cell_value(row,0)
input_time=sheet.cell_value(row,1)
date_values = xlrd.xldate_as_tuple(input_time, inputdata.datemode)
time_value = time(*date_values[3:])
TypeError: 'Sheet' object is not iterable
Does anybody know how to help me?
I appreciate every hint.
Regarding your first solution, strptime takes only one date string as input.
You should join input_date and input_time:
input_time = '18:20:00'
input_date = 'Mon, 30 Nov 2015'
time = datetime.datetime.strptime(' '.join([input_date, input_time]), "%a, %d %b %Y %H:%M:%S")
To create the whole list of datetime objects, you can try:
times = [datetime.datetime.strptime(' '.join([data_meas[row,0], data_meas[row,1]]), "%a, %d %b %Y %H:%M:%S") for row in range(len(data_meas))]
Edit:
If you want to keep the for loop, you have to append each datetime object to your list (otherwise you will only keep the last date):
data_meas = np.array([['07/11/2015 18:20:00'],['09/11/2015 21:20:00']])
#list initilization
times = []
for row in range(len(data_meas)):
input_date = data_meas[row,0]
#we add a new item to our list
times.append(datetime.datetime.strptime(input_date, "%d/%m/%Y %H:%M:%S"))
Now, you can access each datetime in the list times. To calculate time differences, you can check the documentation on timedelta.
#Create a timedelta object
t1 = times[1] - times[0]
#Convert time difference in seconds
t2 = t1.total_seconds()
Related
I'm generating a list of random dates using Datetime and need to display as dd/mm/yy (eg 24 March 20 is 24/03/20). I can get this sorted with strftime, however it breaks the sort as it goes left to right so it's taking the day and sorting in that order.
It seems like overkill to get a datetime object, convert into a string for formatting, then convert that string back into a datetime object to sort.
How can I sort this list for dates correctly?
Thanking you in advance!
import random
from datetime import datetime, timedelta
user_test_date = datetime.strptime("12/12/21", '%d/%m/%y')
ledger = []
''' Create date for transaction '''
def date_gen():
date_step = random.randrange(1, 60) # Set range of 2 months
raw_date = user_test_date + timedelta(days =- date_step) # Alter days value each loop
date = raw_date.strftime('%w %b %y') #Change format of date
ledger.append(date)
ledger.sort(key=lambda item: item[0], reverse=True) #Sort list of dates
for i in range(10):
date_gen()
print(ledger)
here:
date = raw_date.strftime('%w %b %y') #Change format of date
you convert the datetime object to str. Try instead to skip this line and replace the last line in date_gen with ledger.sort()
I want to add hours to a datetime and use:
date = date_object + datetime.timedelta(hours=6)
Now I want to add a time:
time='-7:00' (string) plus 4 hours.
I tried hours=time+4 but this doesn't work. I think I have to int the string like int(time) but this doesn't work either.
Better you parse your time like below and access datetime attributes for getting time components from the parsed datetime object
input_time = datetime.strptime(yourtimestring,'yourtimeformat')
input_seconds = input_time.second # for seconds
input_minutes = input_time.minute # for minutes
input_hours = input_time.hour # for hours
# Usage: input_time = datetime.strptime("07:00","%M:%S")
Rest you have datetime.timedelta method to compose the duration.
new_time = initial_datetime + datetime.timedelta(hours=input_hours,minutes=input_minutes,seconds=input_seconds)
See docs strptime
and datetime format
You need to convert to a datetime object in order to add timedelta to your current time, then return it back to just the time portion.
Using date.today() just uses the arbitrary current date and sets the time to the time you supply. This allows you to add over days and reset the clock to 00:00.
dt.time() prints out the result you were looking for.
from datetime import date, datetime, time, timedelta
dt = datetime.combine(date.today(), time(7, 00)) + timedelta(hours=4)
print dt.time()
Edit:
To get from a string time='7:00' to what you could split on the colon and then reference each.
this_time = this_time.split(':') # make it a list split at :
this_hour = this_time[0]
this_min = this_time[1]
Edit 2:
To put it all back together then:
from datetime import date, datetime, time, timedelta
this_time = '7:00'
this_time = this_time.split(':') # make it a list split at :
this_hour = int(this_time[0])
this_min = int(this_time[1])
dt = datetime.combine(date.today(), time(this_hour, this_min)) + timedelta(hours=4)
print dt.time()
If you already have a full date to use, as mentioned in the comments, you should convert it to a datetime using strptime. I think another answer walks through how to use it so I'm not going to put an example.
I have a string which contains a date. I need to create a list with previous 5 dates including the given date in python.
Example:
date = ['09-26-2016']
dates = ['09-21-2016','09-22-2016','09-23-2016','09-24-2016','09-25-2016','09-26-2016']
I need to create dates given date as input. The input is in string format.
You need to use datetime to parse the strings into dates and calculate timedelta
from datetime import datetime, timedelta
d='09-24-2016'
d1=datetime.strptime(d, '%m-%d-%Y')
[(d1-timedelta(days=i)).strftime('%m-%d-%Y') for i in range(6,0,-1)]
['09-20-2016','09-21-2016', '09-22-2016', '09-23-2016', '09-24-2016', '09-25-2016']
from datetime import datetime, timedelta
date = ['09-26-2016']
date_time_obj = datetime.strptime(date[0], '%m-%d-%Y')
dates=[]
dates.append(date_time_obj)
for i in range(0,4):
print(i)
j=i+1
print(j)
dates_list= date_time_obj.date() - timedelta(days=j)
dates.append(dates_list)
print(dates)
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.
I'm dealing with a large amount of data that has both values and times (in strings). I am converting the string time values into datetime values with the following code:
time = datetime.datetime.strptime(time, " %H:%M:%S.%f")
The only problem is that some of my data has the format: 24:00:00.004. So some of the data is actually over 24 hours
Python is giving me this error:
ValueError: time data ' 24:00:00:004' does not match format ' %H:%M:%S.%f'
The %H parameter can only parse values in the range 0-23. You'll have to manually deal with those specific time stamps:
try:
time = datetime.datetime.strptime(time, " %H:%M:%S.%f")
except ValueError:
time = time.replace(' 24', ' 23')
time = datetime.datetime.strptime(time, " %H:%M:%S.%f")
time += datetime.timedelta(hours=1)
Try parsing the hours separately:
hours, rest = time.split(':', 1)
time = datetime.timedelta(hours=int(hours)) + datetime.datetime.strptime(rest, "%M:%S.%f")
Seems like your data does not contain dates, but time spans, so you should maybe store your data as timedelta instead of datetime.
You can use this function to create a timedelta from your strings:
import re
from datetime import timedelta
def parseTimeDelta(s):
d = re.match(
r'((?P<days>\d+) days, )?(?P<hours>\d+):'
r'(?P<minutes>\d+):(?P<seconds>\d+)\.(?P<milliseconds>\d+)',
str(s)).groupdict(0)
return timedelta(**dict(( (key, int(value))
for key, value in d.items() )))
Parsing your time string '24:00:00.004' like this
>>>t = parseTimeDelta('24:00:00.04')
would result in a timedelta represented like this
>>> print t
1 day, 0:00:00.004000