[Python] If you add _metadata to a custom subclass of Series, the sequence name is lost when...

Discussão em 'Python' iniciado por Stack, Maio 24, 2025.

  1. Stack

    Stack Membro Participativo

    pandas 2.2.3

    Let’s consider two variants of defining a custom subtype of pandas.Series. In the first one, no custom properties are added, while in the second one, custom metadata is included:

    import pandas as pd


    class MySeries(pd.Series):

    @property
    def _constructor(self):
    return MySeries


    seq = MySeries([*'abc'], name='data')

    print(f'''Case without _metadata:
    {isinstance(seq[0:1], MySeries) = }
    {isinstance(seq[[0, 1]], MySeries) = }
    {seq[0:1].name = }
    {seq[[0, 1]].name = }
    ''')

    class MySeries(pd.Series):

    _metadata = ['property']

    @property
    def _constructor(self, property=None):
    return MySeries


    seq = MySeries([*'abc'], name='data')
    seq.property = 'MyProperty'

    print(f'''Case with defined _metadata:
    {isinstance(seq[0:1], MySeries) = }
    {isinstance(seq[[0, 1]], MySeries) = }
    {seq[0:1].name = }
    {seq[[0, 1]].name = }
    {getattr(seq[0:1], 'property', 'NA') = }
    {getattr(seq[[0, 1]], 'property', 'NA') = }
    ''')


    The output will be:

    Case without _metadata:
    isinstance(seq[0:1], MySeries) = True
    isinstance(seq[[0, 1]], MySeries) = True
    seq[0:1].name = 'data'
    seq[[0, 1]].name = 'data'

    Case with defined _metadata:
    isinstance(seq[0:1], MySeries) = True
    isinstance(seq[[0, 1]], MySeries) = True
    seq[0:1].name = 'data'
    seq[[0, 1]].name = None <<< Problematic result of indexing
    getattr(seq[0:1], 'property', 'NA') = 'MyProperty'
    getattr(seq[[0, 1]], 'property', 'NA') = 'MyProperty'


    So, if _metadata is defined, the sequence name is preserved when slicing, but lost when indexing with a list, whereas without _metadata the name is preserved in both cases.

    Question: What needs to be done to preserve the name of a custom Series subclass with added metadata when indexing?

    Continue reading...

Compartilhe esta Página