I have a pandas dataframe of electricity consumption data throughout a year, but would like to update the table to be in another year. I would like the data values to fall on the same weekdays as before.
What I have:
Date 00:00 ... WeekDay requiredDate requiredWeekDay
25/11/2018 20 Sunday 25/11/2018 Sunday
26/11/2018 30 Monday 26/11/2018 Monday
27/11/2018 25 Tuesday 27/11/2018 Tuesday
28/11/2018 35 Wednesday 28/11/2018 Wednesday
29/11/2018 40 Thursday 29/11/2018 Thursday
30/11/2018 15 Friday 30/11/2018 Friday
01/12/2017 65 Sunday 01/12/2018 Saturday
02/12/2017 34 Monday 02/12/2018 Sunday
03/12/2017 81 Tuesday 03/12/2018 Monday
04/12/2017 62 Wednesday 04/12/2018 Tuesday
...
What I would like:
Date 00:00 ... WeekDay
25/11/2018 20 Sunday
26/11/2018 30 Monday
27/11/2018 25 Tuesday
28/11/2018 35 Wednesday
29/11/2018 40 Thursday
30/11/2018 15 Friday
01/12/2018 Saturday
02/12/2018 65 Sunday
03/12/2018 34 Monday
04/12/2018 81 Tuesday
...
What I have tried:
df['Day'] = df['Date'].dt.day
df['Month'] = df['Date'].dt.month
df['Year'] = df['Date'].dt.year
requiredYear = str(df['Year'].median()).replace(".0","")
df = df.sort_values(by = ['Month', 'Day']).reset_index()
df['RemappedDate']= np.nan
for index, row in df.iterrows():
if row['Weekday'] != row['requiredWeekday']:
while row[row['Day']]<31:
row['Day'] = row['Day']-1
row['RemappedDate'] = pd.to_datetime(str(row['Month'])+"/"+
str(row['Day'])+"/"+requiredYear)
else:
print("Already equal")
df['Date'] = df['RemappedDate']
df['Weekday'] = df['requiredWeekday']
Probably nowhere near, so sorry if not. I'm a beginner.