0

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]"

1 Answer 1

1

Sort by date and then use shift:

df.sort_values(by = "Date", inplace = True)
df["hours24"] = df["amount"].shift(24)
Sign up to request clarification or add additional context in comments.

10 Comments

what if I want to shift by 2 weeks? or 3 months?
change the shift number to 14*24 for two weeks. For 3-months is similar.
do you suggest any method for dealing with N/A values resulting from the shift?
what are you trying to do with the data?
Time Series Forecasting with linear regression
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.