dtoolkit.accessor.index.len#

dtoolkit.accessor.index.len(index: Index, /, number: int = 1, other: int = None) Index[source]#

Return the length of each element in the Index.

Equals to index.map(len), but the length of number type will as 1, the length of other types will as NaN.

Parameters
numberint, default 1

The default length of number type.

otherint or None, default None

The default length of other type.

Returns
Index(int64)

Notes

  • To keep the Python naming style, so use this accessor via Index.len rather than Index.lens.

  • Different to pandas.Index.str.len(). It only returns collections.abc.Iterable type length. Other type will return NaN.

Examples

>>> import dtoolkit
>>> import pandas as pd
>>> index = pd.Index([0, 1.5, "str", ("tuple",), ["list"], {}, object])
>>> index
Index([0, 1.5, 'str', ('tuple',), ['list'], {}, <class 'object'>], dtype='object')
>>> index.len()
Float64Index([1.0, 1.0, 3.0, 1.0, 1.0, 0.0, nan], dtype='float64')

Set number and other default return.

>>> index.len(number=0, other=0)
Int64Index([0, 0, 3, 1, 1, 0, 0], dtype='int64')