[Python] Pandas datetime index empty dataframe

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

  1. Stack

    Stack Membro Participativo

    I have the following code:

    data = pandas.read_csv('data.csv')
    data['when'] = pandas.to_datetime(data['when'])
    data.set_index('when', inplace=True)
    print(data)
    print(data.index.dtype)


    which prints:

    price
    when
    2025-01-04 98259.4300
    2025-01-03 98126.6400
    2025-01-02 96949.1800
    2025-01-01 94610.1400
    2024-12-31 93647.0100
    ... ...
    2010-07-21 0.0792
    2010-07-20 0.0747
    2010-07-19 0.0808
    2010-07-18 0.0858
    2010-07-17 0.0500

    [5286 rows x 1 columns]
    datetime64[ns]


    Then, I am trying to select a range like this:

    start_date = datetime(year=2010,month=1,day=1)
    end_date = datetime(year=2025,month=1,day=1)
    print(data.loc[start_date:end_date])
    print(data.loc[start_date:])
    print(data.loc[:end_date])


    and this prints

    Empty DataFrame
    Columns: [price]
    Index: []
    Empty DataFrame
    Columns: [price]
    Index: []
    price
    when
    2025-01-04 98259.43
    2025-01-03 98126.64
    2025-01-02 96949.18
    2025-01-01 94610.14


    Why?

    I am using pandas 2.2.3.

    Continue reading...

Compartilhe esta Página