I am trying to convert strings e.g. "2010-01-01 10:09:01" into datetime to the precision of milliseconds. However even after adding 0 milliseconds at the back of the string e.g. "2010-01-01 10:09:01.000", the datetime always truncates the milliseconds part off. How do I make sure the datetime is always to the precision of millisecond even if there are 0 milliseconds in the timestamp? Thanks! :)
truncating the millisecond off
If I understood you correctly, you can use datetime.microseconds():
from datetime import datetime
a = datetime.strptime("2010-01-01 10:09:01.020", "%Y-%m-%d %H:%M:%S.%f")
print(a.microsecond) # >> 20000
print(a.microsecond//1000) # >> 20
And you can add if blocks or something that you need.
This is inside of value "a":
Related
I have 1000 of UYC timestamps in csv file, I want to convert it into date and time but I am only interested in second like
Timestamps= 1666181576.26295,
1666181609.54292
19/10/2022 15:45:25.34568
from that I only have interest in 25.34568 seconds, also the numbers after points. How can I get this type of conversion in python? Mostly the search on the internet is interested in conversation from UTC to time and date but I also want precision in seconds.
from datetime import datetime
from decimal import Decimal
ts = 1666181576.26295
timestamp = datetime.fromtimestamp(ts)
result = timestamp.second + Decimal(timestamp.microsecond)/1000000
print(result)
Will result in 56.26295
You can use datetime,
from datetime import datetime
ts = 1666181576.26295
mseconds = datetime.utcfromtimestamp(ts).microsecond
Simplest way I can see to do this is by splitting the timestamp to output everything from seconds onwards
timestamp = 1666181609.54292
temp = datetime.utcfromtimestamp(timestamp)
output = str(temp)
print(output[17:])
The following ValueError is being raised while running the following code. The date is passed as a string from an another component, where I need to strip out the time.
ValueError: time data '2022-03-24T14:02:24.6413975' does not match format '%Y-%m-%d %H:%M:%S,%f'
The code:
from datetime import datetime
date='2022-03-24T14:02:24.6413975'
time = datetime.strptime(date, "%Y-%m-%d %H:%M:%S,%f")
if time > '09:30' :
print("do some thing")
The primary issue you're facing is the datetime format, as explained by the error message.
The .%f formatter can only accept six decimal places.
The T is missing from the format string.
There is a comma before the %f formatter, where there should be a full stop.
Therefore, this is the formatting string you need:
'%Y-%m-%dT%H:%M:%S.%f'
Additionally, time can be parsed from a datetime object simply by calling the .time() function as follows. String parsing should not be used, if at all possible.
time = dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%f").time()
Next, the if statement should compare datetime object to datetime object, as:
if time > dt.time(9,30):
...
Therefore, the complete solution is as follows:
import datetime as dt
# Fractional seconds reduced to 6 decimal places.
date = '2022-03-24T14:02:24.641397'
# Use the .time() function to extract the time.
time = dt.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%f').time()
# Compare datetime object to datetime object.
if time > dt.time(9,30):
print('Do some stuff ...')
This should work:
import datetime
date='2022-03-24T14:02:24.6413975'
time = date.split('T')[1].split(':')
time = datetime.time(int(time[0]), int(time[1]), int(time[2].split('.')[0]), int(time[2].split('.')[1][:6]))
if time > datetime.time(9, 30) :
print("do some thing")
Output:
do some thing
This just takes date, splits it T, splits the second part of the resulting string at every :, and passes all of them to datetime.time. The last two arguments to datetime.time have to be split a the decimal to get the microseconds, and the last one has to be shortened because of the limit on how long datetime allows microseconds to be.
Hope this may help:
def stripTime():
date='2022-03-24T14:02:24.6413975'
date=date.split('T')
print(date[1])
stripTime()
Output:
14:02:24.6413975
timestamp = 1539965545566873
timestamp = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S.{:03d}')
ValueError: year is out of range
I this include microseconds and milliseconds. So I can divide it by /1000 to lose the accuracy of microseconds, but I want to store milliseconds too.
Any idea how? Also, I'm using Django, if that matters.
A quick workaround:
timestamp = 1539965545566873
time, micro = divmod(timestamp, 1000000)
timestamp = datetime.utcfromtimestamp(time).strftime('%Y-%m-%d %H:%M:%S.')
timestamp += str(micro)
timestamp
>>>'2018-10-19 16:12:25.566873'
AFAIK utcfromtimestamp does not have a parameter that allows you to pass in microseconds.
How do you round a timestamp down to microseconds in python?
I have a time stamp like this.
2016-05-07 08:29:58.387888640
I have formatted this using format
%Y-%m-%d %H:%M:%S.%f
from the dataframe like this
pd.to_datetime(time['Out'], format="%Y-%m-%d %H:%M:%S.%f")
However, this gives it down to the nano second level. I want this to round off at microsecond but not sure what the best way of doing this would be. Can you kindly let me know?
To display the right amount of decimals, you can use the strptime method of the timestamp - its %f directive is set in to be in microseconds.
tstamp = pd.Timestamp('2016-05-07 08:29:58.387888640')
tstamp.strftime("%Y-%m-%d %H:%M:%S.%f")
This simply truncates the equivalent string into '2016-05-07 08:29:58.387888'.
If you're concerned about the rounding errors, you should also call the round method defined for pandas' timestamps and set resolution:
tstamp.round("1 us").strftime("%Y-%m-%d %H:%M:%S.%f")
Note how the last digit is now properly rounded: '2016-05-07 08:29:58.387889'.
What is the best way to handle portions of a second in Python? The datetime library is excellent, but as far as I can tell it cannot handle any unit less than a second.
In the datetime module, the datetime, time, and timedelta classes all have the smallest resolution of microseconds:
>>> from datetime import datetime, timedelta
>>> now = datetime.now()
>>> now
datetime.datetime(2009, 12, 4, 23, 3, 27, 343000)
>>> now.microsecond
343000
if you want to display a datetime with fractional seconds, just insert a decimal point and strip trailing zeros:
>>> now.strftime("%Y-%m-%d %H:%M:%S.%f").rstrip('0')
'2009-12-04 23:03:27.343'
the datetime and time classes only accept integer input and hours, minutes and seconds must be between 0 to 59 and microseconds must be between 0 and 999999. The timedelta class, however, will accept floating point values with fractions and do all the proper modulo arithmetic for you:
>>> span = timedelta(seconds=3662.567)
>>> span
datetime.timedelta(0, 3662, 567000)
The basic components of timedelta are day, second and microsecond (0, 3662, 567000 above), but the constructor will also accept milliseconds, hours and weeks. All inputs may be integers or floats (positive or negative). All arguments are converted to the base units and then normalized so that 0 <= seconds < 60 and 0 <= microseconds < 1000000.
You can add or subtract the span to a datetime or time instance or to another span. Fool around with it, you can probably easily come up with some functions or classes to do exaxtly what you want. You could probably do all your date/time processing using timedelta instances relative to some fixed datetime, say basetime = datetime(2000,1,1,0,0,0), then convert to a datetime or time instance for display or storage.
A different, non mentioned approach which I like:
from datetime import datetime
from time import sleep
t0 = datetime.now()
sleep(3)
t1 = datetime.now()
tdelta = t1 - t0
print(tdelta.total_seconds())
# will print something near (but not exactly 3)
# 3.0067
To get a better answer you'll need to specify your question further, but this should show at least how datetime can handle microseconds:
>>> from datetime import datetime
>>> t=datetime.now()
>>> t.microsecond
519943
NumPy 1.4 (in release candidate stage) has support for its own Date and DateArray objects. The one advantage is that it supports frequencies smaller than femtoseconds: http://projects.scipy.org/numpy/browser/trunk/doc/neps/datetime-proposal.rst
Otherwise I would go with the regular datetime subsecond frequencies.