Create a pseudo random number from microsecond - python

I'm starting to learn python now, so I'm sorry if it's a stupid question, but I couldn't figure it out or find another question like this. When I write
import datetime
datetime.datetime.now()
It generates a tuple, at least I suppose so because it's between parentesis (). I wanted to separate them in a list using .split(',') and then get the last number on the microsecond which would be a pseudo random number. Can anyone help me?
I solved my problem, I didn't knew there was a documetation explaining the classes! Thank you all for helping me! Here is how I solved it
import datetime
aee=datetime.datetime.isoformat(datetime.datetime.now())
aee=aee[-1]
print(aee)

Here is simple solution. Does that meet your expectations?
import datetime
import math
# This prints microseconds of current time
print(datetime.datetime.now().microsecond)
# This is used to extract milliseconds
print(math.floor(datetime.datetime.now().microsecond / 1000))

Related

Is it possible to manipulate negative time values in Python?

Is there a way to generate negative time values in Python?
I want to generate a time range ranging from -4 minutes to a variable positive time (between 5 to 10 min), something like this:
import datetime
import pandas as pd
time_range = range(-datetime.time(minute=4), datetime.time(minute=5))
# or
time_range = pd.date_range(-datetime.time(minute=4), datetime.time(minute=5))
But datetime does not seem to support negative values.
I need it to generate a graph like the following one but with a time/datetime index instead of integer values (A time/datetime index is especially useful on a plotly graph as it gives a readable index at any zoom level)
In addition, I believe that the possibility to generate negative time values could have many other applications.
datetime.time doesn't accept negative values
Maybe you can try to do something with timedelta
from datetime import timedelta
delta = timedelta(minutes=-4)
I hope this clue will help you.
Saying “thanks” is appreciated, but it doesn’t answer the question. Instead, vote up the answers that helped you the most! If these answers were helpful to you, please consider saying thank you in a more constructive way – by contributing your own answers to questions your peers have asked here.

get time difference in seconds for time referring to another timezone

I intend to find the time difference between two time variables in seconds. The issue here is that I am referring to time in a different zone. I have managed to find a solution, but it is a mix of pandas.datetime function and python datetime library. I guess, the objective can be achieved with just pandas/numpy alone and with fewer lines of code. below is my code, appreciate any guidance on how can i achieve the final_output more efficiently.
import pandas as pd
from datetime import timedelta
local_time = pd.to_datetime('now').tz_localize('UTC').tz_convert('Asia/Dubai')
t1 = timedelta(hours=local_time.now('Asia/Dubai').hour, minutes=local_time.now('Asia/Dubai').minute)
t2 = timedelta(hours=9, minutes=14)
final_output = (t2 - t1).seconds
You may want to convert both times to UTC, then find the difference. Programmers usually like to work with UTC until the time reaches the front end.

Random number generation with Python, ArcGIS 10.1

I have a shapefile with 1,000+ cases and three fields (DOUBLE) ran1, ran2 and ran3, which I have set up to receive the product of separate random number generation operations.
Unfortunately, the Random Number Generator (Environment setting) documentation and Parser:Python do not seem to be appropriate for this sort of thing.
getRandomValue()
import numpy.random as R
def getRandomValue(fieldName1):
return R.random()
Any ideas are welcome.
I'm not sure why you deem the code you posted as not appropriate.
For me the code below works great and to get random values written into fields you would just wrap it in an UpdateCursor.
import numpy.random as R
def getRandomValue(fieldName1):
return R.random()
print getRandomValue()
If the range of random numbers is not suitable then this StackOverflow Question has a good Answer.
Please note that the GIS Stack Exchange might have been a good alternative location to post this Question because it uses ArcPy from ArcGIS.

Python script: convert random date formats to fixed yyyy-mm-dd

I'm quite new to python and don't know much about it but i need to make a small script that when someone inputs a date in any format , it would then converts it in to yyyy-mm-dd format.
The script should be able to share elements of the entered date, and identify patterns.
It might be easy and obvious to some but making one by my self is over my head.
Thanks in advance!
This is a difficult task to do yourself; you might want to take a look at dateutil which has a rather robust parse() method that you can use to try and parse arbitrarily formatted date strings.
You can do something like this (not tested)
import locale
import datetime
...
parsedDate = datetime.strptime(your_string, locale.D_FMT)
print datetime.strftime(parsedDate, "%Y-%M-%d")
This assumes that the user will use its own local convention for dates.
You can use strftime for output (your format is "%Y-%M-%d").
For parsing input there's a corresponding function - strptime. But you won't be able to handle "any format". You have to know what you're getting in the first place. Otherwise you wouldn't be able to tell a difference between (for example) American and other dates. What does 01.02.03 mean for example? This could be:
yy.mm.dd
dd.mm.yy
mm.dd.yy

Comparing DateOffsets in pandas

Is there a way to compare the size of two DateOffset objects?
>>> from pandas.core.datetools import *
>>> Hour(24) > Minute(5)
False
This works with timedelta, so I assumed that pandas would inherit that behavior - or is the time system made from scratch?
pandas DateOffsets does not inherit from timedelta. It's possible for some DateOffsets to be compared, but for offsets like MonthEnd, MonthStart, etc, the span of time to the next offset is non-uniform and depends on the starting date.
Please feel free to start a github issue on this at https://github.com/pydata/pandas, we can continue the discussion there and it'll serve as a reminder.
Thanks.

Categories