I am iterating through column showing the due dates of invoices. I have also create a variable storing the date of Sunday of the current week.
I am trying to create a new row to show if the due_date is smaller than this week's sunday, I should pay the invoice.
However, when I run the code, the Status column only shows the value Pay.
My code is as below:
for index, row in df_320.iterrows():
if due_date[index] < sunday:
df_320['Status'] = "Pay"
elif due_date[index] >= sunday:
df_320['Status'] = "Skip"
I have tried the below code to see if all the conditions show True but it also shows False values.
for index, row in df_320.iterrows():
print(due_date[index] < sunday)
I would appreciate if you can point out what Im doing wrong.
DATEFRAME EXAMPLE:
enter image description here
Have you confirmed that due_date and Sunday are datetime / date objects? You need to parse the date strings into objects to perform comparisons between them. Link to datetime docs: https://docs.python.org/3/library/datetime.html
Also you seem to modify the same structure you are iterating (df_320), same column regardless of your position, are you sure this is what you want to do? I'm guessing you want to change 'row' instead?
Related
Background: Sometimes we need to take a date which is a month after than the original timestamp, since not all days are trading days, some adjustments must be made.
I extracted the index of stock close price, getting a time series with lots of timestamps of trading days.
trading_day_glossory = stock_close_full.index
Now, given a datetime-format variable date, with the following function, the program should return me the day variable indicating a trading day. But indeed it did not. The if condition is never evoked, eventually it added up to 9999:99:99 and reported error.
def return_trading_day(day,trading_day_glossory):
while True:
day = day + relativedelta(days=1)
if day in trading_day_glossory:
break
I reckon that comparing a timestamp with a datetime is problematic, so I rewrote the first part of my function in this way:
trading_day_glossory = stock_close_full.index
trading_day_glossory = trading_day_glossory.to_pydatetime()
# Error message: OverflowError: date value out of range
However this change makes no difference. I further tested some characteristics of the variables involved:
testing1 = trading_day_glossory[20] # returns a datetime variable say 2000-05-08 00:00:00
testing2 = day # returns a datetime variable say 2000-05-07 00:00:00
What may be the problem and what should I do?
Thanks.
Not quite sure what is going on because the errors cannot be reproduced from your codes and variables.
However, you can try searchsorted to find the first timestamp not earlier than a given date in a sorted time series by binary search:
trading_day_glossory.searchsorted(day)
It's way better than comparing values in a while loop.
I have a table which contains information on the number of changes done on a particular day. I want to add a text field to it in the format YYYY-WW (e. g. 2022-01) which indicates the week number of the day. I need this information to determine in what week the total number of changes was the highest.
How can I determine the week number in Python?
Below is the code based on this answer:
week_nr = day.isocalendar().week
year = day.isocalendar().year
week_nr_txt = "{:4d}-{:02d}".format(year, week_nr)
At a first glance it seems to work, but I am not sure that week_nr_txt will contain year-week tuple according to the ISO 8601 standard.
Will it?
If not how do I need to change my code in order to avoid any week-related errors (example see below)?
Example of a week-related error: In year y1 there are 53 weeks and the last week spills over into the year y1+1.
The correct year-week tuple is y1-53. But I am afraid that my code above will result in y2-53 (y2=y1+1) which is wrong.
Thanks. I try to give my answer. You can easily use datetime python module like this:
from datetime import datetime
date = datetime(year, month, day)
# And formating the date time object like :
date.strftime('%Y-%U')
Then you will have the year and wich week the total information changes
I have this piece of code:
cond=(gd_all_df['dateOfLastContact']> '2020-10-10') & (gd_all_df['dateOfLastContact']<pd.to_datetime('now').strftime("%Y-%m-%d"))
gd_all_df_test=gd_all_df_contact[cond]
I have a big data frame and I would like to work only with data from 2020-10-10
But I get this warning : UserWarning: Boolean Series key will be reindexed to match DataFrame index.
And the condition ('Cond')in gd_all_df_test is not applied
Not sure what format your dateOfLastContact column is.
First try getting it in proper format of date to compare by below method
gd_all_df['dateOfLastContact']=gd_all_df['dateOfLastContact'].apply(pd.to_datetime,format="%Y-%m-%d")
And then its better idea to have a variable for the date for comparison may be today as you have given the code snippet.
import datetime
date = datetime.datetime.now()
date = date.strftime('%Y-%m-%d')
cond = (gd_all_df[gd_all_df['dateOfLastContact']> '2020-10-10']) & (gd_all_df[gd_all_df['dateOfLastContact'] < date])
Let me know if you are still facing issue.
I'm sure this is really easy to answer but I have only just started using Pandas.
I have a column in my excel file called 'Day' and a Date/time column called 'Date'.
I want to update my Day column with the corresponding day of NUMEROUS Dates from the 'Date' column.
So far I use this code shown below to change the date/time to just date
df['Date'] = pd.to_datetime(df.Date).dt.strftime('%d/%m/%Y')
And then use this code to change the 'Day' column to Tuesday
df.loc[df['Date'] == '02/02/2018', 'Day'] = '2'
(2 signifies the 2nd day of the week)
This works great. The problem is, my excel sheet has 500000+ rows of data and lots of dates. Therefore I need this code to work with numerous dates (4 different dates to be exact)
For example; I have tried this code;
df.loc[df['Date'] == '02/02/2018' + '09/02/2018' + '16/02/2018' + '23/02/2018', 'Day'] = '2'
Which does not give me an error, but does not change the date to 2. I know I could just use the same line of code numerous times and change the date each time...but there must be a way to do it the way I explained? Help would be greatly appreciated :)
2/2/2018 is a Friday so I don't know what "2nd day in a week" mean. Does your week starts on Thursday?
Since you have already converted day to Timestamp, use the dt accessor:
df['Day'] = df['Date'].dt.dayofweek()
Monday is 0 and Sunday = 6. Manipulate that as needed.
If got it right, you want to change the Day column for just a few Dates, right? If so, you can just include these dates in a separated list and do
my_dates = ['02/02/2018', '09/02/2018', '16/02/2018', '23/02/2018']
df.loc[df['Date'].isin(my_dates), 'Day'] = '2'
I am working on a Dataframe in which there two column checkin date & checkout date. Initially those two columns were in string type. I have changed them to date time object and calculate days difference between then using checkout date - checkin date. Now I want to filter rows based on the stay duration. Could any one help me to do that as the difference came in also in datetime object format.
Here is my code so far:
New_Train['checkin_date']=pd.to_datetime(New_Train['checkin_date'])
New_Train['checkout_date']=pd.to_datetime(New_Train['checkout_date'])
print(New_Train.info())
#Now, Checking date & checkout date type changed to date time
New_Train['Checkin_Year'],New_Train['Checkin_Month']=
New_Train['checkin_date'].dt.year,New_Train['checkin_date'].dt.month
New_Train['Stay_Duration'] = New_Train['checkout_date']- New_Train['checkin_date']
Thanks in Advance