※当サイトの記事には、広告・プロモーションが含まれます。

pandas.Panelクラス廃止されたけど、Pandasで3次元データを作成するには?

f:id:ts0818:20210123102652j:plain

japan.cnet.com

 Brave Softwareは米国時間1月19日、「Brave 1.19」をリリースしたことにより、IPFSをサポートする初の主要ブラウザーメーカーとなった。IPFSは、分散型コンテンツまたは検閲されたコンテンツにアクセスすることを目的とした、ピアツーピア(P2P)のプロトコルだ。

「Brave」、IPFSプロトコルをサポートする初の主要ブラウザーに - CNET Japan

httpsじゃない時代が来るのか~!

どうもボクです。

というわけで、今回は、Python拡張ライブラリのPandasについて調査してみました。

レッツトライ~。

 

Pandasは、1次元データはSeriesクラス、2次元データはDataFrameクラス、3次元データはPanelが受け持ってたらしい?

Python拡張ライブラリの「Pandas」ですが、

pandas.pydata.org

pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.

https://pandas.pydata.org/

⇧ ってな感じで、「データ分析および操作」に特化したライブラリですと。

で、自分は、最近、Pandasを学習したのですが、ネットの情報を見た限りでは、

  • pandas.Series
    1次元データを扱うクラス
  • pandas.DataFrame
    2次元データを扱うクラス
  • pandas.Panel
    3次元データを扱うクラス

 っていうようなイメージを持ったのですが、

towardsdatascience.com

⇧ 上記サイト様を参考に、「Pandas」の「Cheat Sheet」を確認したところ、

⇧ 2次元のデータまでしか言及が無い...

というか、本家の「Cheet Sheet」に至っては、データ構造については何も説明は無い...

Tidy data complements pandas’s vectorized operations. pandas will automatically preserve observations as you manipulate variables. No other format works as intuitively with pandas.

https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf

⇧ 何か、きちんとしたデータであればPandasの機能がバッチリ処理します、ていうことらしいけど、その「きちんとしたデータ」ってのが、どうも2次元データ限定っぽいような書きっぷりなんですよね。

ちなみに、この「Cheet Sheet」については、

This cheat sheet inspired by Rstudio Data Wrangling Cheatsheet (https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf) Written by Irv Lustig, Princeton Consultants

https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf

⇧ 「R Studio」の「Cheet Sheet」に感化されてます、ということみたいね。

まぁ、元々、2次元までのデータしか想定していなかったてことなのかね? 

 

 

pandas.Panelが非推奨になったって...、っていうか廃止されたって

で、 

pandas.pydata.org

Deprecate Panel

Panel was deprecated in the 0.20.x release, showing as a DeprecationWarning. Using Panel will now show a FutureWarning. The recommended way to represent 3-D data are with a MultiIndex on a DataFrame via the to_frame() or with the xarray package. pandas provides a to_xarray() method to automate this conversion (GH13563GH18324).

https://pandas.pydata.org/docs/whatsnew/v0.23.0.html?highlight=panel#deprecate-panel

⇧ バージョン0.23.0 から、Pandas.Panelクラスは非推奨になったのだと...

で、上記の説明のexampleっぽいソースコードを見てみると、Pandasで3次元のデータを作る方法としては、

  • pandas.Panel.to_frame()メソッドで何とかする
  • xarray packageをインストールして何とかする
  • pandas.Panel.to_xarrayメソッドで何とかする

ってことみたいなんですが、そもそもとして、pandas.Panel クラスが非推奨で使わないでって言ってるのに、pandas.Panel クラスのメソッドを解決策として提示してくる大たわけ者っぷりは何なんだろうか...

誰も、ここに疑問を持たないのだろうか...

そもそも、pandas.Panelクラスで作った3次元データありきで話を進めてる時点で、pandas.Panel クラスからの脱却ができなくない?っていうツッコミどころ満載のドキュメントに絶望しか感じません(涙)。

失礼いたしました。続報のリリースノートで Pandas バージョン0.25.0で、

pandas.pydata.org

What’s new in 0.25.0 (July 18, 2019)

Warning

 

Panel has been fully removed. For N-D labeled data structures, please use xarray

https://pandas.pydata.org/pandas-docs/version/0.25.3/whatsnew/v0.25.0.html

⇧ pandas.Panelクラスが完全に削除されたって…

つまり、3次元以上のデータはPandasでは手に負えませんわ、xarray packageに丸投げするわ、ってことみたいね...

っていうか、だったら、Pandasで代替できるものが準備できるまでpandas.Panel を削除しなくても良かったのでは?

意図が見えない...

Pandasの開発者が、考えるの面倒くさくなっちゃったのかね...

まぁ、人間だれしもが、何もやる気にならないことってあるあるなので、何も言えませんが...

 

MultiIndexって何?

サラッと当たり前のような感じで、「The recommended way to represent 3-D data are with a MultiIndex on a DataFrame」って言ってたけど、「MultiIndex」って何ですか?

おそらく、「Pandas」の概念かと。

pandas.pydata.org

The MultiIndex object is the hierarchical analogue of the standard Index object which typically stores the axis labels in pandas objects. You can think of MultiIndex as an array of tuples where each tuple is unique. A MultiIndex can be created from a list of arrays (using MultiIndex.from_arrays()), an array of tuples (using MultiIndex.from_tuples()), a crossed set of iterables (using MultiIndex.from_product()), or a DataFrame (using MultiIndex.from_frame()). The Index constructor will attempt to return a MultiIndex when it is passed a list of tuples. The following examples demonstrate different ways to initialize MultiIndexes.

https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html#creating-a-multiindex-hierarchical-index-object

⇧ 階層的なインデックスっていうものらしいんだけど、What’s?

イメージ的には、

deepage.net

⇧ 上記サイト様のような感じで、インデックスを複数設定できるってことみたい。

SQLなんかの複合主キーのイメージに近いのかな?

 

Pandasで3次元データを作るには?

で、結局のところ、Pandasで3次元データを作るにはどうすれば?

stackoverflow.com

As @Aaron mentioned in a comment above, panels have been deprecated. Also, @tlnagy mentioned his dataset would be likely to expand to more than 3 dimensions in the future.

This sounds like a good use-case for the xarray package, which provides semantically labelled arrays of arbitrarily many dimensions. Pandas and xarray have strong conversion support, and panels have been deprecated in favour of using xarray.

https://stackoverflow.com/questions/24290495/constructing-3d-pandas-dataframe

⇧ xarray package を使えば良いですと。

将来的に、pandas.DataFrameクラスが拡張されて多次元対応するかもってような話も出てるって言ってますね。

まぁ、何て言うか、xarray package使った例を見る限り、戻り値が「xarray.DataArray」ってオブジェクトになっていて、Pandasクラスのオブジェクトじゃなくなるんだけど、

xarray.pydata.org

N-dimensional array with labeled coordinates and dimensions.

DataArray provides a wrapper around numpy ndarrays that uses labeled dimensions and coordinates to support metadata aware operations. The API is similar to that for the pandas Series or DataFrame, but DataArray objects can have any number of dimensions, and their contents have fixed data types.

http://xarray.pydata.org/en/stable/generated/xarray.DataArray.html#xarray-dataarray

⇧ Numpyのndarrayのラッパーで、PandasのDataFrameに似てるけど2次元に限定されない、ってことを謳っておりますね。

 

結論

2021年1月23日(土)現在、「pandas.Panel」が非推奨(最新版のAPI Refernceを確認したところ、pandas.Panelクラスが無かったことにされてるので、削除された?)になってるにも関わらず、未だPandasで3次元データを作る方法は存在しない、たぶん。

つまり、Pandasライブラリで、3次元データを作ることはできません、たぶん。

xarray packageをインポートして、xarray.DataArrayクラスのオブジェクトを作ることで、3次元のデータは作成できるけれど、最早、Pandasクラスのオブジェクトでは無くなる。

まぁ、何が言いたいかというと、pandas.Panelが削除されたようなんですが、代替案が準備できてから削除するようにして欲しいですかね...

もしくは、ちゃんと代替案のexampleを載せるとかして欲しいかな...

Pandasのバージョンアップとかも躊躇しちゃうよね...

そもそも、実際のデータ分析なんかの業務では、3次元データを扱うことがあるのか分かりませんが。

今回はこのへんで。