So I'm building a program that extracts data from several different websites that will display all our business data on a dashboard.
I've managed to get to the point where I can extract the data I need, and have it set up to select todays data, previous week and month to date by importing datetime, and for the example of previous week the following:
date = datetime.date.today()
yesterday = (date - datetime.timedelta(days = 1)).strftime('%d')
lastWeek = (date - datetime.timedelta(days = 7)).strftime('%d')
today = date.strftime('%d')
Then just using the find element function:
browser.find_element_by_link_text(lastWeek).click()
However I'm running into problems at the beginning of the month, where I can't subtract 7 as it would take me into negative numbers and wouldn't go to the previous months date as each month has different days etc.
Is there any way of getting around this?
TIA
Related
Using pyodbc for python and a postgreSQL database, I am looking to gather the max data of a specific day 7 days ago from the script's run date. The table has the following columns which may prove useful, Timestamp (yyyy-MM-dd hh:mm:ss.ffff), year, month, day.
I've tried the following few
mon = currentDate - dt.timedelta(days=7)
monPeak = cursor.execute("SELECT MAX(total) FROM {} WHERE timestamp = {};".format(tableName, mon)).fetchval()
Error 42883: operator does not exist: timestamp with timezone = integer
monPeak = cursor.execute("SELECT MAX(total) FROM {} WHERE timestamp = NOW() - INTERVAL '7 DAY'".format(tableName)).fetchval()
No error, but value is returned as 'None' (didn't think this was a viable solution anyways because I want the max of the entire day, not that specific time)
I've tried a few different ways of incorporating year, date, and time columns from db table but no good. The goal is to gather the max of data for every day of the prior week. Any ideas?
You have to cast the timestamp to a date if you want to do date comparisons.
WHERE timestamp::date = (NOW() - INTERVAL '7 DAY')::date
Note that timestamptz to date conversions are not immutable, they depend on your current timezone setting.
I'm trying to get the previous trading day in relation to a given trading day. To start I am simply trying to make a function which returns the previous business day when given a date, like so:
import datetime
def get_previous_trading_day(day):
day = pd.to_datetime(day)
previous_trading_day = day - datetime.timedelta(days=1)
return previous_trading_day
But when I call my function and print the current vs previous date, the previous date is not the previous day:
2021-05-01 curr
1885-02-22 00:00:00 prev
How do I make this work?
If you change the calculation to use pd.tseries.offsets.BDay instead, you will get the previous business day (instead of the day before). I realise this will not work for bank holidays where no trading occurs. Your function works well when I try it for returning the previous day's date.
def get_previous_trading_day(day):
day = pd.to_datetime(day)
previous_trading_day = day - pd.tseries.offsets.BDay(1)
return previous_trading_day
Calling the function for tomorrow's date will return Friday's date:
get_previous_trading_day("2022-05-16")
#Out: Timestamp('2022-05-13 00:00:00')
For your returned date, you may have put a date format that was not read correctly by the pd.to_datetime. If you require a specific format, add the kwarg format= to this.
This is for Python:
I need a library that is able to do arithmetic operations on dates while taking into account the duration of a month and or year.
For example, say I add a value of "1 day" to 3/31/2020, the result of should return:
1 + 3/31/2020 = 4/1/2020.
I also would need to be able to convert this to datetime format, and extract day, year and month.
Does a library like this exist?
import datetime
tday = datetime.date.today() # create today
print("Today:", tday)
""" create one week time duration """
oneWeek = datetime.timedelta(days=7)
""" create 1 day and 1440 minutes of time duraiton """
eightDays = datetime.timedelta(days=7, minutes=1440)
print("A week later than today:", tday + oneWeek) # print today +7 days
And the output to this code snippet is:
Today: 2020-03-25
A week later than today: 2020-04-01
>>>
As you see, it takes month overflows into account and turns March to April. datetime module has lots of things, I don't know all its attributes well and haven't used for a long time. However, I believe you can find nice documentation or tutorials on the web.
You definitely can create any specific date(there should be some constraints though) instead of today by supplying day, month and year info. I just don't remember how to do it.
I am reading an .xlsx spreadsheet into a Pandas DataFrame so that I can remove duplicate rows based on all columns and export the DataFrame into a .csv. One of the columns is a date column formatted as MM/DD/YY.
Here is a sample of the unaltered data
This spreadsheet contains abnormal pay hours entries for a payroll that is payed every Friday based on hours from one week previous to the current week. Rows are added each day there is an abnormal function with that day's data. I want to tell pandas to only find duplicates in rows whose date is less than or equal to the Friday date one week previous from the current Friday (This script will only be ran on Fridays). For example, if today is Friday 12/7/18, I want to set a cutoff date of the previous Friday, 11/30/18, and only look at rows whose dates are on or before 11/30/18. How can I trim the DataFrame in this way before executing drop_duplicates?
you can use date and timedelta.
get todays date.
store the date one week from todays date.
filter your data (I'm not sure how you have it stored, but I used generic names)
from datetime import date, timedelta
today = date.today()
week_prior = today - timedelta(weeks=1)
df_last_week = df[df['date'] <= week_prior]
Note that using a fixed time window of 1 week (or 7 days) is fine if you are sure that your script will only ever be run on a Friday.
You can, of course programatically get the date of last Friday, and filter your dataframe on that date:
last_friday = datetime.now().date() - timedelta(days=datetime.now().weekday()) + timedelta(days=4, weeks=-1)
print(df[df['date'] <= pd.Timestamp(last_friday)])
I have a simple datetime field named date. Stores dates and time like this: 2015-07-04 01:40:00+00:00.
When I run
today = datetime.datetime.now().today()
x = game.objects.filter(date__year=today.year)
It works, however if i run
today = datetime.datetime.now().today()
x = game.objects.filter(date__month=today.month)
I get an empty list.
Current month is July. If you filter by the year part of the date being equal to current month, then you search for records being in the year of 07 AD. I believe you do not have such records. You need to filter by month of the date if you want to filter by month.