[Python] Merge date with error - If using all scalar values, you must pass an index

Discussão em 'Python' iniciado por Stack, Julho 27, 2025.

  1. Stack

    Stack Membro Participativo

    Data like this:

    date,stockIndex
    2024-01-02,17853.76
    2024-01-03,17559.31
    2024-01-04,17549.65
    2024-01-05,17519.14
    2024-01-08,17572.66
    2024-01-09,17535.4
    2024-01-10,17465.63


    Python Code

    import pandas as pd
    df = pd.read_csv('D:\\Download\\pandas.csv', index_col='date')
    df.index = pd.to_datetime(df.index, format='%Y-%m-%d')
    df.index


    [​IMG]

    df_average = df.rolling(60).mean()
    df_average.index


    [​IMG]

    Trying to merge the data doing :

    df2 = pd.DataFrame({'price':df,'sma':df_average})


    produces the error :


    ValueError: If using all scalar values, you must pass an index
    ​

    With :

    df3 = pd.DataFrame({'price':df})


    still has the same problem. (ValueError: If using all scalar values, you must pass an index)

    and :

    df4 = pd.DataFrame({'price':df}, index='date')


    produces the error :


    TypeError: Index(...) must be called with a collection of some kind, 'date' was passed
    ​

    How to solve ?

    Continue reading...

Compartilhe esta Página