Assume this is my date time stamp list:
[datetime.datetime(2017, 11, 17, 9, 33, 11), datetime.datetime(2017, 11, 17, 9, 33, 36), datetime.datetime(2017, 11, 17, 9, 33, 48)]
A lot of examples have been given for converting these values to epoch, but the values are in GMT.
How do we convert it to Epoch Local time?
To put it in simpler words. The general epoch conversion coverts the given date into epoch considering that the datetime given is in GMT. But the given date time is localtime!
>>> x = datetime.datetime(2017, 11, 17, 9, 33, 36)
>>> x.timestamp()
1510882416.0
>>> x.ctime()
'Fri Nov 17 09:33:36 2017'
You can use this:
l = [datetime.datetime(2017, 11, 17, 9, 33, 11), datetime.datetime(2017, 11, 17, 9, 33, 36), datetime.datetime(2017, 11, 17, 9, 33, 48)]
epo = [x.strftime('%s') for x in l]
print(epo)
# ['1510898591', '1510898616', '1510898628']
You can try arrow lib:
import arrow
import datetime
x = datetime.datetime(2017, 11, 17, 9, 33, 36)
time = arrow.get(x, 'local').shift(hours=-6) // here you can change hours to what you want
print time
>>2017-11-17T03:33:36+00:00
time = time.format('YYYY-MM-DD HH:mm:ss')
print time
>>2017-11-17 03:33:36
Related
I have the following pandas dataframe
import pandas as pd
import datetime
foo = pd.DataFrame({'id': [1,2], 'time' :['[datetime.datetime(2021, 10, 20, 14, 29, 51), datetime.datetime(2021, 10, 20, 14, 46, 8)]', '[datetime.datetime(2021, 10, 20, 15, 0, 44), datetime.datetime(2021, 10, 20, 16, 13, 42)]']})
foo
id time
0 1 [datetime.datetime(2021, 10, 20, 14, 29, 51), datetime.datetime(2021, 10, 20, 14, 46, 8)]
1 2 [datetime.datetime(2021, 10, 20, 15, 0, 44), datetime.datetime(2021, 10, 20, 16, 13, 42)]
I would like to transform each element of the lists in the time column to a string with the format '%Y/%m/%d %H:%M:%S'
I know I can do this:
t = datetime.datetime(2021, 10, 20, 14, 29, 51)
t.strftime('%Y/%m/%d %H:%M:%S')
to yield the value '2021/10/20 14:29:51',
but I do not know how to do this operation for every string element of each list in the time column.
Any help ?
You just need to use list comprehension inside apply after converting string lists to actual lists with eval:
foo.time.apply(lambda str_list: [item.strftime('%Y/%m/%d %H:%M:%S') for item in eval(str_list)])
You can separate the list into rows first with explode and then use the dt accessor in pandas:
(foo
.explode('time')
.assign(time=lambda x: x.time.dt.strftime('%Y/%m/%d %H:%M:%S'))
)
I want to create a file path in the below format using python
Input\tmp +datetime.datetime(2019, 8, 11, 19, 22, 3)
Expect output: \tmp\20190811T192203
import datetime
dt = datetime.datetime(2019, 8, 5, 19, 22, 3)
output = "\\tmp\{}".format(dt.strftime("%Y%m%dT%H%M%S"))
print(output)
output
\tmp\20190805T192203
import datetime
date = datetime.datetime(2019, 8, 11, 19, 22, 3)
output = "\\tmp\\" + date.strftime("%Y%m%d") + "T" + date.strftime("%H%M%S")
Outputs:
\tmp\20190811T192203
This is the list that I am using
dates = [
datetime.datetime(2020, 8, 24, 8, 23),
datetime.datetime(2020, 8, 24, 12, 21),
datetime.datetime(2020, 8, 23, 17, 13),
datetime.datetime(2020, 8, 22, 4, 12),
datetime.datetime(2020, 8, 21, 13, 42),
datetime.datetime(2020, 8, 21, 12, 34),
datetime.datetime(2020, 8, 19, 5, 32),
datetime.datetime(2020, 8, 12, 2, 55),
datetime.datetime(2020, 8, 11, 10, 10),
datetime.datetime(2020, 8, 11, 13, 55),
datetime.datetime(2020, 8, 11, 13, 7)
]
And for calculating the time interval I used this:
dates[0]- dates[1]
and I got this:
datetime.timedelta(days=-1, seconds=72120)
It doesn't make sense at all!! Since datetime(year, month, day, hour, minute, second), the first element in the list is almost 4 hours ahead from the second one. But the result says something completely different.
Actually the result is correct.
"days = -1, seconds=72120 " comes from a normalization of the delta value.
For calculating the time in hours:
72.120 s / 60 / 60 ≈ 20h
20h - 24h = -4h
means Date[0] is 4 hours ahead of Date[1]
Dates and numbers work the same way in that if you want to get a positive result, you need to subtract the smaller value from the bigger value, not the other way around.
>>> datetime.datetime(2020, 8, 24, 12, 21) - datetime.datetime(2020, 8, 24, 8, 23)
datetime.timedelta(seconds=14280)
14280 seconds ~= 4 hours.
If you subtract them the other way (like in your code, subtracting larger from smaller), you get a value that equates to negative 4 hours:
>>> datetime.datetime(2020, 8, 24, 8, 23) - datetime.datetime(2020, 8, 24, 12, 21)
datetime.timedelta(days=-1, seconds=72120)
>>> (datetime.datetime(2020, 8, 24, 8, 23) - datetime.datetime(2020, 8, 24, 12, 21)).total_seconds()
-14280.0
Note that the default way of expressing this negative delta for formatting purposes is "minus one day plus 72120 seconds"; using total_seconds() converts it to simply a number of seconds which makes it a little easier to reason about IMO.
It makes absolutely sense. It does exactly what you asked it to do. You asked to subtract a later date from an earlier date, so the result is negative (1 day = 24 hours; 72120 seconds equals roughly 20 hours) so you have -24h+20h=-4h. If you turn it around you get the exact result:
print(dates[1]-dates[0])
Out:
3:58:00
You will have to check if value 'a' is higher than 'b'
# Simulating the list
case.append(datetime.datetime(2020,8,24,8,23))`
>>> case
[datetime.datetime(2020, 8, 24, 8, 23)]
>>> case.append(datetime.datetime(2020,8,24,12,21))
>>> case
[datetime.datetime(2020, 8, 24, 8, 23), datetime.datetime(2020, 8, 24, 12, 21)]
# Negative value outcome
>>> case[0] - case[1]
datetime.timedelta(days=-1, seconds=72120)
*Positive value outcome*
>>> case[1] - case[0]
datetime.timedelta(seconds=14280)
>>> 14280/60
238.0 (minutes)
>>> 14280/60/60
3.966666666666667 (hours)
This post shows the use of (abs):
why I have negative date by subtraction of two column?
I have an array of unixtime timestamps. How do I convert that using
datetime.utcfromtimestamp().strftime("%Y-%M-%D %H:%M:%S")
? My array is saved under "time". How do I utilize that array in this conversion?
Assuming your times are of the format datetime, you can loop through the list and convert each one.
Here is a quick example:
import datetime
time = []
for i in range(10):
time.append(datetime.datetime.now())
print(time) # output: [datetime.datetime(2020, 7, 8, 10, 7, 4, 314614), datetime.datetime(2020, 7, 8, 10, 7, 4, 314622)....
formattedTime = []
for t in time:
formattedTime.append(t.strftime('%Y-%m-%d %H:%M:%S'))
print(formattedTime) # output: ['2020-07-07/08/20 10:07:04', '2020-07-07/08/20 10:07:04', ....
# the update to my answer:
newTimes = []
for date_time_str in formattedTime:
newTimes.append(datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S'))
print(newTimes) # '%Y-%m-%d %H:%M:%S' [datetime.datetime(2020, 7, 8, 18, 56, 47), datetime.datetime(2020, 7, 8, 18, 56, 47), datetime.datetime(2020, 7, 8, 18, 56, 47),...]
Let me know if you have more questions.
I am also attaching this article for datetime which I found really helpful.
Here is the example in repl
How can I generate recurring dates using Python? For example I want to generate recurring date for "Third Friday of every second month". I want to generate recurring dates for daily, weekly, monthly, yearly (i.e., same as the recurrence function in Outlook Express).
import dateutil.rrule as dr
import dateutil.parser as dp
import dateutil.relativedelta as drel
start=dp.parse("19/02/2010") # Third Friday in Feb 2010
This generates the third Friday of every month
rr = dr.rrule(dr.MONTHLY,byweekday=drel.FR(3),dtstart=start, count=10)
This prints every third Friday:
print map(str,rr)
# ['2010-02-19 00:00:00', '2010-03-19 00:00:00', '2010-04-16 00:00:00', '2010-05-21 00:00:00', '2010-06-18 00:00:00', '2010-07-16 00:00:00', '2010-08-20 00:00:00', '2010-09-17 00:00:00', '2010-10-15 00:00:00', '2010-11-19 00:00:00']
rr is an iterable, so you can use slicing notation to pick out every other item. This prints the third Friday of every other month:
print map(str,rr[::2])
# ['2010-02-19 00:00:00', '2010-04-16 00:00:00', '2010-06-18 00:00:00', '2010-08-20 00:00:00', '2010-10-15 00:00:00']
Above, I used str to prettify the output a little bit. For more flexible string formatting of dates, use strftime: See http://au2.php.net/strftime or the man page for strftime for all the options.
print [d.strftime('%d/%m/%Y') for d in rr[::2]]
# ['19/02/2010', '16/04/2010', '18/06/2010', '20/08/2010', '15/10/2010']
You can give dateutil a try - especially its relativedelta and rrule fetures.
you may try to write this yourself. you will first need an iterator which generates dates separated by a given interval:
import datetime
def dateiter(start, resolution):
date = start
while True:
yield date
date += resolution
now, you can generate dates and filter them:
# generate a list of every tuesday of february
# this iterates over every day from now, and filtered according to the rules
# warning: infinite generator below, there is nothing to end the iteration
tuesdays_of_february = (date for date in dateiter(datetime.datetime.now(), datetime.timedelta(days=1)) if date.weekday() == 4 and date.month == 2)
you can call the iterator yourself until you have enough dates:
>>> next(tuesdays_of_february)
datetime.datetime(2010, 2, 19, 14, 25, 46, 171000)
now, you need to limit the results:
>>> from itertools import *
>>>
>>> # get the five next valid dates:
>>> list(islice(tuesdays_of_february),5)
[datetime.datetime(2010, 2,26, 14, 25, 46, 171000), datetime.datetime(2011, 2, 4
, 14, 25, 46, 171000), datetime.datetime(2011, 2, 11, 14, 25, 46, 171000), datet
ime.datetime(2011, 2, 18, 1 4, 25, 46, 171000), datetime.datetime(2011, 2, 25
, 14, 25, 46, 171000)]
>>>
>>> # or until a condition is met:
>>> list(takewhile( lambda date: date.year < 2014, tuesdays_of_february ))
[datetime.datetime(2012, 2, 3, 14, 25, 46, 171000), datetime.datetime(2012, 2, 1
0, 14, 25, 46, 171000), datetime.datetime(2012, 2, 17, 14, 25, 46, 171000), date
time.datetime(2012, 2, 24, 14, 25, 46, 171000), datetime.datetime(2013, 2, 1, 14
, 25, 46, 171000), datetime.datetime(2013, 2, 8, 14, 25, 46, 171000), datetime.d
atetime(2013, 2, 15, 14, 25, 46, 171000), datetime.datetime(2013, 2, 22, 14, 25,
46, 171000)]
don't forget to have a look at the documentation for the datetime module.