[Python] Groupby column with relativedelta value

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe with a column of the dateutil.relativedelta type. When I try grouping by this column, I get the error TypeError: '<' not supported between instances of 'relativedelta' and 'relativedelta'. I didn't find anythind in the pandas page that indicated that there restrictions on the types of columns that can be used to group the dataframe. Am I missing something?

    Minimal example below:

    from dateutil.relativedelta import relativedelta
    import pandas as pd
    import itertools

    def expand_grid(data_dict):
    rows = itertools.product(*data_dict.values())
    return pd.DataFrame.from_records(rows, columns=data_dict.keys())

    ref_dates = pd.date_range(start="2024-06", end="2025-02", freq="MS").tolist()
    windows = [relativedelta(months=-30),relativedelta(months=-18)]
    max_horizon = [relativedelta(months=2)]
    params = expand_grid({'ref_date': ref_dates, 'max_horizon': max_horizon, 'window': windows})

    for name, group in params.groupby(by=['window']): print(name)

    Continue reading...

Compartilhe esta Página