Pandas get from pandas.core.frame.Pandas object

use the __dict__ attribute.

Consider the following example

attr_name = 'ABCIndex'
pd.core.frame.algos.__dict__[attr_name]

pandas.types.generic.ABCIndex

Another solution I stumbled across is

pandas_object.__getattribute__(attr_name)

Seems to be more natural/intuitive than the

pandas_object.__dict__[attr_name]

Unfortunately, as opposed to .get this method allows not to set a default value


The Pandas class you mention is actually a dynamically created named tuple class (see here). By default the class is named "Pandas" but you can actually name it whatever you want like so:

my_tuples = list(df.itertuples(name='Whatever'))
assert isinstance(my_tuples[0], tuple)
assert my_tuples[0].__class__.__name__ == 'Whatever'

As you can see from the official documentation for named tuples, you can convert them to dictionaries like so:

some_object = some_namedtuple._asdict()
val = some_object.get(attr_name)

But a more Pythonic way of retrieving an attribute would be to just simply use the built-in getattr function which also works with any other Python object:

val = getattr(some_namedtuple, attr_name)

Tags:

Pandas