How can I get all the dates of current week? - python

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))

Related

Python / Bloomberg api - historical security data incl. weekends (sat. and sun.)

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

Converting Datetimeindex of a dataframe to week numbers

I am very new to Python and cannot seem to solve the problem on my own. Currently I have a dataset which I already converted to a DataFrame using pandas which has a datetimeindex according to yyyy-mm-dd-HH-MM-SS with time stamps of minutes. The attached figure shows the already interpolated dataframe.
enter image description here
Now I want to convert the date/datetimeindex to week numbers to plot the corresponding HVAC Actual, Chiller power etc. to their week number. The index already was set to time but I got an error telling that 'Time' was not recognized in the columns. I tried to recall the index like in the code below and from there create a new column using dt.week
building_interpolated = building_interpolated.set_index('Time')
building_interpolated['Week number'] =
building_interpolated['Time'].dt.week
If I am correct this should create a new column called Week number with the week number in it. However, I still get an error telling that ['Time'] is not in the columns (see figure below)
enter image description here
Anyone who can help me?
Regards, nooby Boaz ;)
df.index = df.index.to_series().dt.isocalendar().week

Determine the amount of days between events excluding weekends in python

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!

Calculate Number of Last Week in Isocalendar Year

I am writing a script to grab files from a directory based on the week number in the filenames. I need to grab files with week N and week N+1 in the filenames.
I have the basics down, but am now struggling to figure out the rollover for new years (the file format uses the isocalendar standard). Isocalendars can have either 52 or 53 weeks in them, so I need a way to figure out if the year I'm in is a 52 or 53 week year so that I can then compare to the results of datetime.now().isocalendar()[1] and see if I need to set N+1 to 1.
Is there a built in python function for this?
Why not just use (datetime.now() + timedelta(days=7)).isocalendar()[1] rather than calculating it from the current week number at all?

Python: How to extract time date specific information from text/nltk_contrib timex.py bug

I am new to python. I am looking for ways to extract/tag the date & time specific information from text
e.g.
1.I will meet you tomorrow
2. I had sent it two weeks back
3. Waiting for you last half an hour
I had found timex from nltk_contrib, however found couple of problems with it
https://code.google.com/p/nltk/source/browse/trunk/nltk_contrib/nltk_contrib/timex.py
b. Not sure of the Date data type passed to ground(tagged_text, base_date)
c. It deals only with date i.e. granularity at day level. Cant find expression like next one hour etc.
Thank you for your help
b) The data type that you need to pass to ground(tagged_text, base_date) is an instance of the datetime.date class which you'd initialize using something like:
from datetime import date
base_date = date.today()

Categories

Resources