I have to write a program where I find how many days of the year equal 7.
Example:
1/3/21 = 1+3+2+1 = 7.
My first idea was to create a list for the days, and then somehow loop it through for each month (and save the dates in a new list), but I'm not sure that's the best way.
No need for any difficult code, this is a beginner assignment.
Thanks in advance!
Related
I'm pretty new to coding and have a problem resampling my dataframe with Pandas. I need to resample my data ("value") to means for every 10 minutes (13:30, 13:40, etc.). The problem is: The data start around 13:36 and I can't access them by hand because I need to do this for 143 dataframes. Resampling adds the mean at the respective index (e.g. 13:40 for the second value), but because 13:30 is not part of my indices, that value gets lost.
I'm trying two different approaches here: First, I tried every option of resample() (offset, origin, convention, ...). Then I tried adding the missing values manually with a loop, which doesn't run properly because I didn't know how to access the correct spot on the list. The list does include all relevant values though. I also tried adding a row with 13:30 as the index on top of the dataframe but didn't manage to convince Python that my index is legit because it's a timestamp (this is not in the code).
Sorry for the very rough code, it just didn't work in several places which is why I'm asking here.
If you have a possible solution, please keep in mind that it has to function within an already long loop because of the many dataframes I have to work on simultaneously.
Thank you very much!
df["tenminavg"] = df["value"].resample("10Min").mean()
df["tenminavg"] = df["tenminavg"].ffill()
ls1 = df["value"].resample("10Min").mean() #my alternative: list the resampled values in order to eventually access the first relevant timespan
for i in df.index: #this loop doesn't work. It should add the value for the first 10 min
if df["tenminavg"][i]=="nan":
if datetime.time(13,30) <= df.index.time < datetime.time(13,40):
df["tenminavg"][i] = ls1.index.loc[i.floor("10Min")]["value"] #tried to access the corresponding data point in the list
else:
continue
I'm currently working with an blpapi and trying to get a bdh of an index including weekends. (I'll later need to match this df with another date vector.)
I'm allready using
con.bdh([Index],['PX_LAST'],'19910102', today.strftime('%Y%m%d'), [("periodicitySelection", "DAILY")])
but this will return only weekdays (mon - fr). I know how this works in excel with the bbg function-builder but not sure about the wording within the blpapi.
Since I'll need always the first of each month,
con.bdh([Index],['PX_LAST'],'19910102', today.strftime('%Y%m%d'), [("periodicitySelection", "MONTHLY")])
wont work as well because it will return 28,30,31 and so.
Can anyone help here? THX!
You can use a combination of:
"nonTradingDayFillOption", "ALL_CALENDAR_DAYS" # include all days
"nonTradingDayFillMethod", "PREVIOUS_VALUE" # fill non-trading days with previous value
I have a use case where I need to count the server failures for current week. I am counting by reading a file where all the dates and failures are given. But I need to Calculate weekly failures, So I thought of to get all the dates in current week and compare that with dates in file and hence count the failures. So the question is how can I get all the dates of current week ? Also, how can i check if any date comes in that week?
Can anyone please help ?
Using pandas:
df.loc[df["dates"].dt.week == week_number]
This simply gets all the rows where week is equal to the specified week (you can find out that week by trying a dummy value and using .dt.week).
print(date.today())
for x in range(7):
print(date.today() + timedelta(days=x))
I'm sure the question was not descriptive enough, but here is what I'm looking for. I have 3 columns in my dataframe. Two are datetimes and the other is a float that contains either a 1 or 0. It is a status column and 1 means on 0 means off obviously. I need to find out whether I am able to get the different ranges of times when the status was 0 and 1.
Can I do this with pandas, or do I need to try something else?
Sample data from dataframe
Dataframe name is uptime. Columns in order from left to right are time_utc, state, local_time. I'm really not concerned with time_utc, so you can disregard that.
This is my first question on here as I wasn't really even sure how to google this question. Please let me know if more information is required, and I will provide what I can. Thank you in advance for any response lol.
Edit:
In the table shown in the picture, you can see it was down from 04:54:27 to 5:01:21, which is when it came back up, and was back down by 05:02:16. It then went back down until 05:09:24, where it was back up until 05:11:50. I am just trying to write something that can pull those ranges, and maybe store them in another dataframe.
Edit:
I am doing a terrible job of asking this question, I know, but hopefully this picture of example output will help.
If its by time here you go, and i dont know the column names so i am just gonna throw in a random one
selecte_df = df.loc[(df['time_utc'].dt.hour >= 6) & (df['time_local'].dt.hour <= 8) & (df['state'] == 1)] # Get hour range and state
selected_df.to_csv('new.csv', index=False) # Write to new csv
I was able to solve this with an incredibly complicated IF statement.
I am trying to determine the amount of days between updates on a ticket in zendesk, excluding weekends. I have been able to determine the difference between updates, but it doesnt take into account weekends. I want to determine if the user has updated their ticket within 3 days, excluding weekends, then close the ticket if it has been over 3 days. This is what I have:
updated_time = updated_time[0:10:1]
time_now = str(datetime.datetime.now())
time_now = time_now[0:10:1]
updated_time = dateutil.parser.parse(updated_time)
time_now = dateutil.parser.parse(time_now)
date_to_close = datetime.timedelta(days=3)
please_close = time_now - updated_time
My problem is because it doesnt distinguish between weekdays and weekends, it will automatically close the ticket on Monday if no one has responded over the weekend, which I would not like to count. I've been looking at the isoweekday() but its not really giving me what I need. I was thinking about trying to assign the ticket to a variable and assigning it a number, then increase that number by days without an update, that way it would not count the specific date but rather the number of days in between, but not sure how to do this or if it would even work.
Does anyone have an idea how to count the difference between days while excluding weekends? Is this possible?
If you already know how many days (of any kind), that value must be equal to 7w+x (that is, w full weeks and x days of the remaining partial week). You know that there are 5 weekdays in every full week; that just leaves figuring out how many of the remaining x days are weekdays, which you can determine from how many days until the first weekend and comparing that to x.
Ok, after a lot of digging and reaching out to another python programmer I know, he pointed me to the numpy function busday_count(). This does the calculation that I need. The reference is: Python - Exclude weekends between two Dates
Thanks very much for everyone's answers!