Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 熊猫如何将日历年转换为水年_Python 3.x_Pandas_Time Series_Pandas Groupby - Fatal编程技术网

Python 3.x 熊猫如何将日历年转换为水年

Python 3.x 熊猫如何将日历年转换为水年,python-3.x,pandas,time-series,pandas-groupby,Python 3.x,Pandas,Time Series,Pandas Groupby,这个问题已经用R解决了,但我还没有看到Python的有用示例。我想学习如何将日历年(1990年1月1日至2010年12月31日)排放数据转换为水年数据(即1990年10月1日至2010年9月31日)。感谢您的帮助。您可以使用并编写自己的函数来创建新列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

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

您可以使用并编写自己的函数来创建新列
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
然后:

  • 使用来自

    • 日期:2001-10-01-2017-09-31
  • 必须通过使用
    parse_dates
    导入数据,或在导入数据后使用
    pd.to_datetime()
    'datetime'
    列设置为
    datetime

  • 用于确定水年

    • 使用提取
      月份
      年份
      数字
    • 如果月数小于10,则水年为
      .dt.year
      ,否则,水年为
      .dt.year+1
    • 对于此数据帧中的
      282757
      行,使用此函数的
      .apply
      功能的速度比
      快13倍
将熊猫作为pd导入
#加载数据
df=pd.read\u csv('WabashRiver\u Flow.csv',parse\u dates=['datetime']))
#删除na值
df=df.dropna()
#确定水年
df['water_year']=df.datetime.dt.year.where(df.datetime.dt.month<10,df.datetime.dt.year+1)
#显示(df.head())
机构、现场、无日期、时间、排放量、水年
美国地质勘探局3335500 2001-10-01 00:00:00美国东部时间261002
美国地质勘探局3335500 2001-10-01 01:00:00美国东部时间2610 2002
美国地质勘探局3335500 2001-10-01 02:00:00美国东部时间2610 2002
美国地质勘探局3335500 2001-10-01 03:00:00美国东部时间2630 2002
美国地质勘探局3335500 2001-10-01 04:00:00美国东部时间2630 2002
按水年计算平均流量
年平均排放率=df.groupby('水年')[[['排放量]].mean()
#显示(年平均排放率)
出院
水年
2002           9379.829589
2003           8678.468324
2004           8562.505005
2005           8928.776256
2006           6710.805312
2007          10331.564789
2008          10626.336623
2009           8972.046607
2010           5298.569557
2011          10519.540869
2012           9013.624424
2013           9007.924205
2014           9079.561658
2015          12267.393776
2016           6445.875810
2017          10240.721464
年平均流量图条形图(figsize=(8,6),xlabel=‘水年’,ylabel=‘流量(立方英尺/秒)’,图例=False)


%%timeit
比较
  • 与比较,并从另一个答案的功能。
    • .Series.where
      是矢量化的,而
      .apply
      不是
将numpy导入为np
#功能来自其他答案;已更新,因为pd.datetime已弃用
def分配_wy(行):
如果行月份>=10:
返回(第1行第1年)
其他:
报税表(年度第行)
%%时间
df.datetime.dt.year.where(df.datetime.dt.month<10,df.datetime.dt.year+1)
[out]:
每个回路66.9 ms±1.67 ms(7次运行的平均值±标准偏差,每个10个回路)
%%时间
df.datetime.apply(λv:np.其中(v.month>=10,v.year+1,v.year))
[out]:
每个回路1.38 s±23 ms(7次运行的平均值±标准偏差,每个回路1次)
%%时间
df.datetime.apply(lambda x:assign_wy(x))
[out]:
每个回路861 ms±9.3 ms(7次运行的平均值±标准偏差,每个回路1次)

df['date']=df['date']+pd.DateOffset(月数=9)
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)