TypeError: 'DatetimeIndex' object is not callable

10,752

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')
Share:
10,752
BlueQuant
Author by

BlueQuant

Updated on June 04, 2022

Comments

  • BlueQuant
    BlueQuant almost 2 years

    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?

    • patrick
      patrick almost 7 years
      What is the 150.44?
    • BlueQuant
      BlueQuant almost 7 years
      sorry, edited the question, please check now.
  • BlueQuant
    BlueQuant almost 7 years
    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 ?