0
0

More than 1 year has passed since last update.

module 'pandas' has no attribute 'TimeGrouper'というエラーの原因を調べてみた。

Last updated at Posted at 2023-04-03

今回の疑問

とある書籍で勉強している際、pd.TimeGrouper()というものが出てきたが実行してみるとエラーに。
なので調べてみました。

import pandas as pd

tg = pd.TimeGrouper("10min")
#AttributeError: module 'pandas' has no attribute 'TimeGrouper'

調べたこと

まずはエラーコードを調べてみます。
AttributeError: module 'pandas' has no attribute 'TimeGrouper'
こちらの意味は「pandasにそんなもんねぇ(意訳)」だそうです。

次に公式さんを覗きます。が、TimeGrouperで調べてみてもGrouperばかりが検索に引っ掛かります。
https://pandas.pydata.org/docs/reference/api/pandas.Grouper.html

そこで以下の記事?質問掲示板?みたいなのを見つけました。
https://stackoverflow.com/questions/45156289/pandas-where-is-the-documentation-for-timegrouper

ここの掲示板によると

pd.TimeGrouper() was formally deprecated in pandas v0.21.0 in favor of pd.Grouper().

pd.TimeGrouper()はバージョン0.21.0で消えたから代わりにpd.Grouper()を使うといいよ。」みたいな感じだと思います。

さらに読み進めるとpd.Grouper(freq=)で代用ができるそうです。
よって先ほどのコードは

tg = pd.Grouper(freq="10min")
tg
#TimeGrouper(freq=<10 * Minutes>, axis=0, sort=True, closed='left', label='left', how='mean', convention='e', origin='start_day')

こう直すことで無事に解決しました。
pd.TimeGrouper()はなくなっても、ちゃっかりTimeGrouperオブジェクト(あってるかな?)なことにフフッとなりました。

まとめ

バージョンによって機能が消されたり、追加されたりと移り変わりが激しいこの業界。
まだまだ勉強することがたんまりだなぁと痛感することになりました。

いつかは公式ドキュメントと握手くらいできるような仲になれればと思いながら終わりたいと思います。

参考にさせていただいたページ
https://pandas.pydata.org/docs/reference/api/pandas.Grouper.html
https://stackoverflow.com/questions/45156289/pandas-where-is-the-documentation-for-timegrouper

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0