BlueQuant
BlueQuant

Reputation: 345

TypeError: 'DatetimeIndex' object is not callable

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?

Upvotes: 2

Views: 16264

Answers (1)

cs95
cs95

Reputation: 402263

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')

Upvotes: 4

Related Questions