如何将大 pandas 的日历年转换为水年 [英] How to convert calendar year to water year in pandas

查看:45
本文介绍了如何将大 pandas 的日历年转换为水年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经用 R 解决了,但是我还没有看到有用的 Python 例子.我想学习如何将日历年(1/1/1990 至 12/31/2010)排放数据转换为水年数据(即 10/01/1990 至 9/31/2010).感谢您的帮助.

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.

推荐答案

你可以使用 应用并编写自己的函数来创建一个新列WY:

You could use apply and write your own function to create a new column WY:

如果你有 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

那么:

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)

这篇关于如何将大 pandas 的日历年转换为水年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆