I have a dataframe containing a date column and amount column. Date column consists of hour and date information. For each day there are 24 entries for each hour of the day. I need to create a new column that shows the amount that belongs to 24 hours prior to the date and time for that row.
For example for "2019-11-06 18:00:00", the new column should show the amount info for "2019-11-05 18:00:00". The problem is what we should do with the first entry since it has no prior dates. I'm thinking I can remove the first entry when I create the new column but for now I'm getting KeyError since the first entry does not have a prior date. How to go around the KeyError?
What the dataframe looks like: screenshot of dataframe
[in]:
hours24_c = df["Date"]-timedelta(hours=24)
df["hours24"] = df["amount"].loc[hours24_c]
[out]:
KeyError: "None of [DatetimeIndex(['2015-12-30 00:00:00', '2015-12-30 01:00:00',\n '2015-12-30 02:00:00', '2015-12-30 03:00:00',\n '2015-12-30 04:00:00', '2015-12-30 05:00:00',\n '2015-12-30 06:00:00', '2015-12-30 07:00:00',\n '2015-12-30 08:00:00', '2015-12-30 09:00:00',\n ...\n '2019-11-05 14:00:00', '2019-11-05 15:00:00',\n '2019-11-05 16:00:00', '2019-11-05 17:00:00',\n '2019-11-05 18:00:00', '2019-11-05 19:00:00',\n '2019-11-05 20:00:00', '2019-11-05 21:00:00',\n '2019-11-05 22:00:00', '2019-11-05 23:00:00'],\n dtype='datetime64[ns]', length=33744, freq=None)] are in the [index]"