Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
datetime
#1
I have a document with dates and times inside.
I have converted them into datetime objects, but now
I am having trouble in putting the date and time together.
For instance, the first date is 2015-01-25 and the first time
is 14:05:41.274647.
Now I would like to see only one number of the format "%Y-%m-%d "%H:%M:%S.%f"
but if I write on python date 'and' time, I only get the time (see last line of the code).
How do I get them both?

from astral import Astral 
from scipy import *
from pylab import*
import numpy as np
from numpy import array
import matplotlib.pyplot as plt
import datetime
from datetime import datetime, timezone
import pandas as pd  
import pytz

file=open('bird_jan25jan16.txt','r')

#Turning datas in file into lists
orig_date=[]
orig_time=[]
movements=[]

for i in file:
    tempsplit=i.split(' ')
    orig_date.append(tempsplit[0])
    orig_time.append(tempsplit[1])
    movements.append(tempsplit[2])


orig_dates_list = [datetime.strptime(date, "%Y-%m-%d").date() for date in orig_date] #string to datetime conversion
orig_time_list=[datetime.strptime(time, "%H:%M:%S.%f").time() for time in orig_time] #string to datetime conversion
#


print(orig_dates_list[0] and orig_time_list[0]) #date 'and' time

by the way, despite of what I wrote on the last line,
I must be able to get the format "%Y-%m-%d "%H:%M:%S.%f"
for the whole list, and not just for the first value.
Reply
#2
dt_fmt = '%Y-%m-%d %H:%M:%S.%f'
your_dt = datetime.datetime.strptime(d + ' ' + t, dt_fmt)
You could iterate with zip over orig_date and orig_time, join them with whitespace, parse it with datetime.strptime and add the result to a new list.

This should work:
timestamps = []
for col_dt in zip(orig_date , orig_time):
    new_dt_str = ' '.join(col_dt)
    new_dt = datetime.datetime.strptime(new_dt_str, dt_fmt)
    timestamps.append(new_dt)
I didn't tested it.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
what are d and t on line 2 of your first code?
I tried to run it and it says
Error:
your_dt = datetime.datetime.strptime(d + ' ' + t, dt_fmt) NameError: name 'd' is not defined
Reply
#4
d contained the date as str, t contained the time as str.
I did not paste this part, because you have it already in your two lists.

The first example was to simplify it.
The second example uses zip to iterate over orig_date and orig_time.
The col_td has for each iteration two elements. One element from orig_date and one from orig_time. The join method concatenates the str-values. The delimiter is in this case a whitespace.
Then the new string is parsed by datetime.


It would be much easier, if you do the parsing of datetime, when you read the data.
Otherwise your computer touches the data twice. (With small data, it does not matter)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
what I did is erasing line 2 completely, and it worked.
Thank you.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020