0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】【pandas】pd.DataFrame().Tはどうやって実装されているのか?

Last updated at Posted at 2020-06-20

ipythonでdf.T??を打ってヘルプを見てみる。

>>> df = pd.DataFrame()
>>> df.T??
Type:
property
String form: <property object at 0x7f1aa67d6540>
Source:
# df.T.fget
def transpose(self, *args, copy: bool = False) -> "DataFrame":
"""
Transpose index and columns.
(snip...)

df.Tdf.transpose()と同じらしい。
df.transpose??しても同じヘルプしか見れなかったので、ソースコードをgrepして頑張って見つけた。
以下のように組込関数propertyを使ってself.transepose()self.Tとして呼べるように登録してあるらしい。

Python Documentation 組み込み関数 property

/opt/miniconda3/lib/python3.8/site-packages/pandas/core/base.py
from pandas.compat.numpy import function as nv
(snip...)
class IndexOpsMixin:
(snip...)
    def transpose(self, *args, **kwargs):
        """
        Return the transpose, which is by definition self.

        Returns
        -------
        %(klass)s
        """
        nv.validate_transpose(args, kwargs)
        return self

    T = property(
        transpose,
        doc="""
        Return the transpose, which is by definition self.
        """,
    )
(snip...)

propertyの第一引数は属性値を取得するための関数です。
...うん、イマイチ自分の言葉に落とし込めないのですが、やりたいことはできたので、いつか理解できたらいいなと思っておきます。

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?