Python 3.10 datetime strptime not picking up time zone? - python-3.x

I have a timestamp embedded in some JSON data as a string, for ease of inspection and modification. An example looks like this:
"debug_time": 1670238819.9747384,
"last_saved": "2022-12-05 11:13:39.974725 UTC",
When loaded back in, I need to convert it back to a float for comparison against time.time() and similar things, however, I can't seem to find the magic incantations to have it restore the correct value.
In restoring the JSON data, I attempt to convert the string to a float via strptime() like this:
loaded_time = datetime.datetime.strptime(obj.last_saved, '%Y-%m-%d %H:%M:%S.%f %Z')
This does restore the timestamp to a valid datetime object, however calling .tzname() results in None, and my attempts to use loaded_time.replace(tzinfo=zoneinfo.ZoneInfo('UTC')) have not yielded any useful results.
In short, emitting loaded_time.timestamp() yields 1670267619.974725, which is 8 hours ahead of what it should be. I've tried using .astimezone(), in various permutations, but can't find a way to correctly have it convert to the client's local time.
I even tried to hard-code in my own time zone US/Pacific but it stubbornly refuses to give me that original debug_time value back.
This doesn't seem like it should be a difficult problem, but clearly I'm misunderstanding something about how python 3's time handling works. Any ideas are welcome!
Thank you for your time!

you have to use built in function replace like
.strftime("%Y-%m-%dT%H:%M:%S.%f%Z").replace("UTC", "")

Related

Set Datetime to 00:00:00 in LogicApp

currently I am working with a Logic App, I want to set the hours to a given DateTimeFormat to 00:00:00 with a Variable. After that I try to add an int variable as hours with it, but that doesn't work. Any ideas?
formatDateTime(addHours(utcNow(), -utcNow().Hour + variables('Daytime')), concat('yyyy-MM-ddTHH:mm:ss', variables('DateTimeOffset')))
It would be nice to know what values the variables have in them because without it, it's not as easy to provide a complete answer, but, bottom line, you can't use + like you have in the expression.
If you want to add and minus, etc., you need to use the inbuilt function add.
The expression builder is a function based approach.
Without knowing exactly what you want to achieve, something like this will help you along ...
concat(addHours(formatDateTime(utcNow(), 'yyyy-MM-dd'), 6, 'yyyy-MM-dd HH:mm:ss'), '+06:00')
... but will need to be cleaned up.
You also need to replace my hardcoded values with your variables.

Turning a Dataframe into a Series with .squeeze("columns")

I'm studying how to work with data right now and so I'm following along with a tutorial for working with Time Series data. Among the first things he does is read_csv on the file path and then use squeeze=True to read it as a Series. Unfortunately (and as you probably know), squeeze has been depricated from read_csv.
I've been reading documentation to figure out how to read a csv as a series, and everything I try fails. The documentation itself says to use pd.read_csv('filename').squeeze('columns') , but, when I check the type afterward, it is always still a Dataframe.
I've looked up various other methods online, but none of them seem to work. I'm doing this on a Jupyter Notebook using Python3 (which the tutorial uses as well).
If anyone has any insights into why I cannot change the type in this way, I would appreciate it. I'm not sure if I've misunderstood the tutorial altogether or if I'm not understanding the documentation.
I do literally type .squeeze("columns") when I write this out because when I write a column name or index, it fails completely. Am I doing that correctly? Is this the correct method or am I missing a better method?
Thanks for the help!
shampoo = pd.read_csv('shampoo_with_exog.csv',index_col= [0], parse_dates=True).squeeze("columns")
I would start with this...
#Change the the stuff between the '' to the entire file path of where your csv is located.
df = pd.read_csv(r'c:\user\documents\shampoo_with_exog.csv')
To start this will name your dataframe as df which is kind of the unspoken industry standard the same as pd for pandas.
Additionally, this will allow you to use a "raw" (the r) string which makes it easier to insert directories into your python code.
Once you are are able to successfully run this you can simply put df in a separate cell in jupyter. This will show you what your data looks like from your CSV. Once you have done all that you can start manipulating your data. While you can use the fancy stuff in pd.read_csv() I mostly just try to get the data and manipulate it from the code itself. Obviously there are reasons not to only do a pd.read_csv but as you progress you can start adding things here and there. I almost never use squeeze although I'm sure there will be those here to comment stating how "essential" it is for whatever the specific case might be.

Matplotlib pcolormesh incompatibility with datetime object

I'm processing time series from some instruments, and the numbers came in different patterns.
Sometimes I have the time as year, month, day, hour, etc., and I create the datetime array/list directly. When I print one element of the array (or list) created, I have something like this
datetime.datetime(2018, 4, 6, 12, 0, 0)
2018-04-06 12:00:00
And when I use it with matploblib.pcolormesh, it works.
However, now I have the time as 'seconds since 1970/1/1'. My first try was doing this
And I can use the array time_dt with plt.plot(), but when I use it with plt.pcolormesh() I got
TypeError: Incompatible X, Y inputs to pcolormesh; see help(pcolormesh)
After checking and re-checking everything, the only difference was the way I create the datetime array using matplotlib.dates, I guess. When I create a time list using this
everything goes fine! In the present case, I can go this way. But in other cases I can't create the time array and must convert from the files from variable sources... I need to understand what's going on.
What's happening? What do I missing?
Working further with this data... I found this other strange behavior!
I have the time_dt, created with matplotlib.dates.num2date()
and I have the time_dt2, created just with datetime.datetime()
Making a plot for the same variable with the two time arrays, I got this
So, it seems that time_dt and time_dt2 are about the same.
When I do this
This is my expected result from the begging. But... when I do this
The pcolormesh is gone, no error! Quite weird...

Get Date/Time Pattern in ICU4J

I have a requirement where I need the pattern ICU uses to format the given date/time.
For example: If I have something like DateFormat.getDateInstance(DateFormat.SHORT, en_US).format(currentDate) gives me 5/3/2015, then I need an API which can tell me that the pattern ICU4J internally using is d/M/yyyy.
I did figure out a solution, all you need to do is convert the DateFormat into a SimpleDateFormat and call toPattern().
((SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT, en_US)).toPattern() returns d/M/y which is what I almost needed.

How to parse an ISO string value to a NodaTime Instant?

I am getting to know NodaTime and like it a lot. But I don't know it that well (yet)!
Given a value such as '2014-04-08T09:30:18Z', what are the steps required to parse such a string to a NodaTime Instant?
Thank you!
I figured this out. For others who want to do the same thing, here is what I used:
var isoString = "2014-04-08T09:30:18Z";
var result = InstantPattern.GeneralPattern.Parse(isoString).Value;
The Value property in this case returns the actual Instant object. If you omit that, the result is of type ParseResult<Instant> in this case, and has other information such as whether the parsing succeeded, etc.
http://nodatime.org/1.2.x/api/html/T_NodaTime_Text_ParseResult_1.htm
There aren't a lot of examples on Noda Time yet, but I am really liking it and turning to it more and more. Fantastic work by the team who created it. Thank you!

Resources