[Python] DataFrame with multiple types in same column

Discussão em 'Python' iniciado por Stack, Agosto 31, 2025.

  1. Stack

    Stack Membro Participativo

    I do have a DataFrame as shown below. Build it, I start with adding the numbers (100 for spec A Sum in 2020 and so on). Additionally I add the median as a date.

    2020 2021 ... 2024 2025
    spec A Sum 100 110 ... 188 190
    spec A median 2020-08-05 2021-08-06 ... 2024-08-01 2025-07-30
    spec B Sum 510 550 ... 501 750
    spec B median 2020-09-03 2021-09-05 ... 2024-09-04 2025-08-31


    Building the number:

    df_complex.loc['spec A Sum'] = len(df_h)


    Building the median:

    median = math.floor(df_h['datum'].astype('int64').median())
    result = np.datetime64(median, "ns")
    ts = pd.to_datetime(str(result))
    d = ts.strftime('%Y-%m-%d')
    df_complex.loc['spec A median'] = d


    Doing so, I get the following warning/error:

    FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '2020-07-28' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.


    That means, pandas' expecting float (due to float/int from first build?). But getting datetime as another dtype.

    Does anyone know, if there is a possibility to add a type to each value? Or how to handle this kind of problem?

    Continue reading...

Compartilhe esta Página