1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why my code isnt outputting the same type df if its identical to its other version

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    def position_size():
    for df_type, df in zip(["long", "short"], [df_long_df, df_short_df]):
    for col in ativos.columns:
    if df_type == "long":
    print(f"checking column: long_{col}")
    size_long = pd.DataFrame(
    np.where(df_long_df[f"long_{col}"], 1.0, 0),
    index = ativos.index,
    columns=[f"ativos_{df_type}_{col}_size"]
    )
    df_size_long.append(size_long)
    elif df_type == "short":
    print(f"checking columns: short_{col}")
    size_short = pd.DataFrame(
    np.where(df_short_df[f"short_{col}"], -1.0, 0),
    index=ativos.index,
    columns=[f"ativos_{df_type}_{col}_size"]
    )
    df_size_short.append(size_short)
    size_short_df = pd.concat(df_size_short, axis=1)
    print(size_short_df)
    size_long_df = pd.concat(df_size_long)
    print(size_long_df)


    So this is my code, the problem is exactly that the code for "df_size_short" and "df_size_long" are identical but only the df_size_short outputs rightly, df_size_long, kinda overwrite the positions sizes given by np.where(), I think the problem is somehow at iteration but i cant exactly figure out why, would appreciate some help, thanks in advance. Also the df_long_df and df_short_df are bollean dfs, no NaN in either. And df_size_long/short are empty lists.

    Continue reading...

Compartilhe esta Página