パンダースになるSeriesにハイブリッドタイプindexがある場合のサブセット取りエラーの処理

672 ワード

問題の説明
pandasを作成するとSeriesオブジェクトを設定し、indexに数値型と文字列型が含まれるように設定すると、後続のサブセット取得操作がエラーになる場合があります.
サンプルコード
se = pd.Series([1,2], index=[123456789,'1234569'])
se[[123456789,'1234569']] #   
se[['1234569']] #       index   
se[[123456789]] #      index   。  :IndexError: index 123456789 is out of bounds for axis 1 with size 2

解決策
Seriesオブジェクトのindexのデータ型が特定できない場合、またはindexに混合型がある場合に用いる.ixはSeriesオブジェクトのサブセットを取ります.
se = pd.Series([1,2], index=[123456789,'1234569'])
se.ix[[123456789,'1234569']] #   
se.ix[['1234569']] #       index   
se.ix[[123456789]] #      index