Extract another value based on an input (or automated input) - python

Not seen this answered elsewhere. I want to automate a file to put in three headers based on the current week. Firstly i cant figure out how to get the current week. A similar thing to the excel formula weeknum(today()). So this week is week 25 in the fiscal calender.
I need the programme to work out the current week but then also input the previous 2 weeks so to pull back week 25,24 and 23. I cant just get the week number 25 above and -1 as when we hit week 1 next year the number will go to 0
Hope that makes sense. I heard date is a bit of a pain so hopefully its not too complicated.

I'm not sure what you are trying to accomplish but maybe this will help.
import datetime
datetime.date.today().isocalendar()
It returns
(2021, 25, 1)
So to get just week 25, simply do:
datetime.date.today().isocalendar()[1]

Related

Python code that returns the week of the month

I would like to know if there is a code that returns the week number of the month
for example, if the input data is November 2nd, 2022.
Then I would like the code to tell that it is the 1st week of the month.
The week should begin on Monday.
I tried looking for an answer in similar questions but could not find the right answer, could anyone help me out here?
Thanks :)

Working with Hours & Minutes in Python & openpyxl - Excel Formatting vs Strings & Ints

Hoping someone with some openpyxl or general Excel experience might be able to help.
I'm working on a project to record flying hours, and produce an Excel spreadsheet of flights completed in a month.
So far, I've used PySimpleGUI to create a nice front end, and got it working so it stores each flight's details as a dictionary, where the keys are terms like the names of the crew, the aircraft registration and so on. Each flight is separately stored in a dictionary for the current month.
To make sure the hours flown make sense, I've used number spinners so they can't get nonsense inputs. Each type of flying hour is recorded as 2 keys, one for hours and one for minutes. So the dictionary has a section with parts like:
'-firstPilotHours-': 1,
'-firstPilotMins-': 30,
'-captainHours-': 1,
'-captainMins-': 30,
.. and so on.
I've managed to get these put into Excel by converting them to strings and the concatenating them with a colon in the middle:
ws1.cell(row=sortieIdent, column=9).value = str(currentMonth[sortie]["-captainHours-"]) + ":" + str(currentMonth[sortie]["-captainMins"])
... so it appears as "1:30" in Excel, which is the way I used to input the data when I ran a manual Excel file for this purpose.
The cell's number format is set as "[h]:mm" to allow me to perform calculations on the values as hours and minutes, so there can be a monthly total shown and so on.
However, this is the point where I'm stuck. I think because I'm converting them to strings, even though they look like "1:30" in Excel, they're being handled in Excel like a string and not an integer. It's not possible to perform any calculations with them. If I overtype them in Excel with "1:30," then they move to the right hand side of the column and start behaving like numbers.
I can't think of any way to get these into Excel in a manner where I can carry out calculations on them. Can anyone help?
I've thought about having separate columns for the hours and minutes, but I can't figure out how to work calculations in that manner either. I also thought about just displaying them as strings as it works now, but doing the calculations in Python; but I can't figure out how to do proper "hours & minutes" calculations within Python.
Hopefully this gives you some insight into Excel's date/time processing.
For a given decimal number that represents date/time...
The left side of the decimal represents the days since 1/0/1900.
The right side of a decimal number represents time.
Practical Example:
The date time I am writing this post is: 24 May 2020 # 8:02pm
Excel's underlying value is: 43975.8347222222
43975 days since 1/0/1900 = 24 May 2020
.8347222222 = Decimal portion of 24 hours
Having that foundation down (let me know if it isn't clear).
Now we can tackle your example.
To express 1:30 into Excel's world.
We would need to turn 1 hour and 30 minutes into a decimal day
Your 1:30 = .0625 in Excel
To show .0625, you can Format the cell as "hh:mm" or whatever you want using the custom format.
Custom Format = Display value to the user
.0625 = Underlying value Excel uses to calculate
Hope this nudges you further down the road.

Is there a best-practice method for getting this day last year in python?

For instance, today is a Wednesday. If I wanted to get this specific Wednesday last year, the method I am using so far is to create a ISOCalendar tuple from today's date and subtract one year. Is this the best way to do this, or am I missing something simple in the datetime class to be able to better get comparable dates. I do not want to get the specific date, but the specific day of the week in that specific week.
I also do not know how to correctly account for leap days.

Iterating a custom date range over multiple years in Pandas

The short of it: How do I parse through yearly data in non-standard year. In my case Sept to Sept.
I've got a script to parse through years' worth of hourly temperature data and calculate the accumulated growing degree days (GDD) per year. Some demo data and the script are on this gist if you're curious where I'm at. The meat and potatoes though is getting the yearly cumulative sum with this:
df[col_name] = df.resample('Y')['dGDD'].cumsum()
and all works well. Each day will show the accumulated GDD in the proper column until Dec 31 when it starts from zero again.
My next goal is to calculate Chilling Degree Days which works similarly as GDD but it runs from Sept to Sept each year and I have no idea how to work that in (or what to properly google for help). I know I can set a date range to run it over, ie df['2012-9-1':'2013-9-1'] but I'm not sure how to automate it for the entirety of my data (2007-2018).
Thanks!
Solved it! Turns out the period of time I'm looking for is known as a 'Water Year'. Learning that lead me to another question and answer. That, combined with a closer look at the .resample() docs where I learned you can direct the resample to something other than index, got me this:
df['WaterYear'] = df.index + pd.DateOffset(months=-8)
df[col_name] = df.resample('Y', on='WaterYear')['dCDD'].cumsum()
And everything seems to be working swimmingly now.

NetCDF: how to create list of time values for years with 366 days (all_leap or 366_day calendar)?

I'd like to write a NetCDF that will contain 366 days per year for all years, with the Feb 28th value repeated as the Feb 29th value in the case of non-leap years. How would I build the list/array of time values so that the Feb 29th slot contains the same time value as Feb 28th during non-leap years? Is this really what I want to do, or is there another approach typically used for this? I haven't yet found an example of how to create a time coordinate variable with calendar attribute all_leap or 366_day.
My concern is that I'll need to do something to account for the "filler" Feb 29th in the non-leap years in order to satisfy software such as Panoply which I use for quick plots when doing data analysis. I'm not referring to the data variable values, I mean the actual time step values such as "5894 days since 1900". For example when I'm stepping through the data timestep by timestep (day by day) I want to make sure that I don't start getting off-by-one errors that end up confusing Panoply, so when I'm looking at a plot for a timestep it's interpreted correctly when it displays the time value in date format.
Maybe the crux of this is whether or not I can have duplicate values in the array of time step values, and if so will Panoply etc. handle these gracefully, i.e. when I'm constructing an array of time values to load into the time coordinate can I duplicate the value for Feb 28th in the array element mapping to Feb 29th when it's not a leap year?
This a tricky issue, which comes up when computing daily climatologies over many years. You want your computations to include 366 days even for non-leap years but to use NaN for Feb. 29.
You don't mention what language you are using to create your NetCDF files. There is an answer using Python and Pandas, in the context of creating climatologies at this question: Compute daily climatology using pandas python which might help get you started.
My answer to that question shows a method for dealing with the leap year issue.
I've created 30 year daily climatology files, using this method and Panoply has no problems viewing them.

Categories

Resources