2

I have the following data:

                High
 Date
 2017-07-17    150.90
 2017-07-18    150.13
 2017-07-19    151.42
 2017-07-20    151.74
 2017-07-21    150.44
 2017-07-24    152.44

I was trying to get the index by putting in value inside Highs.index(values) but am unable to get the index.

import datetime as dt
from datetime import timedelta as td
import pandas as pd
import pandas_datareader.data as web
import numpy as np

start = dt.datetime(2017, 7, 15)
df = web.DataReader('AAPL', 'google', start)
highs = df['High']
print('Index = ',highs.index(150.44))

When i use print('Index = ',highs.index(150.44)) i get the type error:

print('Index = ',highs.index(150.44))

TypeError: 'DatetimeIndex' object is not callable

Is their anyway to get the datetime index using a particular value from the dataframe?

2
  • What is the 150.44? Commented Jul 25, 2017 at 15:04
  • sorry, edited the question, please check now. Commented Jul 25, 2017 at 15:06

1 Answer 1

4

You have to use square braces since you are trying to index/slice into the DataFrame's index. So, instead of

df.index(...)

Use

df.index[...]

However, it seems you want to get the index of the column where the High is 150.44. You can do that like this, with boolean indexing:

highs[df['High'] == 150.44].index
# DatetimeIndex(['2017-07-21'], dtype='datetime64[ns]', name='Date', freq=None)

Or, more simply:

highs[df['High'] == 150.44].index.tolist()[0]
# Timestamp('2017-07-21 00:00:00')
Sign up to request clarification or add additional context in comments.

1 Comment

Highs[df['High'] == 150.44].index This returns date but with unnecessary information, is their anyway to just get output like 2017-07-21 @Coldspeed ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.