This question has been solved with R, but I haven't seen useful examples with Python. I would like to learn how to convert calendar year (1/1/1990 to 12/31/2010) discharge data to water year data (i.e. 10/01/1990 to 9/31/2010). Thank you for the assistance.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You could use apply and write your own function to create a new column WY
:
IF you have have df
:
Date Discharge
0 2011-10-01 00:00:00 0.0
1 2011-10-01 01:00:00 0.0
2 2011-10-01 02:00:00 0.0
3 2011-10-01 03:00:00 0.0
4 2011-10-01 04:00:00 0.0
Then:
import pandas as pd
def assign_wy(row):
if row.Date.month>=10:
return(pd.datetime(row.Date.year+1,1,1).year)
else:
return(pd.datetime(row.Date.year,1,1).year)
df['WY'] = df.apply(lambda x: assign_wy(x), axis=1)