【问题标题】:Plotting Monthly data using groupby in dask dataset在 dask 数据集中使用 groupby 绘制每月数据
【发布时间】:2020-11-29 17:39:09
【问题描述】:

我有一个大的CSV 文件,它是用 Dask 打开的。

import numpy as np
import pandas as pd
import hvplot.pandas
import hvplot.dask
import intake

data = '../file.csv'
ddf = intake.open_csv(data).to_dask()
ddf.head()

Datetime    latitude    longitude   Temp_2m(C)  
1   1980-01-02 03:00:00     30.605  50.217  5.31
2   1980-01-02 04:00:00     30.605  50.217  5.36
3   1980-01-02 05:00:00     30.605  50.217  7.04
4   1980-01-02 06:00:00     30.605  50.217  10.24

我想用 hvplot 每月绘制Temp_2m(C)。使用Datetime 的每小时数据绘制正确完成,但是当我想将Datetime 分组如下时,它返回错误。

# Convert 'Datetime' column to 'datetime64'
ddf["Datetime"] = ddf["Datetime"].astype("M8[us]")

# set index column
ddf = ddf.set_index('Datetime')

g = pd.Grouper(freq='M', key='Datetime')
month_ddf = dff.groupby(g).mean()

# plot
month_ddf.hvplot('Temp_2m(C)')

错误: ValueError: all keys need to be the same shape 我的错误是什么?

回复@frankr6591:

month_ddf.describe()
Dask DataFrame Structure:
    latitude    longitude   Temp_2m(C)
npartitions=1                                           
    float64     float64     float64
    ...     ...     ... 
Dask Name: describe-numeric, 89 tasks

【问题讨论】:

  • 请不要分享截图。他们很难帮助你。而是共享一些数据。
  • @Serge de Gosson de Varennes,感谢您的评论,我已编辑。
  • 你做过month_ddf.describe()来看看它的形状吗?重新查看 ERROR 后,似乎 month_ddf 不统一。
  • @frankr6591,我在问题中添加了month_ddf.describe()
  • @HMadadi,请使用 print(month_ddf.describe()) 以便我们可以看到整个表格而不是“...”

标签: python pandas pandas-groupby dask hvplot


【解决方案1】:

我使用 to_datetime() 并使用 .plot() 获得了正确的绘图...在安装 hvplot 时遇到了问题。

import numpy as np
import pandas as pd
# FIXME : the following does not work
#import hvplot.pandas
%matplotlib inline

d = dict(datetime = ['1980-01-02 02:00:00',
                        '1980-01-02 03:00:00',
                        '1980-01-02 04:00:00',
                        '1980-01-02 05:00:00',
                        '1980-07-02 06:00:00'],
            latitude = [30.605 for n in range(5)],            
            longitude = [50.217 for n in range(5)],
            Temp_2m = [np.random.random()*10 for n in range(5)])
df = pd.DataFrame(d)

df['datetime'] = pd.to_datetime(df['datetime'])
df['mon'] = df['datetime'].dt.to_period('M')
print(df)

ddf = df.groupby('mon').mean()
print(ddf)

# This works on my py3.7
ddf.plot('Temp_2m')

# This fails because hvplot could not be imported. 
ddf.hvplot('Temp_2m')

             datetime  latitude  longitude   Temp_2m      mon
0 1980-01-02 02:00:00    30.605     50.217  2.512897  1980-01
1 1980-01-02 03:00:00    30.605     50.217  0.247358  1980-01
2 1980-01-02 04:00:00    30.605     50.217  7.678030  1980-01
3 1980-01-02 05:00:00    30.605     50.217  0.637331  1980-01
4 1980-07-02 06:00:00    30.605     50.217  2.156502  1980-07


         latitude  longitude   Temp_2m
mon                                   
1980-01    30.605     50.217  5.080373
1980-07    30.605     50.217  1.324140

【讨论】:

  • 谢谢你的回答,因为我用dask打开我的csv文件,用你的解决方案,它返回AttributeError: 'DataFrame' object has no attribute 'dt' 错误。
  • 将我的代码中的 'dt' 更改为 'datetime'... .
  • 你的答案是正确的,但我想用 dask 来接受它。我有非常大的 csv 文件,我必须用 dask 打开它。我赞成你的回答。
猜你喜欢
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-06
  • 1970-01-01
相关资源
最近更新 更多