87892


# 実行環境

  * Python 3.10.2
  * pandas 1.4.1

# やりたいこと

以下の`pandas.DataFrame`に対して、`user`ごとに`count`を1週間単位で集計したいです。

[code]

    In [1]: df=pandas.DataFrame({"count":[1,2,3], "working_hours":[6,7,8], "user":["alice","alice","bob"],
         ...: "date": pandas.date_range('2022-03-13', periods=3, freq='D')})
    
    
    
    In [2]: df
    Out[2]: 
       count  working_hours   user       date
    0      1              6  alice 2022-03-13
    1      2              7  alice 2022-03-14
    2      3              8    bob 2022-03-15
    
    
[/code]

# エラー発生

以下のコードを実行したら、`KeyError: 'The grouper name date is not found'`が発生しました。

[code]

    In [2]: df.groupby("user").resample("W",on="date",label="left",closed="left")[["count"]].sum()
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    Input In [137], in <cell line: 1>()
    ----> 1 df.groupby("user").resample("W",on="date",label="left",closed="left")[["count"]].sum()
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/resample.py:1028, in f(self, _method, min_count, *args, **kwargs)
       1026 def f(self, _method=method, min_count=0, *args, **kwargs):
       1027     nv.validate_resampler_func(_method, args, kwargs)
    -> 1028     return self._downsample(_method, min_count=min_count)
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/resample.py:1097, in _GroupByMixin._apply(self, f, *args, **kwargs)
       1093         return getattr(x, f)(**kwargs)
       1095     return x.apply(f, *args, **kwargs)
    -> 1097 result = self._groupby.apply(func)
       1098 return self._wrap_result(result)
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/groupby/groupby.py:1414, in GroupBy.apply(self, func, *args, **kwargs)
       1412 with option_context("mode.chained_assignment", None):
       1413     try:
    -> 1414         result = self._python_apply_general(f, self._selected_obj)
       1415     except TypeError:
       1416         # gh-20949
       1417         # try again, with .apply acting as a filtering
       (...)
       1421         # fails on *some* columns, e.g. a numeric operation
       1422         # on a string grouper column
       1424         with self._group_selection_context():
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/groupby/groupby.py:1455, in GroupBy._python_apply_general(self, f, data, not_indexed_same)
       1429 @final
       1430 def _python_apply_general(
       1431     self,
       (...)
       1434     not_indexed_same: bool | None = None,
       1435 ) -> DataFrame | Series:
       1436     """
       1437     Apply function f in python space
       1438 
       (...)
       1453         data after applying f
       1454     """
    -> 1455     values, mutated = self.grouper.apply(f, data, self.axis)
       1457     if not_indexed_same is None:
       1458         not_indexed_same = mutated or self.mutated
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/groupby/ops.py:761, in BaseGrouper.apply(self, f, data, axis)
        759 # group might be modified
        760 group_axes = group.axes
    --> 761 res = f(group)
        762 if not mutated and not _is_indexed_like(res, group_axes, axis):
        763     mutated = True
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/resample.py:1090, in _GroupByMixin._apply.<locals>.func(x)
       1089 def func(x):
    -> 1090     x = self._shallow_copy(x, groupby=self.groupby)
       1092     if isinstance(f, str):
       1093         return getattr(x, f)(**kwargs)
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/resample.py:178, in Resampler._shallow_copy(self, obj, **kwargs)
        176     if attr not in kwargs:
        177         kwargs[attr] = getattr(self, attr)
    --> 178 return self._constructor(obj, **kwargs)
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/resample.py:164, in Resampler.__init__(self, obj, groupby, axis, kind, selection, **kwargs)
        161 self.group_keys = True
        162 self.as_index = True
    --> 164 self.groupby._set_grouper(self._convert_obj(obj), sort=True)
        165 self.binner, self.grouper = self._get_binner()
        166 self._selection = selection
    
    File ~/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pandas/core/groupby/grouper.py:384, in Grouper._set_grouper(self, obj, sort)
        382     else:
        383         if key not in obj._info_axis:
    --> 384             raise KeyError(f"The grouper name {key} is not found")
        385         ax = Index(obj[key], name=key)
        387 else:
    
    KeyError: 'The grouper name date is not found'
    
    
[/code]

`[["count"]]`を`sum()`の後に指定すれば、欲しい結果を得ることができました。

[code]

    In [148]: df.groupby("user").resample("W",on="date",label="left",closed="left").sum()[["count"]]
    Out[148]: 
                      count
    user  date             
    alice 2022-03-13      3
    bob   2022-03-13      3
    
[/code]

# 質問

上記のエラーは何が原因でしょうか？エラーメッセージの意味が分かりませんでした。

以下のように、`groupby`関数を使わない場合は`resample`関数の後に`[["count"]]`を指定できるます。`groupby`関数を使った場合も、同様のことができると思っていました。

[code]

    In [140]: df.resample("W",on="date",label="left",closed="left")[["count"]].sum()
    Out[140]: 
                count
    date             
    2022-03-13      6
    
    
[/code]

# 補足

`groupby`関数を使うかどうかで、`resample`関数の結果の型が異なるようです。これが関係するのでしょうか？

[code]

    In [141]: df.resample("W",on="date",label="left",closed="left")
    Out[141]: <pandas.core.resample.DatetimeIndexResampler object at 0x7fdf73ae6860>
    
    In [142]: df.groupby("user").resample("W",on="date",label="left",closed="left")
    Out[142]: <pandas.core.resample.DatetimeIndexResamplerGroupby object at 0x7fdf7180fa00>
    
    
[/code]


CC BY-SA 4.0
2022-03-14T03:21:45.877
86824
2022-07-12T01:17:02.843
2022-07-11T09:36:11.897
19524
19524
question
python pandas
`df.groupby("user").resample("W",on="date",label="left",closed="left")[["count"]].sum()`を実行したときに`KeyError`が発生する原因を教えてください。


date列が存在しないというエラーです。単純に`.resample()`を使ったときは発生せず、`groupby().resample()`でかつ`on`キーワードで日付列を指定した場合のみ発生するようですね。

`[[]]`内に`"date"`を追加するとパスできました。

[code]

    df.groupby("user").resample("W", on="date", label="left", closed="left")[["date", "count"]].sum()
    
[/code]


CC BY-SA 4.0
2022-03-18T04:55:27.677
87892
2022-07-12T01:17:02.843
2022-07-12T01:17:02.843
37167
37167
86824
answer


単純にgroupby関数でuserだけ集約している=dateを捨てているからでは…


CC BY-SA 4.0
2022-07-11T13:48:17.517
89916
2022-07-11T13:48:17.517
53286
86824
answer