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] Indexing a pandas dataframe with datetime.date index leads to KeyError

Discussão em 'Python' iniciado por Stack, Novembro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I retrieve some data from my MySQL database. This data has the date (not datetime) in one column and some other random data in the other columns. Let's say dtf is my dataframe. There is no index yet so I set one

    dtf.set_index('date', inplace=True)


    Now I would like to get data from a specific date, so I write for example:

    dtf.loc['2000-01-03']


    or just

    dtf['2000-01-03']


    This gives me a KeyError:

    KeyError: '2000-01-03'


    But I know it's in there; dtf.head() shows me that.
    So I took a look at the type of the index of the first row:

    type(dtf.index[0])


    and it tells me: datetime.date. All good. Now if I just type

    dtf.index


    the output is

    Index([2000-01-03, 2000-01-04, 2000-01-05, 2000-01-06, 2000-01-07, 2000-01-10,
    2000-01-11, 2000-01-12, 2000-01-13, 2000-01-14,
    ...
    2015-09-09, 2015-09-10, 2015-09-11, 2015-09-14, 2015-09-15, 2015-09-16,
    2015-09-17, 2015-09-18, 2015-09-21, 2015-09-22],
    dtype='object', name='date', length=2763)


    I am a bit confused about the dtype='object'. Shouldn't this read datetime.date?

    If I use datetime in my mysql table instead of date everything works like a charm. Is this a bug or a feature? I really would like to use datetime.date because it describes my data best.

    My pandas version is 0.17.0
    I am using python 3.5.0
    My os is arch linux

    Continue reading...

Compartilhe esta Página