Python Forum
Iterate through dataframe to extract delta of a particular time period
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate through dataframe to extract delta of a particular time period
#1
I have a file, df, that I wish to take the delta of every 7 day period and reflect the timestamp for that particular period

df:

Date Value
10/15/2020 75
10/14/2020 70
10/13/2020 65
10/12/2020 60
10/11/2020 55
10/10/2020 50
10/9/2020 45
10/8/2020 40
10/7/2020 35
10/6/2020 30
10/5/2020 25
10/4/2020 20
10/3/2020 15
10/2/2020 10
10/1/2020 5


**Desired Output:**






Date Value

10/9/2020 30
10/2/2020 30




10/15/2020 to 10/9/2020 is 7 days with the delta being: 75 - 45 = 30
10/9/2020 timestamp would be: 30 and so on




This is what I am doing:


for row in df.itertuples():
callDate = datetime.strptime(row.Date, "%m/%d/%y %I:%M %p")
previousRecord = df['Date'].shift(-6).strptime(row.Date, "%m/%d/%y %I:%M %p")
Delta = Date - previousRecord



I am thinking I may be able to do this as well:


df=df.assign(Delta=df.Sun.sub(df.Monday),Date=pd.Series\ (pd.date_range(pd.Timestamp('2020-01-01'), periods=7, freq='7d')))[
[['Delta','Date']]

Any suggestion is appreciated
Reply
#2
Since rows corresponds days you can just shift the data frame on 6 positions and compute difference between shifted and original ones:

(df.shift(6).iloc[:, 1] - df.iloc[:,1])[df.index % 6 == 0].dropna()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Identify anomalies in a payment detail dataset for a period klllmmm 1 530 Aug-05-2021, 09:41 AM
Last Post: Larz60+
  Extract the categorial variables of a DataFrame Tut 1 1,134 Sep-20-2020, 09:21 AM
Last Post: scidam
  Simple String to Time within a pandas dataframe Ecniv 1 1,446 Jun-14-2019, 03:25 AM
Last Post: scidam
  splitting time (h,m,s) from dataframe column dedaelfl 2 3,150 Oct-09-2018, 12:28 PM
Last Post: volcano63
  How to extract only time from the date_time? Jack_Sparrow 1 2,555 May-11-2018, 01:42 PM
Last Post: Larz60+
  extract specific content in a pandas dataframe with a regex? steve1040 0 11,705 Oct-05-2017, 03:17 AM
Last Post: steve1040
  is a pandas dataframe timeseries time index in a specified range (but ignoring date)? m_lotinga 4 16,540 Dec-12-2016, 10:51 PM
Last Post: m_lotinga

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020