0

I can't seem to index rows using datetime index with pandas. Information on my dataframe shows that the index is datetimeindex:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 223 entries, 2013-10-29 to 2017-05-29
Data columns (total 6 columns):
Unnamed: 0    223 non-null float64
company       223 non-null object
date          223 non-null object
date_conv     223 non-null object
text          223 non-null object
title         223 non-null object
dtypes: float64(1), object(5)
memory usage: 17.2+ KB

But when I do this it returns 'key error'

df['2017-02-04']

Should I have index series name as "index" to make this work? Although the my df is using datetimeindex, the column name of the index is not 'index' it's 'date_conv'.

2
  • 2
    use df.loc['2017-02-04'] Commented Mar 28, 2019 at 19:09
  • @run-out Ok, that worked! thank you Commented Mar 28, 2019 at 19:09

2 Answers 2

1

In your example '2017-02-04' is string. You have to refer to the row by datetime:

df.loc[datetime.datetime.strptime('2017-2-4', '%Y-%m-%d'),:]
Sign up to request clarification or add additional context in comments.

Comments

0

You CAN use a string to address a row in a DataFrame, but you need to do it using the loc property:

df_row = df.loc['2017-02-04']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.