Luc*_*des 2 grouping python-3.x python-xarray
我正在处理GLDAS. 时期的数据重新分析1 year。这些文件是.nc4. 我可以打开文件,但我不能groupby给出1 month。我不想手动或通过 for,但我发现确实xarray如此groupby。我的脚本是:
In[16]:import xarray as xr\n\nIn[17]:gldas = xr.open_mfdataset('./GLDAS_2010/*.nc4', chunks=None, concat_dim='time', preprocess=None, engine='netcdf4', lock=None,compat='minimal',coords='minimal',data_vars='minimal')\n\nIn[18]: gldas\nOut[18]: \n<xarray.Dataset>\nDimensions: (bnds: 2, lat: 40, lon: 48, time: 365)\nCoordinates:\n * lat (lat) float32 -34.875 -34.625 -34.375 -34.125 ...\n * lon (lon) float32 -59.875 -59.625 -59.375 -59.125 ...\n * time (time) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...\nDimensions without coordinates: bnds\nData variables:\n Albedo_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n AvgSurfT_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n CanopInt_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n ECanop_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n ESoil_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Evap_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n LWdown_f_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Lwnet_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n PotEvap_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Psurf_f_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Qair_f_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Qg_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Qh_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Qle_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Qs_acc (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Qsb_acc (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Qsm_acc (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Rainf_f_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Rainf_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n RootMoist_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SWE_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SWdown_f_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SnowDepth_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Snowf_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SoilMoi0_10cm_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SoilMoi100_200cm_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SoilMoi40_100cm_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SoilTMP0_10cm_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SoilTMP100_200cm_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SoilTMP10_40cm_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n SoilTMP40_100cm_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Swnet_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Tair_f_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Tveg_tavg (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n Wind_f_inst (time, lat, lon) float32 dask.array<shape=(365, 40, 48), chunksize=(1, 40, 48)>\n time_bnds (time, bnds) float64 dask.array<shape=(365, 2), chunksize=(1, 2)>\nAttributes:\n CDI: Climate Data Interface version 1.6.9 (http://mpim...\n Conventions: CF-1.4\n history: Mon Feb 5 20:07:25 2018: /usr/bin/ncks -O -L 1 /...\n source: Noah_v3.3\n institution: NASA GSFC\n missing_value: -9999.0\n tavg definision:: past 3-hour average\n acc definision:: past 3-hour accumulation\n inst definision:: instantaneous\n title: GLDAS2.1 LIS land surface model output\n references: Rodell_etal_BAMS_2004, Kumar_etal_EMS_2006, Peter...\n conventions: CF-1.6\n comment: website: http://ldas.gsfc.nasa.gov/gldas, http://...\n MAP_PROJECTION: EQUIDISTANT CYLINDRICAL\n SOUTH_WEST_CORNER_LAT: -59.875\n SOUTH_WEST_CORNER_LON: -179.875\n DX: 0.25\n DY: 0.25\n CDO: Climate Data Operators version 1.6.9 (http://mpim...\n NCO: 20180205\nRun Code Online (Sandbox Code Playgroud)\n\n当我尝试时:
\n\nIn[19]:pp.set_index('time').groupby(freq='1M').mean('time')\nRun Code Online (Sandbox Code Playgroud)\n\n给出这个错误:
\n\nTypeError: groupby() got an unexpected keyword argument 'freq'\nRun Code Online (Sandbox Code Playgroud)\n\n好吧,知道有这个论点,我将进行下一次尝试,即:
\n\nIn[20]:import pandas as pd\nIn[21]:pp.groupby(['Albedo_inst',pd.Grouper(key='\xe2\x80\x8c\xe2\x80\x8btime', freq='M')]).mean()\nRun Code Online (Sandbox Code Playgroud)\n\n错误是:
\n\nTypeError: `group` must be an xarray.DataArray or the name of an xarray variable or dimension\nRun Code Online (Sandbox Code Playgroud)\n\n最后一次尝试没有频率:
\n\nIn[22]:pp.groupby('Albedo_inst').mean('time')\nRun Code Online (Sandbox Code Playgroud)\n\n和错误:
\n\nValueError: Level values must be unique: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0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playgroud)\n\n我尝试:
\n\ngldas.resample('1MS', dim='time', how='mean')\nRun Code Online (Sandbox Code Playgroud)\n\n错误是:
\n\nTypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Float64Index'\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试:
\n\ngldas.groupby('time.month').mean('time')\nRun Code Online (Sandbox Code Playgroud)\n\n错误是:
\n\nAttributeError: 'IndexVariable' object has no attribute 'month'\nRun Code Online (Sandbox Code Playgroud)\n\n有人会帮助我吗?多谢你们!
\n\n编辑:
\n\nIn [24]: pp['time']\nOut[24]: \n<xarray.DataArray 'time' (time: 365)>\narray([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0., 0., 0., 0., 0., 0.])\nCoordinates:\n * time (time) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...\nAttributes:\n standard_name: time\n bounds: time_bnds\n units: day as %Y%m%d.%f\n calendar: proleptic_gregorian\nRun Code Online (Sandbox Code Playgroud)\n\n编辑:
\n\n如果我打开一个文件:
\n\nIn[108]:gldas\nOut[108]: \n<xarray.Dataset>\nDimensions: (bnds: 2, lat: 40, lon: 48, time: 1)\nCoordinates:\n * lat (lat) float32 -34.875 -34.625 -34.375 -34.125 ...\n * lon (lon) float32 -59.875 -59.625 -59.375 -59.125 ...\n * time (time) float64 0.0\n * bnds (bnds) int64 0 1\nData variables:\n Albedo_inst (time, lat, lon) float64 15.66 16.0 16.23 15.98 ...\n AvgSurfT_inst (time, lat, lon) float64 302.8 302.3 302.3 302.4 ...\n CanopInt_inst (time, lat, lon) float64 0.001 0.0 0.0 0.0 0.0 ...\n ECanop_tavg (time, lat, lon) float64 2.48 0.89 0.16 0.0 0.02 ...\n ESoil_tavg (time, lat, lon) float64 46.39 46.58 49.18 55.22 ...\n Evap_tavg (time, lat, lon) float64 0.0001668 0.0001683 ...\n LWdown_f_tavg (time, lat, lon) float64 369.5 368.3 366.8 365.6 ...\n Lwnet_tavg (time, lat, lon) float64 -88.05 -87.24 -87.68 ...\n PotEvap_tavg (time, lat, lon) float64 622.3 610.4 601.1 599.6 ...\n Psurf_f_inst (time, lat, lon) float64 1.004e+05 1.006e+05 ...\n Qair_f_inst (time, lat, lon) float64 0.01117 0.01144 0.01172 ...\n Qg_tavg (time, lat, lon) float64 54.2 53.65 55.13 58.52 ...\n Qh_tavg (time, lat, lon) float64 110.4 101.9 104.5 114.7 ...\n Qle_tavg (time, lat, lon) float64 417.2 420.8 416.1 405.6 ...\n Qs_acc (time, lat, lon) float64 0.0 0.0 0.0 0.0 0.0 0.0 ...\n Qsb_acc (time, lat, lon) float64 0.00253 0.00335 0.00528 ...\n Qsm_acc (time, lat, lon) float64 0.0 0.0 0.0 0.0 0.0 0.0 ...\n Rainf_f_tavg (time, lat, lon) float64 1.4e-06 5e-07 1e-07 0.0 ...\n Rainf_tavg (time, lat, lon) float64 1.4e-06 5e-07 1e-07 0.0 ...\n RootMoist_inst (time, lat, lon) float64 255.3 259.6 263.9 266.1 ...\n SWE_inst (time, lat, lon) float64 0.0 0.0 0.0 0.0 0.0 0.0 ...\n SWdown_f_tavg (time, lat, lon) float64 793.9 789.7 791.5 794.6 ...\n SnowDepth_inst (time, lat, lon) float64 0.0 0.0 0.0 0.0 0.0 0.0 ...\n Snowf_tavg (time, lat, lon) float64 0.0 0.0 0.0 0.0 0.0 0.0 ...\n SoilMoi0_10cm_inst (time, lat, lon) float64 24.86 25.17 25.47 25.51 ...\n SoilMoi100_200cm_inst (time, lat, lon) float64 254.4 258.8 266.1 274.1 ...\n SoilMoi40_100cm_inst (time, lat, lon) float64 153.7 156.7 159.8 161.6 ...\n SoilTMP0_10cm_inst (time, lat, lon) float64 293.8 293.5 293.4 293.7 ...\n SoilTMP100_200cm_inst (time, lat, lon) float64 291.3 291.0 290.9 290.9 ...\n SoilTMP10_40cm_inst (time, lat, lon) float64 292.4 292.2 292.1 292.2 ...\n SoilTMP40_100cm_inst (time, lat, lon) float64 292.2 292.0 291.8 291.9 ...\n Swnet_tavg (time, lat, lon) float64 669.5 663.3 663.0 667.7 ...\n Tair_f_inst (time, lat, lon) float64 300.2 300.0 299.8 299.5 ...\n Tveg_tavg (time, lat, lon) float64 368.3 373.3 366.8 350.4 ...\n Wind_f_inst (time, lat, lon) float64 3.704 3.704 3.704 3.704 ...\n time_bnds (time, bnds) float64 0.0 0.0\nAttributes:\n CDI: Climate Data Interface version 1.6.9 (http://mpimet.mpg.de/cdi)\n Conventions: CF-1.4\n history: Mon Feb 5 20:07:25 2018: /usr/bin/ncks -O -L 1 /tmpdata/regridder/services_88661/cdoGLDAS_NOAH025_3H.A20100101.1500.021.SUB.nc4 /tmpdata/regridder/services_88661/deflatecdoGLDAS_NOAH025_3H.A20100101.1500.021.SUB.nc4\nMon Feb 05 20:07:21 2018: cdo -s -L -f nc4 -sellonlatbox,-60.0,-48.0,-35.0,-25.0 -selname,Albedo_inst,AvgSurfT_inst,CanopInt_inst,ECanop_tavg,ESoil_tavg,Evap_tavg,LWdown_f_tavg,Lwnet_tavg,PotEvap_tavg,Psurf_f_inst,Qair_f_inst,Qg_tavg,Qh_tavg,Qle_tavg,Qs_acc,Qsb_acc,Qsm_acc,Rainf_...\n source: Noah_v3.3\n institution: NASA GSFC\n missing_value: -9999.0\n tavg definision:: past 3-hour average\n acc definision:: past 3-hour accumulation\n inst definision:: instantaneous\n title: GLDAS2.1 LIS land surface model output\n references: Rodell_etal_BAMS_2004, Kumar_etal_EMS_2006, Peters-Lidard_etal_ISSE_2007\n conventions: CF-1.6\n comment: website: http://ldas.gsfc.nasa.gov/gldas, http://lis.gsfc.nasa.gov/\n MAP_PROJECTION: EQUIDISTANT CYLINDRICAL\n SOUTH_WEST_CORNER_LAT: -59.875\n SOUTH_WEST_CORNER_LON: -179.875\n DX: 0.25\n DY: 0.25\n CDO: Climate Data Operators version 1.6.9 (http://mpimet.mpg.de/cdo)\n NCO: 20180205/usr/local/lib/python3.6/dist-packages/spyder/widgets/variableexplorer/utils.py:414: FutureWarning: 'summary' is deprecated and will be removed in a future version.\n display = value.summary()\nRun Code Online (Sandbox Code Playgroud)\n\n结论:\n我明白了。如果有人需要帮助,请给我留言。
\n小智 8
如果您没有有效的时间变量(上面示例中的时间始终为 0...),您必须修复该问题。但一旦完成,我认为您正在寻找的行是:
gldas['Albedo_inst'].resample(time="1MS").mean(dim="time")
| 归档时间: |
|
| 查看次数: |
9743 次 |
| 最近记录: |