0

I have a time series as pandas dataframe, that have one column named "pre". The index of the dataframe is pandas date_range. The dates range from 1998-01-01 to 2002-12-31.

I want to get the sum of pre values from "1998-01-01" to "1998-05-31" plus "1998-09-01" to "1998-12-31". After getting this for the year 1998, how can I get the same for all the years?

1 Answer 1

2

Use dictionary comprehension with f-strings:

np.random.seed(2020)

r = pd.date_range('1998-01-01','2002-12-31')
s = pd.Series(np.random.randint(10, size=len(r)), index=r)
# print (s)

out = {y: s.loc[f"{y}-01-01":f"{y}-05-31"].sum() + s.loc[f"{y}-09-01":f"{y}-12-31"].sum() 
       for y in range(1998, 2003)}
print (out)
{1998: 1235, 1999: 1201, 2000: 1154, 2001: 1270, 2002: 1184}

Another solution is convert DatetimeIndex to months and remove 6,7,8 months and then grouping by years with aggregate sum:

s1 = s[~s.index.month.isin([6,7,8])]
print (s1.groupby(s1.index.year).sum())
1998    1235
1999    1201
2000    1154
2001    1270
2002    1184
dtype: int32
Sign up to request clarification or add additional context in comments.

3 Comments

Showing syntax error, invalid syntax after f"{y}-01-01"
@lsr729 - what is your python version? It working 3.5+
@lsr729 - Also added new solution, it working for any pandas versions, Ithink.

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.