reindex_like
Pandas シリーズ オブジェクトの使用時と関連機能の違いについて混乱しています。たとえば、次の Series オブジェクトについて考えてみます。
>>> import numpy
>>> import pandas
>>> series = pandas.Series([1, 2, 3])
>>> x = pandas.Series([True]).reindex_like(series).fillna(True)
>>> y = pandas.Series(True, index=series.index)
>>> x
0 True
1 True
2 True
>>> y
0 True
1 True
2 True
表面的にはx
とy
の内容と索引は同一に見えます。ただし、使用時にエラーが発生するものと発生しないものがあるため、何らかの方法で異なる必要がありますnumpy.logical_and()
。
>>> numpy.logical_and(series, y)
0 True
1 True
2 True
>>> numpy.logical_and(series, x)
Traceback (most recent call last):
File "<ipython-input-10-e2050a2015bf>", line 1, in <module>
numpy.logical_and(series, x)
AttributeError: logical_and
numpy.logical()
ここで文句を言っているのは何ですか?x
2 つのシリーズの違いがわかりませんy
。ただし、微妙な違いがあるはずです。
Pandas のドキュメントによると、Series オブジェクトは「ほとんどの NumPy 関数」に対する有効な引数です。明らかに、これはこの場合ある程度真実です。どうやら、作成メカニズムによりx
、この特定のnumpy関数が使用できなくなります。
補足として、このシナリオでは、2 つの作成メカニズムreindex_like()
とindex
引数のどちらがより効率的で慣用的ですか? たぶん、私も考えていない別の/より良い方法があります。