I've got an array of seconds since the beginning of the year from a netCDF file and want to turn it into a datetime array. The time array is in units of seconds, every minute, i.e.:
timesec = ncfile['time'][:]
>>>timesec
0
60
120
180
240
300
360
420
...
And there is a basetime variable of the number of second since the begining of the year:
basetime = ncfile['base_time'][:]
>>>basetime
1457568000
basetime being March 10th at 12am.
How do I convert it into an array of format '2016-03-10 00:00:00' and the following minute being '2016-03-10 00:01:00' and so on. I'm sure there are many ways of doing it, but if you could recommend me one that'd be excellent.
Thank You!
Without any further information on the exact input and output format: You could just use a list comprehension to add the basetime to each of the seconds in timesec, then convert those to datetime using fromtimestamp:
>>> import datetime
>>> timesec = [60*i for i in range(3)]
>>> basetime = 1457568000
>>> [datetime.datetime.fromtimestamp(basetime + s) for s in timesec]
[datetime.datetime(2016, 3, 10, 1, 0),
datetime.datetime(2016, 3, 10, 1, 1),
datetime.datetime(2016, 3, 10, 1, 2)]
You can convert those to str, which is pretty much the format you want, or use isoformat or strftime for other formats:
>>> [str(datetime.datetime.fromtimestamp(basetime + s)) for s in timesec]
['2016-03-10 01:00:00', '2016-03-10 01:01:00', '2016-03-10 01:02:00']
Related
Have two data sets with different time format. One in epcoh time, seconds from 1/1/1970, and the other hours since 1/1/1900. Need to convert the first to the second one. How can I do that in python ?
Thanks
So, for starters, here are the necessary tools:
from datetime import datetime, timezone, timedelta
Then you need to establish the starting time:
atmos_epoch = datetime(1900, 1, 1, 0, 0, tzinfo=timezone.utc)
Then you can work with deltas to that time:
>>> d = datetime.now(timezone.utc) - atmos_epoch
>>> d
datetime.timedelta(days=43893, seconds=28215, microseconds=982930)
>>> d.total_seconds() / 60 / 60
1053439.8377730362
That last value is the difference in hours.
You can add deltas to that epoch timestamp:
>>> atmos_epoch + timedelta(hours=1000000)
datetime.datetime(2014, 1, 29, 16, 0, tzinfo=datetime.timezone.utc)
The other thing you talk about, seconds since 1970, are simply UNIX timestamps, which you can work with easily:
>>> datetime.now().timestamp()
1583395060.91666
>>> datetime.fromtimestamp(1500000000)
datetime.datetime(2017, 7, 14, 4, 40)
Now you have a way to convert from both values to datetime objects and from datetime objects to either value.
I have strings like:
first = '2018-09-16 15:00:00'
second = '1900-01-01 09:45:55.500597'
I want to compare them.
All methods I found like Convert string date to timestamp in Python requires to know the format of the string.
I don't know the format of the strings (see differences between first and second) all I know is that they can be converted to timestamps.
How can I convert them in order to compare them?
Edit:
The "largest" string that I can get is:
1900-01-01 09:45:55.500597
but I can also get:
1900-01-01
1900-01-01 09:45
1900-01-01 09:45:55
etc..
It's always YYYY-MM-DD HH-MM....
You can use pandas.to_datetime. It offers a lot of flexibility in the string timestamp format, and you can use it on single strings or list/series/tuples of timestamps.
>>> import pandas as pd
>>> day = pd.to_datetime('1900-01-01')
>>> minute = pd.to_datetime('1900-01-01 09:45')
>>> second = pd.to_datetime('1900-01-01 09:45:55')
>>> subsecond = pd.to_datetime('1900-01-01 09:45:55.500597')
>>> assert subsecond > second
>>> assert minute < second
>>> assert day < minute
You can use the dateutil module (pip install python-dateutil):
>>> from dateutil.parser import parse
>>> parse('2018-09-16 15:00:00')
datetime.datetime(2018, 9, 16, 15, 0)
>>> parse('1900-01-01 09:45:55.500597')
datetime.datetime(1900, 1, 1, 9, 45, 55, 500597)
From the list of its features:
Generic parsing of dates in almost any string format;
Once you have the datetime objects, you can compare them directly, there's no need to calculate the timestamps.
I want to subtract 2 times and convert that into a time array.
I consulted this How to calculate the time interval between two time strings. Stating this following code
from datetime import datetime as dt
import time
print("Time Subtraction")
FMT = '%Y-%m-%d %H:%M:%S'
time_tuple = (2018, 1, 13, 13, 51, 18, 2, 317, 0)
time2_tuple = (2018, 1, 15, 13, 50, 18, 2, 317, 0)
s1 = time.strftime(FMT, time_tuple)
s2 = time.strftime(FMT, time2_tuple)
tdelta = dt.strptime(s2, FMT) - dt.strptime(s1, FMT)
print(tdelta)
The result is:
Time Subtraction
1 day, 23:59:00
But I want to get a tuple/print that will look like this
tuple = (0,0,1,23,59,0,2,317,0)
I usually use time not datetime so I am not sure what to do. Any ideas?
tdelta is a datetime.timedelta object, therefore you are printing the string representation of that object. You can get the days, hours, minutes, etc by performing simple arithmetic (since they are stored as fractions-of-days) on the attributes:
def days_hours_minutes(delta):
return delta.days, delta.seconds//3600, (delta.seconds//60)%60
You can add as many of these attributes to the tuple as you'd like.
I have a time which is 13:11:06 and i want to -GMT (i.e -0530). I can minus it by simply doing -5 by splitting the string taking the first digit (convert to int) and then minus it and then re-join. But then i get it in a format which is 8:11:06 which is not right as it should be 08:11:06, secondly its a lengthy process. Is there a easy way to get my time in -GMT format (08:11:06)
This is what i did to get -GMT time after getting the datetime
timesplithour = int(timesplit[1]) + -5
timesplitminute = timesplit[2]
timesplitseconds = timesplit[3]
print timesplithour
print timesplitminute
print timesplitseconds
print timesplithour + ":" + timesplitminute + ":" + timesplitseconds
You could use Python's datatime library to help you as follows:
import datetime
my_time = "13:11:06"
new_time = datetime.datetime.strptime("2016 " + my_time, "%Y %H:%M:%S") - datetime.timedelta(hours=5, minutes=30)
print new_time.strftime("%H:%M:%S")
This would print:
07:41:06
First it converts your string into a datetime object. It then creates a timedelta object allowing you to subtract 5 hours 30 minutes from the datetime object. Finally it uses strftime to format the resulting datetime into a string in the same format.
Use the datetime module:
from datetime import datetime, timedelta
dt = datetime.strptime('13:11:06', '%H:%M:%S')
time_gmt = (dt - timedelta(hours=5, minutes=30)).time()
print(time_gmt.hour)
print(time_gmt.minute)
print(time_gmt.second)
s = time_gmt.strftime('%H:%M:%S')
print(s)
Output
7
41
6
07:41:06
Note that this subtracts 5 hours and 30 minutes as initially mentioned in the question. If you really only want to subtract 5 hours, use timedelta(hours=5).
You can use datetimes timedelta.
print datetime.datetime.today()
>>> datetime.datetime(2016, 3, 3, 10, 45, 6, 270711)
print datetime.datetime.today() - datetime.timedelta(days=3)
>>> datetime.datetime(2016, 2, 29, 10, 45, 8, 559073)
This way you can subtract easily
Assuming the time is a datetime instance
import datetime as dt
t = datetime(2015,12,31,13,11,06)
#t.time() # gives time object. ie no date information
offset = dt.timedelta(hours=5,minutes=30) # or hours=5.5
t -= offset
t.strftime(("%H:%M:%S") # output as your desired string
#'18:41:06'
If the object is datetime and you don't care about DST, the simplest thing you can do is,
In [1]: from datetime import datetime
In [2]: curr = datetime.now()
In [3]: curr
Out[3]: datetime.datetime(2016, 3, 3, 9, 57, 31, 302231)
In [4]: curr.utcnow()
Out[4]: datetime.datetime(2016, 3, 3, 8, 57, 57, 286956)
A function returns date and time in unicode format.
u'2014-03-06T04:38:51Z'
I wish to convert this to date and time format and subtract it with current datetime to get the number of days in between.
Thanks in advance
Check string is unicode
>>> import types
>>> type(u'2014-03-06T04:38:51Z') is types.UnicodeType
True
Converting strings to datetime:
>>> import datetime
>>> datetime.datetime.strptime(u'2014-03-06T04:38:51Z', '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2014, 3, 6, 4, 38, 51)
Subtract from today to
>>> import datetime
>>> today = datetime.datetime.today()
>>> yourdate = datetime.datetime.strptime(u'2014-03-06T04:38:51Z', '%Y-%m-%dT%H:%M:%SZ')
>>> difference = today - yourdate
print str(difference)
First you have to convert your string to a datetime.datetime object.
import datetime
then = datetime.datetime.strptime(u'2014-03-06T04:38:51Z', "%Y-%m-%dT%H:%M:%SZ")
then represents itself as datetime.datetime(2014, 3, 6, 4, 38, 51), which looks about right. Then you have to get today's date as a datetime.datetime.
now = datetime.datetime.now()
Finally subtract it from your date (or vice versa - the question didn't make it clear).delta is a datetime.timedelta object that stores increments in days, seconds and microseconds. The latter two are always positive, the first can be negative.
for delta in (now-then, then-now):
print(delta, "::", delta.days, delta.seconds, delta.microseconds)
This prints out:
-1 day, 20:18:14.250142 :: -1 73094 250142
3:41:45.749858 :: 0 13305 749858
Best try it with a few examples to convince yourself it's correct.