Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is FrozenList different from tuple?

Tags:

python

pandas

from pandas.core.base import FrozenList

Type:        type
String form: <class 'pandas.core.base.FrozenList'>
File:        /site-packages/pandas/core/base.py
Docstring:
Container that doesn't allow setting item *but*
because it's technically non-hashable, will be used
for lookups, appropriately, etc.

Why not just use tuple? What extra functionality would a FrozenList offer?

like image 838
dmvianna Avatar asked Sep 04 '14 22:09

dmvianna


People also ask

What is the difference between frozen set and tuple?

tuples are immutable lists , frozensets are immutable sets . frozensets aren't indexed, but you have the functionality of sets - O(1) element lookups, and functionality such as unions and intersections. They also can't contain duplicates, like their mutable counterparts.

What is a FrozenList?

FrozenList is a list-like structure which implements collections. abc. MutableSequence. The list is mutable until FrozenList.

Do tuples have worse performance than lists?

Internally, tuples are stored a little more efficiently than lists, and also tuples can be accessed slightly faster.

What is the use of Frozenset in Python?

Python frozenset() Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.


1 Answers

This is an internal pandas construct. Not using tuple because:

  • It inherits from a common pandas class
  • Its customizable (e.g. the repr)
  • It doesn't have quite all of the functions of a tuple (some are disabled)
  • It nots hashable (so more like a list here and not a tuple)

The construct is used to represent a MultiIndex levels,labels, and names. The point of it is to prevent modification of these thru attributes and force the use of methods (e.g. set_levels()). As the state of these cannot be changed independent (for level/labels), but must be changed together.

These are 'public' properties though, so it needed an access mechanism that could do all of this (and yet still be changed internally if necessary, for performance reasons).

like image 137
Jeff Avatar answered Sep 29 '22 22:09

Jeff